Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/wandercn/gostd
Browse files Browse the repository at this point in the history
  • Loading branch information
wander committed Jun 27, 2024
2 parents 8b2c689 + 4d35631 commit 55145cb
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 16 deletions.
53 changes: 46 additions & 7 deletions time/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4059,6 +4059,18 @@ fn initLocal() -> Location {
"/usr/lib/locale/TZ/",
"tzdata/zoneinfo/",
];

let mut dir = Path::new(env!("CARGO_MANIFEST_DIR"))
.to_str()
.unwrap_or_default()
.replace("\\", "/")
.to_owned();

#[cfg(target_os = "windows")]
let zoneSources = vec![{
dir.push_str("/tzdata/zoneinfo/");
dir.as_str()
}];
// consult $TZ to find the time zone to use.
// no $TZ means use the system default /etc/localtime.
// $TZ="" means use UTC.
Expand Down Expand Up @@ -4431,6 +4443,19 @@ pub fn LoadLocation(name: &str) -> Result<Location, Error> {
"/usr/share/zoneinfo/",
"tzdata/zoneinfo/",
];

let mut dir = Path::new(env!("CARGO_MANIFEST_DIR"))
.to_str()
.unwrap_or_default()
.replace("\\", "/")
.to_owned();

#[cfg(target_os = "windows")]
let zoneSources = vec![{
dir.push_str("/tzdata/zoneinfo/");
dir.as_str()
}];

let zoneinfoOnce = Once::new();
if name == "" || name == "UTC" {
return Ok(UTC.clone());
Expand All @@ -4457,8 +4482,13 @@ pub fn LoadLocation(name: &str) -> Result<Location, Error> {
let z = LoadLocationFromTZData(name, zoneData)?;
return Ok(z);
} else {
let z = loadLocation(name, zoneSources)?;
return Ok(z);
match loadLocation(name, zoneSources) {
Ok(z) => Ok(z),
Err(e) => {
println!("loadLocation error:{}", e);
Err(e)
}
}
}
}

Expand Down Expand Up @@ -4561,9 +4591,13 @@ fn tzset(s: &str, initEnd: int64, sec: int64) -> (String, int, int64, int64, boo
if !ok || len!(s) > 0 {
return ("".to_string(), 0, 0, 0, false, false);
}

use std::num::Wrapping;
let (year, _, _, yday) = absDate(
uint64!((sec + unixToInternal + internalToAbsolute).abs()),
uint64!(
(Wrapping(sec) + Wrapping(unixToInternal) + Wrapping(internalToAbsolute))
.0
.abs()
),
false,
);

Expand Down Expand Up @@ -4875,6 +4909,7 @@ fn loadFromEmbeddedTZData(zipname: &str) -> Result<string, Error> {
use std::fs::File;
use std::io;
use std::io::prelude::*;
use std::path::Path;

fn loadTzinfoFromDirOrZip(dir: &str, name: &str) -> Result<Vec<byte>, Error> {
// 从zip文件读取时区信息,暂时不实现,大部分情况系统目录都有时区文件
Expand All @@ -4889,9 +4924,13 @@ fn loadTzinfoFromDirOrZip(dir: &str, name: &str) -> Result<Vec<byte>, Error> {
name = temp
}
let mut buf = Vec::new();
let mut f = File::open(name)?;
f.read_to_end(&mut buf)?;
Ok(buf)
match File::open(name) {
Ok(mut f) => {
f.read_to_end(&mut buf)?;
Ok(buf)
}
Err(err) => Err(err),
}
}

fn findZone(zones: Vec<zone>, name: String, offset: int, isDST: bool) -> int {
Expand Down
11 changes: 8 additions & 3 deletions time/src/sys.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
use gostd_builtin::*;
// 计算的时间是相对于系统启动时间的时间戳,以纳秒为单位
pub fn monotonic_now() -> uint64 {
inner::monotonic_now()
let mon_now = inner::monotonic_now();
// dbg!("mon_now: {}", mon_now);
mon_now
}

// 获取当前时间戳,包括秒部分和纳秒部分
pub fn real_time_now() -> (uint64, uint64) {
inner::real_time_now()
let real_now = inner::real_time_now();
// dbg!("real_now: {:?}", real_now);
real_now
}

#[cfg(all(all(unix), not(target_os = "macos")))]
Expand Down
16 changes: 10 additions & 6 deletions time/src/sys/windows.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
use cvt::cvt;
use gostd_builtin::*;
use std::mem;
use std::num::Wrapping;
use winapi::shared::minwindef::FILETIME;
use winapi::shared::ntdef::LARGE_INTEGER;
use winapi::um::profileapi::{QueryPerformanceCounter, QueryPerformanceFrequency};
use winapi::um::sysinfoapi::GetSystemTimePreciseAsFileTime;

const EPOCH_DIFFERENCE: u64 = 11644473600; // windos时间戳和Unix时间戳的差值以秒为单位

#[cfg(windows)]
pub fn monotonic_now() -> uint64 {
let mut frequency: LARGE_INTEGER = unsafe { mem::zeroed() };
let mut counter: LARGE_INTEGER = unsafe { mem::zeroed() };

cvt(unsafe { QueryPerformanceFrequency(&mut frequency as *mut _) }).unwrap();
cvt(unsafe { QueryPerformanceCounter(&mut counter as *mut _) }).unwrap();
let frequency_u64 = uint64!(unsafe { *frequency.QuadPart() });
let counter_u64 = uint64!(unsafe { *counter.QuadPart() });
let nanoseconds = counter_u64 * 1_000_000_000 / frequency_u64;
let frequency_u64 = uint64!(unsafe { *frequency.QuadPart() }); // 每秒钟周期数
let counter_u64 = uint64!(unsafe { *counter.QuadPart() }); // 总共计算周期数
let nanoseconds = counter_u64 / frequency_u64 * 1_000_000_000; //获得从系统启动以来的时间间隔信息以纳秒为单位

uint64!(nanoseconds)
}
Expand All @@ -27,9 +30,10 @@ pub fn real_time_now() -> (uint64, uint64) {
unsafe {
GetSystemTimePreciseAsFileTime(&mut t);
}

let nanoseconds = ((uint64!(t.dwHighDateTime)) << 32 | (uint64!(t.dwLowDateTime))) * 100;
let seconds = nanoseconds / 1_000_000_000;
let u1 = (uint64!(t.dwHighDateTime)) << 32;
let u2 = (uint64!(t.dwLowDateTime));
let nanoseconds = (u1 | u2) * 100;
let seconds = nanoseconds / 1_000_000_000 - EPOCH_DIFFERENCE;
let nanoseconds = nanoseconds % 1_000_000_000;

(uint64!(seconds), uint64!(nanoseconds))
Expand Down

0 comments on commit 55145cb

Please sign in to comment.