tm.tm_sec = (dayclock % 60) as i32;
tm.tm_min = ((dayclock % 3600) / 60) as i32;
tm.tm_hour = (dayclock / 3600) as i32;
tm.tm_wday = ((dayno + 4) % 7) as i32; loop { let yearsize = if leapyear(year) { 366
} else { 365
}; if dayno >= yearsize {
dayno -= yearsize;
year += 1;
} else { break;
}
}
tm.tm_year = (year - 1900) as i32;
tm.tm_yday = dayno as i32; letmut mon = 0; while dayno >= _ytab[if leapyear(year) { 1 } else { 0 }][mon] {
dayno -= _ytab[if leapyear(year) { 1 } else { 0 }][mon];
mon += 1;
}
tm.tm_mon = mon as i32;
tm.tm_mday = dayno as i32 + 1;
tm.tm_isdst = 0;
}
pubfn tm_to_time(tm: &Tm) -> i64 { letmut y = tm.tm_year as i64 + 1900; letmut m = tm.tm_mon as i64 + 1; if m <= 2 {
y -= 1;
m += 12;
} let d = tm.tm_mday as i64; let h = tm.tm_hour as i64; let mi = tm.tm_min as i64; let s = tm.tm_sec as i64;
(365*y + y/4 - y/100 + y/400 + 3*(m+1)/5 + 30*m + d - 719561)
* 86400 + 3600 * h + 60 * mi + s
}
}
#[cfg(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))))] mod inner { use std::ops::{Add, Sub}; use Tm; use Duration; usesuper::common::{time_to_tm, tm_to_time};
impl Sub for SteadyTime { type Output = Duration; fn sub(self, _other: SteadyTime) -> Duration {
unimplemented!()
}
}
impl Sub<Duration> for SteadyTime { type Output = SteadyTime; fn sub(self, _other: Duration) -> SteadyTime {
unimplemented!()
}
}
impl Add<Duration> for SteadyTime { type Output = SteadyTime; fn add(self, _other: Duration) -> SteadyTime {
unimplemented!()
}
}
}
#[cfg(target_os = "wasi")] mod inner { use std::ops::{Add, Sub}; use Tm; use Duration; usesuper::common::{time_to_tm, tm_to_time}; use wasi::{clock_time_get, CLOCKID_MONOTONIC, CLOCKID_REALTIME};
impl Sub for SteadyTime { type Output = Duration; fn sub(self, other: SteadyTime) -> Duration {
Duration::nanoseconds(self.t as i64 - other.t as i64)
}
}
impl Add<Duration> for SteadyTime { type Output = SteadyTime; fn add(self, other: Duration) -> SteadyTime { let delta = other.num_nanoseconds().unwrap();
SteadyTime {
t: (self.t as i64 + delta) as u64,
}
}
}
}
#[cfg(target_env = "sgx")] mod inner { use std::ops::{Add, Sub}; use Tm; use Duration; usesuper::common::{time_to_tm, tm_to_time}; use std::time::SystemTime;
/// The number of nanoseconds in seconds. const NANOS_PER_SEC: u64 = 1_000_000_000;
pubfn get_precise_ns() -> u64 { // This unwrap is safe because current time is well ahead of UNIX_EPOCH, unless system // clock is adjusted backward. let std_duration = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap();
std_duration.as_secs() * NANOS_PER_SEC + std_duration.subsec_nanos() as u64
}
impl SteadyTime { pubfn now() -> SteadyTime { // This unwrap is safe because current time is well ahead of UNIX_EPOCH, unless system // clock is adjusted backward. let std_duration = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap(); // This unwrap is safe because duration is well within the limits of i64. let duration = Duration::from_std(std_duration).unwrap();
SteadyTime { t: duration }
}
}
impl Sub for SteadyTime { type Output = Duration; fn sub(self, other: SteadyTime) -> Duration { self.t - other.t
}
}
impl Sub<Duration> for SteadyTime { type Output = SteadyTime; fn sub(self, other: Duration) -> SteadyTime {
SteadyTime { t: self.t - other }
}
}
impl Add<Duration> for SteadyTime { type Output = SteadyTime; fn add(self, other: Duration) -> SteadyTime {
SteadyTime { t: self.t + other }
}
}
}
#[cfg(unix)] mod inner { use libc::{self, time_t}; use std::mem; use std::io; use Tm;
#[cfg(any(target_os = "macos", target_os = "ios"))] mod mac { #[allow(deprecated)] use libc::{self, timeval, mach_timebase_info}; #[allow(deprecated)] use std::sync::{Once, ONCE_INIT}; use std::ops::{Add, Sub}; use Duration;
#[allow(deprecated)] #[inline] pubfn get_precise_ns() -> u64 { unsafe { let time = libc::mach_absolute_time(); let info = info();
time * info.numer as u64 / info.denom as u64
}
}
#[cfg(all(not(target_os = "macos"), not(target_os = "ios")))] mod unix { use std::fmt; use std::cmp::Ordering; use std::mem::zeroed; use std::ops::{Add, Sub}; use libc;
use Duration;
pubfn get_time() -> (i64, i32) { // SAFETY: libc::timespec is zero initializable. letmut tv: libc::timespec = unsafe { zeroed() }; unsafe { libc::clock_gettime(libc::CLOCK_REALTIME, &mut tv); }
(tv.tv_sec as i64, tv.tv_nsec as i32)
}
pubfn get_precise_ns() -> u64 { // SAFETY: libc::timespec is zero initializable. letmut ts: libc::timespec = unsafe { zeroed() }; unsafe {
libc::clock_gettime(libc::CLOCK_MONOTONIC, &mut ts);
}
(ts.tv_sec as u64) * 1000000000 + (ts.tv_nsec as u64)
}
impl Add<Duration> for SteadyTime { type Output = SteadyTime; fn add(mutself, other: Duration) -> SteadyTime { let seconds = other.num_seconds(); let nanoseconds = other - Duration::seconds(seconds); let nanoseconds = nanoseconds.num_nanoseconds().unwrap();
#[cfg(all(target_arch = "x86_64", target_pointer_width = "32"))] type nsec = i64; #[cfg(not(all(target_arch = "x86_64", target_pointer_width = "32")))] type nsec = libc::c_long;
self.t.tv_sec += seconds as libc::time_t; self.t.tv_nsec += nanoseconds as nsec; ifself.t.tv_nsec >= ::NSEC_PER_SEC as nsec { self.t.tv_nsec -= ::NSEC_PER_SEC as nsec; self.t.tv_sec += 1;
} elseifself.t.tv_nsec < 0 { self.t.tv_sec -= 1; self.t.tv_nsec += ::NSEC_PER_SEC as nsec;
} self
}
}
#[cfg(windows)] #[allow(non_snake_case)] mod inner { use std::io; use std::mem; #[allow(deprecated)] use std::sync::{Once, ONCE_INIT}; use std::ops::{Add, Sub}; use {Tm, Duration};
use winapi::um::winnt::*; use winapi::shared::minwindef::*; use winapi::um::minwinbase::SYSTEMTIME; use winapi::um::profileapi::*; use winapi::um::timezoneapi::*; use winapi::um::sysinfoapi::GetSystemTimeAsFileTime;
fn time_to_file_time(sec: i64) -> FILETIME { let t = (((sec * HECTONANOSECS_IN_SEC) + HECTONANOSEC_TO_UNIX_EPOCH)) as u64;
FILETIME {
dwLowDateTime: t as DWORD,
dwHighDateTime: (t >> 32) as DWORD
}
}
fn file_time_as_u64(ft: &FILETIME) -> u64 {
((ft.dwHighDateTime as u64) << 32) | (ft.dwLowDateTime as u64)
}
fn file_time_to_nsec(ft: &FILETIME) -> i32 { let t = file_time_as_u64(ft) as i64;
((t % HECTONANOSECS_IN_SEC) * 100) as i32
}
fn file_time_to_unix_seconds(ft: &FILETIME) -> i64 { let t = file_time_as_u64(ft) as i64;
((t - HECTONANOSEC_TO_UNIX_EPOCH) / HECTONANOSECS_IN_SEC) as i64
}
fn system_time_to_file_time(sys: &SYSTEMTIME) -> FILETIME { unsafe { letmut ft = mem::zeroed();
SystemTimeToFileTime(sys, &mut ft);
ft
}
}
fn tm_to_system_time(tm: &Tm) -> SYSTEMTIME { letmut sys: SYSTEMTIME = unsafe { mem::zeroed() };
sys.wSecond = tm.tm_sec as WORD;
sys.wMinute = tm.tm_min as WORD;
sys.wHour = tm.tm_hour as WORD;
sys.wDay = tm.tm_mday as WORD;
sys.wDayOfWeek = tm.tm_wday as WORD;
sys.wMonth = (tm.tm_mon + 1) as WORD;
sys.wYear = (tm.tm_year + 1900) as WORD;
sys
}
fn system_time_to_tm(sys: &SYSTEMTIME, tm: &mut Tm) {
tm.tm_sec = sys.wSecond as i32;
tm.tm_min = sys.wMinute as i32;
tm.tm_hour = sys.wHour as i32;
tm.tm_mday = sys.wDay as i32;
tm.tm_wday = sys.wDayOfWeek as i32;
tm.tm_mon = (sys.wMonth - 1) as i32;
tm.tm_year = (sys.wYear - 1900) as i32;
tm.tm_yday = yday(tm.tm_year, tm.tm_mon + 1, tm.tm_mday);
fn yday(year: i32, month: i32, day: i32) -> i32 { let leap = if month > 2 { if year % 4 == 0 { 1 } else { 2 }
} else { 0
}; let july = if month > 7 { 1 } else { 0 };
// SystemTimeToTzSpecificLocalTime already applied the biases so // check if it non standard
tm.tm_utcoff = (local_sec - sec) as i32;
tm.tm_isdst = if tm.tm_utcoff == -60 * (tz.Bias + tz.StandardBias) { 0
} else { 1
};
}
}
unsafe { letmut tz = mem::zeroed::<TIME_ZONE_INFORMATION>();
GetTimeZoneInformation(&mut tz); let ret = TzReset { old: tz }; // Since date set precisely this is 2015's dates
tz.Bias = 0;
tz.DaylightBias = -60;
tz.DaylightDate.wYear = 0;
tz.DaylightDate.wMonth = 3;
tz.DaylightDate.wDayOfWeek = 0;
tz.DaylightDate.wDay = 5;
tz.DaylightDate.wHour = 2;
tz.StandardBias = 0;
tz.StandardDate.wYear = 0;
tz.StandardDate.wMonth = 10;
tz.StandardDate.wDayOfWeek = 0;
tz.StandardDate.wDay = 5;
tz.StandardDate.wHour = 2;
call!(SetTimeZoneInformation(&tz)); return ret
}
}
// Ensures that this process has the necessary privileges to set a new time // zone, and this is all transcribed from: // https://msdn.microsoft.com/en-us/library/windows/desktop/ms724944%28v=vs.85%29.aspx #[cfg(test)] fn acquire_privileges() { use winapi::um::processthreadsapi::*; use winapi::um::winbase::LookupPrivilegeValueA; const SE_PRIVILEGE_ENABLED: DWORD = 2; #[allow(deprecated)] static INIT: Once = ONCE_INIT;
// Computes (value*numer)/denom without overflow, as long as both // (numer*denom) and the overall result fit into i64 (which is the case // for our time conversions). fn mul_div_i64(value: i64, numer: i64, denom: i64) -> i64 { let q = value / denom; let r = value % denom; // Decompose value as (value/denom*denom + value%denom), // substitute into (value*numer)/denom and simplify. // r < denom, so (denom*numer) is the upper bound of (r*numer)
q * numer + r * numer / denom
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.