| 1 |
#![allow(bad_style)] |
| 2 |
|
| 3 |
pub use self::inner::*; |
| 4 |
|
| 5 |
#[cfg(any( |
| 6 |
all(target_arch = "wasm32", not(target_os = "emscripten")), |
| 7 |
target_env = "sgx" |
| 8 |
))] |
| 9 |
mod common { |
| 10 |
use Tm; |
| 11 |
|
| 12 |
pub fn time_to_tm(ts: i64, tm: &mut Tm) { |
| 13 |
let leapyear = |year| -> bool { |
| 14 |
year % 4 == 0 && (year % 100 != 0 || year % 400 == 0) |
| 15 |
}; |
| 16 |
|
| 17 |
static _ytab: [[i64; 12]; 2] = [ |
| 18 |
[ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ], |
| 19 |
[ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ] |
| 20 |
]; |
| 21 |
|
| 22 |
let mut year = 1970; |
| 23 |
|
| 24 |
let dayclock = ts % 86400; |
| 25 |
let mut dayno = ts / 86400; |
| 26 |
|
| 27 |
tm.tm_sec = (dayclock % 60) as i32; |
| 28 |
tm.tm_min = ((dayclock % 3600) / 60) as i32; |
| 29 |
tm.tm_hour = (dayclock / 3600) as i32; |
| 30 |
tm.tm_wday = ((dayno + 4) % 7) as i32; |
| 31 |
loop { |
| 32 |
let yearsize = if leapyear(year) { |
| 33 |
366 |
| 34 |
} else { |
| 35 |
365 |
| 36 |
}; |
| 37 |
if dayno >= yearsize { |
| 38 |
dayno -= yearsize; |
| 39 |
year += 1; |
| 40 |
} else { |
| 41 |
break; |
| 42 |
} |
| 43 |
} |
| 44 |
tm.tm_year = (year - 1900) as i32; |
| 45 |
tm.tm_yday = dayno as i32; |
| 46 |
let mut mon = 0; |
| 47 |
while dayno >= _ytab[if leapyear(year) { 1 } else { 0 }][mon] { |
| 48 |
dayno -= _ytab[if leapyear(year) { 1 } else { 0 }][mon]; |
| 49 |
mon += 1; |
| 50 |
} |
| 51 |
tm.tm_mon = mon as i32; |
| 52 |
tm.tm_mday = dayno as i32 + 1; |
| 53 |
tm.tm_isdst = 0; |
| 54 |
} |
| 55 |
|
| 56 |
pub fn tm_to_time(tm: &Tm) -> i64 { |
| 57 |
let mut y = tm.tm_year as i64 + 1900; |
| 58 |
let mut m = tm.tm_mon as i64 + 1; |
| 59 |
if m <= 2 { |
| 60 |
y -= 1; |
| 61 |
m += 12; |
| 62 |
} |
| 63 |
let d = tm.tm_mday as i64; |
| 64 |
let h = tm.tm_hour as i64; |
| 65 |
let mi = tm.tm_min as i64; |
| 66 |
let s = tm.tm_sec as i64; |
| 67 |
(365*y + y/4 - y/100 + y/400 + 3*(m+1)/5 + 30*m + d - 719561) |
| 68 |
* 86400 + 3600 * h + 60 * mi + s |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
#[cfg(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))))] |
| 73 |
mod inner { |
| 74 |
use std::ops::{Add, Sub}; |
| 75 |
use Tm; |
| 76 |
use Duration; |
| 77 |
use super::common::{time_to_tm, tm_to_time}; |
| 78 |
|
| 79 |
#[derive(Copy, Clone, Debug, PartialOrd, Ord, PartialEq, Eq)] |
| 80 |
pub struct SteadyTime; |
| 81 |
|
| 82 |
pub fn time_to_utc_tm(sec: i64, tm: &mut Tm) { |
| 83 |
time_to_tm(sec, tm); |
| 84 |
} |
| 85 |
|
| 86 |
pub fn time_to_local_tm(sec: i64, tm: &mut Tm) { |
| 87 |
// FIXME: Add timezone logic |
| 88 |
time_to_tm(sec, tm); |
| 89 |
} |
| 90 |
|
| 91 |
pub fn utc_tm_to_time(tm: &Tm) -> i64 { |
| 92 |
tm_to_time(tm) |
| 93 |
} |
| 94 |
|
| 95 |
pub fn local_tm_to_time(tm: &Tm) -> i64 { |
| 96 |
// FIXME: Add timezone logic |
| 97 |
tm_to_time(tm) |
| 98 |
} |
| 99 |
|
| 100 |
pub fn get_time() -> (i64, i32) { |
| 101 |
unimplemented!() |
| 102 |
} |
| 103 |
|
| 104 |
pub fn get_precise_ns() -> u64 { |
| 105 |
unimplemented!() |
| 106 |
} |
| 107 |
|
| 108 |
impl SteadyTime { |
| 109 |
pub fn now() -> SteadyTime { |
| 110 |
unimplemented!() |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
impl Sub for SteadyTime { |
| 115 |
type Output = Duration; |
| 116 |
fn sub(self, _other: SteadyTime) -> Duration { |
| 117 |
unimplemented!() |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
impl Sub<Duration> for SteadyTime { |
| 122 |
type Output = SteadyTime; |
| 123 |
fn sub(self, _other: Duration) -> SteadyTime { |
| 124 |
unimplemented!() |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
impl Add<Duration> for SteadyTime { |
| 129 |
type Output = SteadyTime; |
| 130 |
fn add(self, _other: Duration) -> SteadyTime { |
| 131 |
unimplemented!() |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
#[cfg(target_os = "wasi")] |
| 137 |
mod inner { |
| 138 |
use std::ops::{Add, Sub}; |
| 139 |
use Tm; |
| 140 |
use Duration; |
| 141 |
use super::common::{time_to_tm, tm_to_time}; |
| 142 |
use wasi::{clock_time_get, CLOCKID_MONOTONIC, CLOCKID_REALTIME}; |
| 143 |
|
| 144 |
#[derive(Copy, Clone, Debug, PartialOrd, Ord, PartialEq, Eq)] |
| 145 |
pub struct SteadyTime { |
| 146 |
t: u64 |
| 147 |
} |
| 148 |
|
| 149 |
pub fn time_to_utc_tm(sec: i64, tm: &mut Tm) { |
| 150 |
time_to_tm(sec, tm); |
| 151 |
} |
| 152 |
|
| 153 |
pub fn time_to_local_tm(sec: i64, tm: &mut Tm) { |
| 154 |
// FIXME: Add timezone logic |
| 155 |
time_to_tm(sec, tm); |
| 156 |
} |
| 157 |
|
| 158 |
pub fn utc_tm_to_time(tm: &Tm) -> i64 { |
| 159 |
tm_to_time(tm) |
| 160 |
} |
| 161 |
|
| 162 |
pub fn local_tm_to_time(tm: &Tm) -> i64 { |
| 163 |
// FIXME: Add timezone logic |
| 164 |
tm_to_time(tm) |
| 165 |
} |
| 166 |
|
| 167 |
pub fn get_time() -> (i64, i32) { |
| 168 |
let ts = get_precise_ns(); |
| 169 |
( |
| 170 |
ts as i64 / 1_000_000_000, |
| 171 |
(ts as i64 % 1_000_000_000) as i32, |
| 172 |
) |
| 173 |
} |
| 174 |
|
| 175 |
pub fn get_precise_ns() -> u64 { |
| 176 |
unsafe { clock_time_get(CLOCKID_REALTIME, 1_000_000_000) } |
| 177 |
.expect("Host doesn't implement a real-time clock") |
| 178 |
} |
| 179 |
|
| 180 |
impl SteadyTime { |
| 181 |
pub fn now() -> SteadyTime { |
| 182 |
SteadyTime { |
| 183 |
t: unsafe { clock_time_get(CLOCKID_MONOTONIC, 1_000_000_000) } |
| 184 |
.expect("Host doesn't implement a monotonic clock"), |
| 185 |
} |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
impl Sub for SteadyTime { |
| 190 |
type Output = Duration; |
| 191 |
fn sub(self, other: SteadyTime) -> Duration { |
| 192 |
Duration::nanoseconds(self.t as i64 - other.t as i64) |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
impl Sub<Duration> for SteadyTime { |
| 197 |
type Output = SteadyTime; |
| 198 |
fn sub(self, other: Duration) -> SteadyTime { |
| 199 |
self + -other |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
impl Add<Duration> for SteadyTime { |
| 204 |
type Output = SteadyTime; |
| 205 |
fn add(self, other: Duration) -> SteadyTime { |
| 206 |
let delta = other.num_nanoseconds().unwrap(); |
| 207 |
SteadyTime { |
| 208 |
t: (self.t as i64 + delta) as u64, |
| 209 |
} |
| 210 |
} |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
#[cfg(target_env = "sgx")] |
| 215 |
mod inner { |
| 216 |
use std::ops::{Add, Sub}; |
| 217 |
use Tm; |
| 218 |
use Duration; |
| 219 |
use super::common::{time_to_tm, tm_to_time}; |
| 220 |
use std::time::SystemTime; |
| 221 |
|
| 222 |
/// The number of nanoseconds in seconds. |
| 223 |
const NANOS_PER_SEC: u64 = 1_000_000_000; |
| 224 |
|
| 225 |
#[derive(Copy, Clone, Debug, PartialOrd, Ord, PartialEq, Eq)] |
| 226 |
pub struct SteadyTime { |
| 227 |
t: Duration |
| 228 |
} |
| 229 |
|
| 230 |
pub fn time_to_utc_tm(sec: i64, tm: &mut Tm) { |
| 231 |
time_to_tm(sec, tm); |
| 232 |
} |
| 233 |
|
| 234 |
pub fn time_to_local_tm(sec: i64, tm: &mut Tm) { |
| 235 |
// FIXME: Add timezone logic |
| 236 |
time_to_tm(sec, tm); |
| 237 |
} |
| 238 |
|
| 239 |
pub fn utc_tm_to_time(tm: &Tm) -> i64 { |
| 240 |
tm_to_time(tm) |
| 241 |
} |
| 242 |
|
| 243 |
pub fn local_tm_to_time(tm: &Tm) -> i64 { |
| 244 |
// FIXME: Add timezone logic |
| 245 |
tm_to_time(tm) |
| 246 |
} |
| 247 |
|
| 248 |
pub fn get_time() -> (i64, i32) { |
| 249 |
SteadyTime::now().t.raw() |
| 250 |
} |
| 251 |
|
| 252 |
pub fn get_precise_ns() -> u64 { |
| 253 |
// This unwrap is safe because current time is well ahead of UNIX_EPOCH, unless system |
| 254 |
// clock is adjusted backward. |
| 255 |
let std_duration = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap(); |
| 256 |
std_duration.as_secs() * NANOS_PER_SEC + std_duration.subsec_nanos() as u64 |
| 257 |
} |
| 258 |
|
| 259 |
impl SteadyTime { |
| 260 |
pub fn now() -> SteadyTime { |
| 261 |
// This unwrap is safe because current time is well ahead of UNIX_EPOCH, unless system |
| 262 |
// clock is adjusted backward. |
| 263 |
let std_duration = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap(); |
| 264 |
// This unwrap is safe because duration is well within the limits of i64. |
| 265 |
let duration = Duration::from_std(std_duration).unwrap(); |
| 266 |
SteadyTime { t: duration } |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
impl Sub for SteadyTime { |
| 271 |
type Output = Duration; |
| 272 |
fn sub(self, other: SteadyTime) -> Duration { |
| 273 |
self.t - other.t |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
impl Sub<Duration> for SteadyTime { |
| 278 |
type Output = SteadyTime; |
| 279 |
fn sub(self, other: Duration) -> SteadyTime { |
| 280 |
SteadyTime { t: self.t - other } |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
impl Add<Duration> for SteadyTime { |
| 285 |
type Output = SteadyTime; |
| 286 |
fn add(self, other: Duration) -> SteadyTime { |
| 287 |
SteadyTime { t: self.t + other } |
| 288 |
} |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
#[cfg(unix)] |
| 293 |
mod inner { |
| 294 |
use libc::{self, time_t}; |
| 295 |
use std::mem; |
| 296 |
use std::io; |
| 297 |
use Tm; |
| 298 |
|
| 299 |
#[cfg(any(target_os = "macos", target_os = "ios"))] |
| 300 |
pub use self::mac::*; |
| 301 |
#[cfg(all(not(target_os = "macos"), not(target_os = "ios")))] |
| 302 |
pub use self::unix::*; |
| 303 |
|
| 304 |
#[cfg(any(target_os = "solaris", target_os = "illumos"))] |
| 305 |
extern { |
| 306 |
static timezone: time_t; |
| 307 |
static altzone: time_t; |
| 308 |
} |
| 309 |
|
| 310 |
fn rust_tm_to_tm(rust_tm: &Tm, tm: &mut libc::tm) { |
| 311 |
tm.tm_sec = rust_tm.tm_sec; |
| 312 |
tm.tm_min = rust_tm.tm_min; |
| 313 |
tm.tm_hour = rust_tm.tm_hour; |
| 314 |
tm.tm_mday = rust_tm.tm_mday; |
| 315 |
tm.tm_mon = rust_tm.tm_mon; |
| 316 |
tm.tm_year = rust_tm.tm_year; |
| 317 |
tm.tm_wday = rust_tm.tm_wday; |
| 318 |
tm.tm_yday = rust_tm.tm_yday; |
| 319 |
tm.tm_isdst = rust_tm.tm_isdst; |
| 320 |
} |
| 321 |
|
| 322 |
fn tm_to_rust_tm(tm: &libc::tm, utcoff: i32, rust_tm: &mut Tm) { |
| 323 |
rust_tm.tm_sec = tm.tm_sec; |
| 324 |
rust_tm.tm_min = tm.tm_min; |
| 325 |
rust_tm.tm_hour = tm.tm_hour; |
| 326 |
rust_tm.tm_mday = tm.tm_mday; |
| 327 |
rust_tm.tm_mon = tm.tm_mon; |
| 328 |
rust_tm.tm_year = tm.tm_year; |
| 329 |
rust_tm.tm_wday = tm.tm_wday; |
| 330 |
rust_tm.tm_yday = tm.tm_yday; |
| 331 |
rust_tm.tm_isdst = tm.tm_isdst; |
| 332 |
rust_tm.tm_utcoff = utcoff; |
| 333 |
} |
| 334 |
|
| 335 |
#[cfg(any(target_os = "nacl", target_os = "solaris", target_os = "illumos"))] |
| 336 |
unsafe fn timegm(tm: *mut libc::tm) -> time_t { |
| 337 |
use std::env::{set_var, var_os, remove_var}; |
| 338 |
extern { |
| 339 |
fn tzset(); |
| 340 |
} |
| 341 |
|
| 342 |
let ret; |
| 343 |
|
| 344 |
let current_tz = var_os("TZ"); |
| 345 |
set_var("TZ", "UTC"); |
| 346 |
tzset(); |
| 347 |
|
| 348 |
ret = libc::mktime(tm); |
| 349 |
|
| 350 |
if let Some(tz) = current_tz { |
| 351 |
set_var("TZ", tz); |
| 352 |
} else { |
| 353 |
remove_var("TZ"); |
| 354 |
} |
| 355 |
tzset(); |
| 356 |
|
| 357 |
ret |
| 358 |
} |
| 359 |
|
| 360 |
pub fn time_to_utc_tm(sec: i64, tm: &mut Tm) { |
| 361 |
unsafe { |
| 362 |
let sec = sec as time_t; |
| 363 |
let mut out = mem::zeroed(); |
| 364 |
if libc::gmtime_r(&sec, &mut out).is_null() { |
| 365 |
panic!("gmtime_r failed: {}", io::Error::last_os_error()); |
| 366 |
} |
| 367 |
tm_to_rust_tm(&out, 0, tm); |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
pub fn time_to_local_tm(sec: i64, tm: &mut Tm) { |
| 372 |
unsafe { |
| 373 |
let sec = sec as time_t; |
| 374 |
let mut out = mem::zeroed(); |
| 375 |
if libc::localtime_r(&sec, &mut out).is_null() { |
| 376 |
panic!("localtime_r failed: {}", io::Error::last_os_error()); |
| 377 |
} |
| 378 |
#[cfg(any(target_os = "solaris", target_os = "illumos"))] |
| 379 |
let gmtoff = { |
| 380 |
::tzset(); |
| 381 |
// < 0 means we don't know; assume we're not in DST. |
| 382 |
if out.tm_isdst == 0 { |
| 383 |
// timezone is seconds west of UTC, tm_gmtoff is seconds east |
| 384 |
-timezone |
| 385 |
} else if out.tm_isdst > 0 { |
| 386 |
-altzone |
| 387 |
} else { |
| 388 |
-timezone |
| 389 |
} |
| 390 |
}; |
| 391 |
#[cfg(not(any(target_os = "solaris", target_os = "illumos")))] |
| 392 |
let gmtoff = out.tm_gmtoff; |
| 393 |
tm_to_rust_tm(&out, gmtoff as i32, tm); |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
pub fn utc_tm_to_time(rust_tm: &Tm) -> i64 { |
| 398 |
#[cfg(all(target_os = "android", target_pointer_width = "32"))] |
| 399 |
use libc::timegm64 as timegm; |
| 400 |
#[cfg(not(any( |
| 401 |
all(target_os = "android", target_pointer_width = "32"), |
| 402 |
target_os = "nacl", |
| 403 |
target_os = "solaris", |
| 404 |
target_os = "illumos" |
| 405 |
)))] |
| 406 |
use libc::timegm; |
| 407 |
|
| 408 |
let mut tm = unsafe { mem::zeroed() }; |
| 409 |
rust_tm_to_tm(rust_tm, &mut tm); |
| 410 |
unsafe { timegm(&mut tm) as i64 } |
| 411 |
} |
| 412 |
|
| 413 |
pub fn local_tm_to_time(rust_tm: &Tm) -> i64 { |
| 414 |
let mut tm = unsafe { mem::zeroed() }; |
| 415 |
rust_tm_to_tm(rust_tm, &mut tm); |
| 416 |
unsafe { libc::mktime(&mut tm) as i64 } |
| 417 |
} |
| 418 |
|
| 419 |
#[cfg(any(target_os = "macos", target_os = "ios"))] |
| 420 |
mod mac { |
| 421 |
#[allow(deprecated)] |
| 422 |
use libc::{self, timeval, mach_timebase_info}; |
| 423 |
#[allow(deprecated)] |
| 424 |
use std::sync::{Once, ONCE_INIT}; |
| 425 |
use std::ops::{Add, Sub}; |
| 426 |
use Duration; |
| 427 |
|
| 428 |
#[allow(deprecated)] |
| 429 |
fn info() -> &'static mach_timebase_info { |
| 430 |
static mut INFO: mach_timebase_info = mach_timebase_info { |
| 431 |
numer: 0, |
| 432 |
denom: 0, |
| 433 |
}; |
| 434 |
static ONCE: Once = ONCE_INIT; |
| 435 |
|
| 436 |
unsafe { |
| 437 |
ONCE.call_once(|| { |
| 438 |
mach_timebase_info(&mut INFO); |
| 439 |
}); |
| 440 |
&INFO |
| 441 |
} |
| 442 |
} |
| 443 |
|
| 444 |
pub fn get_time() -> (i64, i32) { |
| 445 |
use std::ptr; |
| 446 |
let mut tv = timeval { tv_sec: 0, tv_usec: 0 }; |
| 447 |
unsafe { libc::gettimeofday(&mut tv, ptr::null_mut()); } |
| 448 |
(tv.tv_sec as i64, tv.tv_usec * 1000) |
| 449 |
} |
| 450 |
|
| 451 |
#[allow(deprecated)] |
| 452 |
#[inline] |
| 453 |
pub fn get_precise_ns() -> u64 { |
| 454 |
unsafe { |
| 455 |
let time = libc::mach_absolute_time(); |
| 456 |
let info = info(); |
| 457 |
time * info.numer as u64 / info.denom as u64 |
| 458 |
} |
| 459 |
} |
| 460 |
|
| 461 |
#[derive(Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Debug)] |
| 462 |
pub struct SteadyTime { t: u64 } |
| 463 |
|
| 464 |
impl SteadyTime { |
| 465 |
pub fn now() -> SteadyTime { |
| 466 |
SteadyTime { t: get_precise_ns() } |
| 467 |
} |
| 468 |
} |
| 469 |
impl Sub for SteadyTime { |
| 470 |
type Output = Duration; |
| 471 |
fn sub(self, other: SteadyTime) -> Duration { |
| 472 |
Duration::nanoseconds(self.t as i64 - other.t as i64) |
| 473 |
} |
| 474 |
} |
| 475 |
impl Sub<Duration> for SteadyTime { |
| 476 |
type Output = SteadyTime; |
| 477 |
fn sub(self, other: Duration) -> SteadyTime { |
| 478 |
self + -other |
| 479 |
} |
| 480 |
} |
| 481 |
impl Add<Duration> for SteadyTime { |
| 482 |
type Output = SteadyTime; |
| 483 |
fn add(self, other: Duration) -> SteadyTime { |
| 484 |
let delta = other.num_nanoseconds().unwrap(); |
| 485 |
SteadyTime { |
| 486 |
t: (self.t as i64 + delta) as u64 |
| 487 |
} |
| 488 |
} |
| 489 |
} |
| 490 |
} |
| 491 |
|
| 492 |
#[cfg(test)] |
| 493 |
pub struct TzReset; |
| 494 |
|
| 495 |
#[cfg(test)] |
| 496 |
pub fn set_los_angeles_time_zone() -> TzReset { |
| 497 |
use std::env; |
| 498 |
env::set_var("TZ", "America/Los_Angeles"); |
| 499 |
::tzset(); |
| 500 |
TzReset |
| 501 |
} |
| 502 |
|
| 503 |
#[cfg(test)] |
| 504 |
pub fn set_london_with_dst_time_zone() -> TzReset { |
| 505 |
use std::env; |
| 506 |
env::set_var("TZ", "Europe/London"); |
| 507 |
::tzset(); |
| 508 |
TzReset |
| 509 |
} |
| 510 |
|
| 511 |
#[cfg(all(not(target_os = "macos"), not(target_os = "ios")))] |
| 512 |
mod unix { |
| 513 |
use std::fmt; |
| 514 |
use std::cmp::Ordering; |
| 515 |
use std::mem::zeroed; |
| 516 |
use std::ops::{Add, Sub}; |
| 517 |
use libc; |
| 518 |
|
| 519 |
use Duration; |
| 520 |
|
| 521 |
pub fn get_time() -> (i64, i32) { |
| 522 |
// SAFETY: libc::timespec is zero initializable. |
| 523 |
let mut tv: libc::timespec = unsafe { zeroed() }; |
| 524 |
unsafe { libc::clock_gettime(libc::CLOCK_REALTIME, &mut tv); } |
| 525 |
(tv.tv_sec as i64, tv.tv_nsec as i32) |
| 526 |
} |
| 527 |
|
| 528 |
pub fn get_precise_ns() -> u64 { |
| 529 |
// SAFETY: libc::timespec is zero initializable. |
| 530 |
let mut ts: libc::timespec = unsafe { zeroed() }; |
| 531 |
unsafe { |
| 532 |
libc::clock_gettime(libc::CLOCK_MONOTONIC, &mut ts); |
| 533 |
} |
| 534 |
(ts.tv_sec as u64) * 1000000000 + (ts.tv_nsec as u64) |
| 535 |
} |
| 536 |
|
| 537 |
#[derive(Copy)] |
| 538 |
pub struct SteadyTime { |
| 539 |
t: libc::timespec, |
| 540 |
} |
| 541 |
|
| 542 |
impl fmt::Debug for SteadyTime { |
| 543 |
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { |
| 544 |
write!(fmt, "SteadyTime {{ tv_sec: {:?}, tv_nsec: {:?} }}", |
| 545 |
self.t.tv_sec, self.t.tv_nsec) |
| 546 |
} |
| 547 |
} |
| 548 |
|
| 549 |
impl Clone for SteadyTime { |
| 550 |
fn clone(&self) -> SteadyTime { |
| 551 |
SteadyTime { t: self.t } |
| 552 |
} |
| 553 |
} |
| 554 |
|
| 555 |
impl SteadyTime { |
| 556 |
pub fn now() -> SteadyTime { |
| 557 |
let mut t = SteadyTime { |
| 558 |
// SAFETY: libc::timespec is zero initializable. |
| 559 |
t: unsafe { zeroed() } |
| 560 |
}; |
| 561 |
unsafe { |
| 562 |
assert_eq!(0, libc::clock_gettime(libc::CLOCK_MONOTONIC, |
| 563 |
&mut t.t)); |
| 564 |
} |
| 565 |
t |
| 566 |
} |
| 567 |
} |
| 568 |
|
| 569 |
impl Sub for SteadyTime { |
| 570 |
type Output = Duration; |
| 571 |
fn sub(self, other: SteadyTime) -> Duration { |
| 572 |
if self.t.tv_nsec >= other.t.tv_nsec { |
| 573 |
Duration::seconds(self.t.tv_sec as i64 - other.t.tv_sec as i64) + |
| 574 |
Duration::nanoseconds(self.t.tv_nsec as i64 - other.t.tv_nsec as i64) |
| 575 |
} else { |
| 576 |
Duration::seconds(self.t.tv_sec as i64 - 1 - other.t.tv_sec as i64) + |
| 577 |
Duration::nanoseconds(self.t.tv_nsec as i64 + ::NSEC_PER_SEC as i64 - |
| 578 |
other.t.tv_nsec as i64) |
| 579 |
} |
| 580 |
} |
| 581 |
} |
| 582 |
|
| 583 |
impl Sub<Duration> for SteadyTime { |
| 584 |
type Output = SteadyTime; |
| 585 |
fn sub(self, other: Duration) -> SteadyTime { |
| 586 |
self + -other |
| 587 |
} |
| 588 |
} |
| 589 |
|
| 590 |
impl Add<Duration> for SteadyTime { |
| 591 |
type Output = SteadyTime; |
| 592 |
fn add(mut self, other: Duration) -> SteadyTime { |
| 593 |
let seconds = other.num_seconds(); |
| 594 |
let nanoseconds = other - Duration::seconds(seconds); |
| 595 |
let nanoseconds = nanoseconds.num_nanoseconds().unwrap(); |
| 596 |
|
| 597 |
#[cfg(all(target_arch = "x86_64", target_pointer_width = "32"))] |
| 598 |
type nsec = i64; |
| 599 |
#[cfg(not(all(target_arch = "x86_64", target_pointer_width = "32")))] |
| 600 |
type nsec = libc::c_long; |
| 601 |
|
| 602 |
self.t.tv_sec += seconds as libc::time_t; |
| 603 |
self.t.tv_nsec += nanoseconds as nsec; |
| 604 |
if self.t.tv_nsec >= ::NSEC_PER_SEC as nsec { |
| 605 |
self.t.tv_nsec -= ::NSEC_PER_SEC as nsec; |
| 606 |
self.t.tv_sec += 1; |
| 607 |
} else if self.t.tv_nsec < 0 { |
| 608 |
self.t.tv_sec -= 1; |
| 609 |
self.t.tv_nsec += ::NSEC_PER_SEC as nsec; |
| 610 |
} |
| 611 |
self |
| 612 |
} |
| 613 |
} |
| 614 |
|
| 615 |
impl PartialOrd for SteadyTime { |
| 616 |
fn partial_cmp(&self, other: &SteadyTime) -> Option<Ordering> { |
| 617 |
Some(self.cmp(other)) |
| 618 |
} |
| 619 |
} |
| 620 |
|
| 621 |
impl Ord for SteadyTime { |
| 622 |
fn cmp(&self, other: &SteadyTime) -> Ordering { |
| 623 |
match self.t.tv_sec.cmp(&other.t.tv_sec) { |
| 624 |
Ordering::Equal => self.t.tv_nsec.cmp(&other.t.tv_nsec), |
| 625 |
ord => ord |
| 626 |
} |
| 627 |
} |
| 628 |
} |
| 629 |
|
| 630 |
impl PartialEq for SteadyTime { |
| 631 |
fn eq(&self, other: &SteadyTime) -> bool { |
| 632 |
self.t.tv_sec == other.t.tv_sec && |
| 633 |
self.t.tv_nsec == other.t.tv_nsec |
| 634 |
} |
| 635 |
} |
| 636 |
|
| 637 |
impl Eq for SteadyTime {} |
| 638 |
|
| 639 |
} |
| 640 |
} |
| 641 |
|
| 642 |
#[cfg(windows)] |
| 643 |
#[allow(non_snake_case)] |
| 644 |
mod inner { |
| 645 |
use std::io; |
| 646 |
use std::mem; |
| 647 |
#[allow(deprecated)] |
| 648 |
use std::sync::{Once, ONCE_INIT}; |
| 649 |
use std::ops::{Add, Sub}; |
| 650 |
use {Tm, Duration}; |
| 651 |
|
| 652 |
use winapi::um::winnt::*; |
| 653 |
use winapi::shared::minwindef::*; |
| 654 |
use winapi::um::minwinbase::SYSTEMTIME; |
| 655 |
use winapi::um::profileapi::*; |
| 656 |
use winapi::um::timezoneapi::*; |
| 657 |
use winapi::um::sysinfoapi::GetSystemTimeAsFileTime; |
| 658 |
|
| 659 |
fn frequency() -> i64 { |
| 660 |
static mut FREQUENCY: i64 = 0; |
| 661 |
#[allow(deprecated)] |
| 662 |
static ONCE: Once = ONCE_INIT; |
| 663 |
|
| 664 |
unsafe { |
| 665 |
ONCE.call_once(|| { |
| 666 |
let mut l = i64_to_large_integer(0); |
| 667 |
QueryPerformanceFrequency(&mut l); |
| 668 |
FREQUENCY = large_integer_to_i64(l); |
| 669 |
}); |
| 670 |
FREQUENCY |
| 671 |
} |
| 672 |
} |
| 673 |
|
| 674 |
fn i64_to_large_integer(i: i64) -> LARGE_INTEGER { |
| 675 |
unsafe { |
| 676 |
let mut large_integer: LARGE_INTEGER = mem::zeroed(); |
| 677 |
*large_integer.QuadPart_mut() = i; |
| 678 |
large_integer |
| 679 |
} |
| 680 |
} |
| 681 |
|
| 682 |
fn large_integer_to_i64(l: LARGE_INTEGER) -> i64 { |
| 683 |
unsafe { |
| 684 |
*l.QuadPart() |
| 685 |
} |
| 686 |
} |
| 687 |
|
| 688 |
const HECTONANOSECS_IN_SEC: i64 = 10_000_000; |
| 689 |
const HECTONANOSEC_TO_UNIX_EPOCH: i64 = 11_644_473_600 * HECTONANOSECS_IN_SEC; |
| 690 |
|
| 691 |
fn time_to_file_time(sec: i64) -> FILETIME { |
| 692 |
let t = (((sec * HECTONANOSECS_IN_SEC) + HECTONANOSEC_TO_UNIX_EPOCH)) as u64; |
| 693 |
FILETIME { |
| 694 |
dwLowDateTime: t as DWORD, |
| 695 |
dwHighDateTime: (t >> 32) as DWORD |
| 696 |
} |
| 697 |
} |
| 698 |
|
| 699 |
fn file_time_as_u64(ft: &FILETIME) -> u64 { |
| 700 |
((ft.dwHighDateTime as u64) << 32) | (ft.dwLowDateTime as u64) |
| 701 |
} |
| 702 |
|
| 703 |
fn file_time_to_nsec(ft: &FILETIME) -> i32 { |
| 704 |
let t = file_time_as_u64(ft) as i64; |
| 705 |
((t % HECTONANOSECS_IN_SEC) * 100) as i32 |
| 706 |
} |
| 707 |
|
| 708 |
fn file_time_to_unix_seconds(ft: &FILETIME) -> i64 { |
| 709 |
let t = file_time_as_u64(ft) as i64; |
| 710 |
((t - HECTONANOSEC_TO_UNIX_EPOCH) / HECTONANOSECS_IN_SEC) as i64 |
| 711 |
} |
| 712 |
|
| 713 |
fn system_time_to_file_time(sys: &SYSTEMTIME) -> FILETIME { |
| 714 |
unsafe { |
| 715 |
let mut ft = mem::zeroed(); |
| 716 |
SystemTimeToFileTime(sys, &mut ft); |
| 717 |
ft |
| 718 |
} |
| 719 |
} |
| 720 |
|
| 721 |
fn tm_to_system_time(tm: &Tm) -> SYSTEMTIME { |
| 722 |
let mut sys: SYSTEMTIME = unsafe { mem::zeroed() }; |
| 723 |
sys.wSecond = tm.tm_sec as WORD; |
| 724 |
sys.wMinute = tm.tm_min as WORD; |
| 725 |
sys.wHour = tm.tm_hour as WORD; |
| 726 |
sys.wDay = tm.tm_mday as WORD; |
| 727 |
sys.wDayOfWeek = tm.tm_wday as WORD; |
| 728 |
sys.wMonth = (tm.tm_mon + 1) as WORD; |
| 729 |
sys.wYear = (tm.tm_year + 1900) as WORD; |
| 730 |
sys |
| 731 |
} |
| 732 |
|
| 733 |
fn system_time_to_tm(sys: &SYSTEMTIME, tm: &mut Tm) { |
| 734 |
tm.tm_sec = sys.wSecond as i32; |
| 735 |
tm.tm_min = sys.wMinute as i32; |
| 736 |
tm.tm_hour = sys.wHour as i32; |
| 737 |
tm.tm_mday = sys.wDay as i32; |
| 738 |
tm.tm_wday = sys.wDayOfWeek as i32; |
| 739 |
tm.tm_mon = (sys.wMonth - 1) as i32; |
| 740 |
tm.tm_year = (sys.wYear - 1900) as i32; |
| 741 |
tm.tm_yday = yday(tm.tm_year, tm.tm_mon + 1, tm.tm_mday); |
| 742 |
|
| 743 |
fn yday(year: i32, month: i32, day: i32) -> i32 { |
| 744 |
let leap = if month > 2 { |
| 745 |
if year % 4 == 0 { 1 } else { 2 } |
| 746 |
} else { |
| 747 |
0 |
| 748 |
}; |
| 749 |
let july = if month > 7 { 1 } else { 0 }; |
| 750 |
|
| 751 |
(month - 1) * 30 + month / 2 + (day - 1) - leap + july |
| 752 |
} |
| 753 |
} |
| 754 |
|
| 755 |
macro_rules! call { |
| 756 |
($name:ident($($arg:expr),*)) => { |
| 757 |
if $name($($arg),*) == 0 { |
| 758 |
panic!(concat!(stringify!($name), " failed with: {}"), |
| 759 |
io::Error::last_os_error()); |
| 760 |
} |
| 761 |
} |
| 762 |
} |
| 763 |
|
| 764 |
pub fn time_to_utc_tm(sec: i64, tm: &mut Tm) { |
| 765 |
let mut out = unsafe { mem::zeroed() }; |
| 766 |
let ft = time_to_file_time(sec); |
| 767 |
unsafe { |
| 768 |
call!(FileTimeToSystemTime(&ft, &mut out)); |
| 769 |
} |
| 770 |
system_time_to_tm(&out, tm); |
| 771 |
tm.tm_utcoff = 0; |
| 772 |
} |
| 773 |
|
| 774 |
pub fn time_to_local_tm(sec: i64, tm: &mut Tm) { |
| 775 |
let ft = time_to_file_time(sec); |
| 776 |
unsafe { |
| 777 |
let mut utc = mem::zeroed(); |
| 778 |
let mut local = mem::zeroed(); |
| 779 |
call!(FileTimeToSystemTime(&ft, &mut utc)); |
| 780 |
call!(SystemTimeToTzSpecificLocalTime(0 as *const _, |
| 781 |
&mut utc, &mut local)); |
| 782 |
system_time_to_tm(&local, tm); |
| 783 |
|
| 784 |
let local = system_time_to_file_time(&local); |
| 785 |
let local_sec = file_time_to_unix_seconds(&local); |
| 786 |
|
| 787 |
let mut tz = mem::zeroed(); |
| 788 |
GetTimeZoneInformation(&mut tz); |
| 789 |
|
| 790 |
// SystemTimeToTzSpecificLocalTime already applied the biases so |
| 791 |
// check if it non standard |
| 792 |
tm.tm_utcoff = (local_sec - sec) as i32; |
| 793 |
tm.tm_isdst = if tm.tm_utcoff == -60 * (tz.Bias + tz.StandardBias) { |
| 794 |
0 |
| 795 |
} else { |
| 796 |
1 |
| 797 |
}; |
| 798 |
} |
| 799 |
} |
| 800 |
|
| 801 |
pub fn utc_tm_to_time(tm: &Tm) -> i64 { |
| 802 |
unsafe { |
| 803 |
let mut ft = mem::zeroed(); |
| 804 |
let sys_time = tm_to_system_time(tm); |
| 805 |
call!(SystemTimeToFileTime(&sys_time, &mut ft)); |
| 806 |
file_time_to_unix_seconds(&ft) |
| 807 |
} |
| 808 |
} |
| 809 |
|
| 810 |
pub fn local_tm_to_time(tm: &Tm) -> i64 { |
| 811 |
unsafe { |
| 812 |
let mut ft = mem::zeroed(); |
| 813 |
let mut utc = mem::zeroed(); |
| 814 |
let mut sys_time = tm_to_system_time(tm); |
| 815 |
call!(TzSpecificLocalTimeToSystemTime(0 as *mut _, |
| 816 |
&mut sys_time, &mut utc)); |
| 817 |
call!(SystemTimeToFileTime(&utc, &mut ft)); |
| 818 |
file_time_to_unix_seconds(&ft) |
| 819 |
} |
| 820 |
} |
| 821 |
|
| 822 |
pub fn get_time() -> (i64, i32) { |
| 823 |
unsafe { |
| 824 |
let mut ft = mem::zeroed(); |
| 825 |
GetSystemTimeAsFileTime(&mut ft); |
| 826 |
(file_time_to_unix_seconds(&ft), file_time_to_nsec(&ft)) |
| 827 |
} |
| 828 |
} |
| 829 |
|
| 830 |
pub fn get_precise_ns() -> u64 { |
| 831 |
let mut ticks = i64_to_large_integer(0); |
| 832 |
unsafe { |
| 833 |
assert!(QueryPerformanceCounter(&mut ticks) == 1); |
| 834 |
} |
| 835 |
mul_div_i64(large_integer_to_i64(ticks), 1000000000, frequency()) as u64 |
| 836 |
|
| 837 |
} |
| 838 |
|
| 839 |
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)] |
| 840 |
pub struct SteadyTime { |
| 841 |
t: i64, |
| 842 |
} |
| 843 |
|
| 844 |
impl SteadyTime { |
| 845 |
pub fn now() -> SteadyTime { |
| 846 |
let mut l = i64_to_large_integer(0); |
| 847 |
unsafe { QueryPerformanceCounter(&mut l); } |
| 848 |
SteadyTime { t : large_integer_to_i64(l) } |
| 849 |
} |
| 850 |
} |
| 851 |
|
| 852 |
impl Sub for SteadyTime { |
| 853 |
type Output = Duration; |
| 854 |
fn sub(self, other: SteadyTime) -> Duration { |
| 855 |
let diff = self.t as i64 - other.t as i64; |
| 856 |
Duration::nanoseconds(mul_div_i64(diff, 1000000000, |
| 857 |
frequency())) |
| 858 |
} |
| 859 |
} |
| 860 |
|
| 861 |
impl Sub<Duration> for SteadyTime { |
| 862 |
type Output = SteadyTime; |
| 863 |
fn sub(self, other: Duration) -> SteadyTime { |
| 864 |
self + -other |
| 865 |
} |
| 866 |
} |
| 867 |
|
| 868 |
impl Add<Duration> for SteadyTime { |
| 869 |
type Output = SteadyTime; |
| 870 |
fn add(mut self, other: Duration) -> SteadyTime { |
| 871 |
self.t += (other.num_microseconds().unwrap() * frequency() / |
| 872 |
1_000_000) as i64; |
| 873 |
self |
| 874 |
} |
| 875 |
} |
| 876 |
|
| 877 |
#[cfg(test)] |
| 878 |
pub struct TzReset { |
| 879 |
old: TIME_ZONE_INFORMATION, |
| 880 |
} |
| 881 |
|
| 882 |
#[cfg(test)] |
| 883 |
impl Drop for TzReset { |
| 884 |
fn drop(&mut self) { |
| 885 |
unsafe { |
| 886 |
call!(SetTimeZoneInformation(&self.old)); |
| 887 |
} |
| 888 |
} |
| 889 |
} |
| 890 |
|
| 891 |
#[cfg(test)] |
| 892 |
pub fn set_los_angeles_time_zone() -> TzReset { |
| 893 |
acquire_privileges(); |
| 894 |
|
| 895 |
unsafe { |
| 896 |
let mut tz = mem::zeroed::<TIME_ZONE_INFORMATION>(); |
| 897 |
GetTimeZoneInformation(&mut tz); |
| 898 |
let ret = TzReset { old: tz }; |
| 899 |
tz.Bias = 60 * 8; |
| 900 |
call!(SetTimeZoneInformation(&tz)); |
| 901 |
return ret |
| 902 |
} |
| 903 |
} |
| 904 |
|
| 905 |
#[cfg(test)] |
| 906 |
pub fn set_london_with_dst_time_zone() -> TzReset { |
| 907 |
acquire_privileges(); |
| 908 |
|
| 909 |
unsafe { |
| 910 |
let mut tz = mem::zeroed::<TIME_ZONE_INFORMATION>(); |
| 911 |
GetTimeZoneInformation(&mut tz); |
| 912 |
let ret = TzReset { old: tz }; |
| 913 |
// Since date set precisely this is 2015's dates |
| 914 |
tz.Bias = 0; |
| 915 |
tz.DaylightBias = -60; |
| 916 |
tz.DaylightDate.wYear = 0; |
| 917 |
tz.DaylightDate.wMonth = 3; |
| 918 |
tz.DaylightDate.wDayOfWeek = 0; |
| 919 |
tz.DaylightDate.wDay = 5; |
| 920 |
tz.DaylightDate.wHour = 2; |
| 921 |
tz.StandardBias = 0; |
| 922 |
tz.StandardDate.wYear = 0; |
| 923 |
tz.StandardDate.wMonth = 10; |
| 924 |
tz.StandardDate.wDayOfWeek = 0; |
| 925 |
tz.StandardDate.wDay = 5; |
| 926 |
tz.StandardDate.wHour = 2; |
| 927 |
call!(SetTimeZoneInformation(&tz)); |
| 928 |
return ret |
| 929 |
} |
| 930 |
} |
| 931 |
|
| 932 |
// Ensures that this process has the necessary privileges to set a new time |
| 933 |
// zone, and this is all transcribed from: |
| 934 |
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms724944%28v=vs.85%29.aspx |
| 935 |
#[cfg(test)] |
| 936 |
fn acquire_privileges() { |
| 937 |
use winapi::um::processthreadsapi::*; |
| 938 |
use winapi::um::winbase::LookupPrivilegeValueA; |
| 939 |
const SE_PRIVILEGE_ENABLED: DWORD = 2; |
| 940 |
#[allow(deprecated)] |
| 941 |
static INIT: Once = ONCE_INIT; |
| 942 |
|
| 943 |
// TODO: FIXME |
| 944 |
extern "system" { |
| 945 |
fn AdjustTokenPrivileges( |
| 946 |
TokenHandle: HANDLE, DisableAllPrivileges: BOOL, NewState: PTOKEN_PRIVILEGES, |
| 947 |
BufferLength: DWORD, PreviousState: PTOKEN_PRIVILEGES, ReturnLength: PDWORD, |
| 948 |
) -> BOOL; |
| 949 |
} |
| 950 |
|
| 951 |
INIT.call_once(|| unsafe { |
| 952 |
let mut hToken = 0 as *mut _; |
| 953 |
call!(OpenProcessToken(GetCurrentProcess(), |
| 954 |
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, |
| 955 |
&mut hToken)); |
| 956 |
|
| 957 |
let mut tkp = mem::zeroed::<TOKEN_PRIVILEGES>(); |
| 958 |
assert_eq!(tkp.Privileges.len(), 1); |
| 959 |
let c = ::std::ffi::CString::new("SeTimeZonePrivilege").unwrap(); |
| 960 |
call!(LookupPrivilegeValueA(0 as *const _, c.as_ptr(), |
| 961 |
&mut tkp.Privileges[0].Luid)); |
| 962 |
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; |
| 963 |
tkp.PrivilegeCount = 1; |
| 964 |
call!(AdjustTokenPrivileges(hToken, FALSE, &mut tkp, 0, |
| 965 |
0 as *mut _, 0 as *mut _)); |
| 966 |
}); |
| 967 |
} |
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
// Computes (value*numer)/denom without overflow, as long as both |
| 972 |
// (numer*denom) and the overall result fit into i64 (which is the case |
| 973 |
// for our time conversions). |
| 974 |
fn mul_div_i64(value: i64, numer: i64, denom: i64) -> i64 { |
| 975 |
let q = value / denom; |
| 976 |
let r = value % denom; |
| 977 |
// Decompose value as (value/denom*denom + value%denom), |
| 978 |
// substitute into (value*numer)/denom and simplify. |
| 979 |
// r < denom, so (denom*numer) is the upper bound of (r*numer) |
| 980 |
q * numer + r * numer / denom |
| 981 |
} |
| 982 |
|
| 983 |
#[test] |
| 984 |
fn test_muldiv() { |
| 985 |
assert_eq!(mul_div_i64( 1_000_000_000_001, 1_000_000_000, 1_000_000), |
| 986 |
1_000_000_000_001_000); |
| 987 |
assert_eq!(mul_div_i64(-1_000_000_000_001, 1_000_000_000, 1_000_000), |
| 988 |
-1_000_000_000_001_000); |
| 989 |
assert_eq!(mul_div_i64(-1_000_000_000_001,-1_000_000_000, 1_000_000), |
| 990 |
1_000_000_000_001_000); |
| 991 |
assert_eq!(mul_div_i64( 1_000_000_000_001, 1_000_000_000,-1_000_000), |
| 992 |
-1_000_000_000_001_000); |
| 993 |
assert_eq!(mul_div_i64( 1_000_000_000_001,-1_000_000_000,-1_000_000), |
| 994 |
1_000_000_000_001_000); |
| 995 |
} |
| 996 |
} |