1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10 
11 use super::Tm;
12 use std::io;
13 use std::mem;
14 
15 use winapi::shared::minwindef::*;
16 use winapi::um::minwinbase::SYSTEMTIME;
17 use winapi::um::timezoneapi::*;
18 
19 const HECTONANOSECS_IN_SEC: i64 = 10_000_000;
20 const HECTONANOSEC_TO_UNIX_EPOCH: i64 = 11_644_473_600 * HECTONANOSECS_IN_SEC;
21 
time_to_file_time(sec: i64) -> FILETIME22 fn time_to_file_time(sec: i64) -> FILETIME {
23     let t = ((sec * HECTONANOSECS_IN_SEC) + HECTONANOSEC_TO_UNIX_EPOCH) as u64;
24     FILETIME { dwLowDateTime: t as DWORD, dwHighDateTime: (t >> 32) as DWORD }
25 }
26 
file_time_as_u64(ft: &FILETIME) -> u6427 fn file_time_as_u64(ft: &FILETIME) -> u64 {
28     ((ft.dwHighDateTime as u64) << 32) | (ft.dwLowDateTime as u64)
29 }
30 
file_time_to_unix_seconds(ft: &FILETIME) -> i6431 fn file_time_to_unix_seconds(ft: &FILETIME) -> i64 {
32     let t = file_time_as_u64(ft) as i64;
33     ((t - HECTONANOSEC_TO_UNIX_EPOCH) / HECTONANOSECS_IN_SEC) as i64
34 }
35 
system_time_to_file_time(sys: &SYSTEMTIME) -> FILETIME36 fn system_time_to_file_time(sys: &SYSTEMTIME) -> FILETIME {
37     unsafe {
38         let mut ft = mem::zeroed();
39         SystemTimeToFileTime(sys, &mut ft);
40         ft
41     }
42 }
43 
tm_to_system_time(tm: &Tm) -> SYSTEMTIME44 fn tm_to_system_time(tm: &Tm) -> SYSTEMTIME {
45     let mut sys: SYSTEMTIME = unsafe { mem::zeroed() };
46     sys.wSecond = tm.tm_sec as WORD;
47     sys.wMinute = tm.tm_min as WORD;
48     sys.wHour = tm.tm_hour as WORD;
49     sys.wDay = tm.tm_mday as WORD;
50     sys.wDayOfWeek = tm.tm_wday as WORD;
51     sys.wMonth = (tm.tm_mon + 1) as WORD;
52     sys.wYear = (tm.tm_year + 1900) as WORD;
53     sys
54 }
55 
system_time_to_tm(sys: &SYSTEMTIME, tm: &mut Tm)56 fn system_time_to_tm(sys: &SYSTEMTIME, tm: &mut Tm) {
57     tm.tm_sec = sys.wSecond as i32;
58     tm.tm_min = sys.wMinute as i32;
59     tm.tm_hour = sys.wHour as i32;
60     tm.tm_mday = sys.wDay as i32;
61     tm.tm_wday = sys.wDayOfWeek as i32;
62     tm.tm_mon = (sys.wMonth - 1) as i32;
63     tm.tm_year = (sys.wYear - 1900) as i32;
64     tm.tm_yday = yday(tm.tm_year, tm.tm_mon + 1, tm.tm_mday);
65 
66     fn yday(year: i32, month: i32, day: i32) -> i32 {
67         let leap = if month > 2 {
68             if year % 4 == 0 {
69                 1
70             } else {
71                 2
72             }
73         } else {
74             0
75         };
76         let july = if month > 7 { 1 } else { 0 };
77 
78         (month - 1) * 30 + month / 2 + (day - 1) - leap + july
79     }
80 }
81 
82 macro_rules! call {
83     ($name:ident($($arg:expr),*)) => {
84         if $name($($arg),*) == 0 {
85             panic!(concat!(stringify!($name), " failed with: {}"),
86                     io::Error::last_os_error());
87         }
88     }
89 }
90 
time_to_local_tm(sec: i64, tm: &mut Tm)91 pub fn time_to_local_tm(sec: i64, tm: &mut Tm) {
92     let ft = time_to_file_time(sec);
93     unsafe {
94         let mut utc = mem::zeroed();
95         let mut local = mem::zeroed();
96         call!(FileTimeToSystemTime(&ft, &mut utc));
97         call!(SystemTimeToTzSpecificLocalTime(0 as *const _, &mut utc, &mut local));
98         system_time_to_tm(&local, tm);
99 
100         let local = system_time_to_file_time(&local);
101         let local_sec = file_time_to_unix_seconds(&local);
102 
103         let mut tz = mem::zeroed();
104         GetTimeZoneInformation(&mut tz);
105 
106         // SystemTimeToTzSpecificLocalTime already applied the biases so
107         // check if it non standard
108         tm.tm_utcoff = (local_sec - sec) as i32;
109         tm.tm_isdst = if tm.tm_utcoff == -60 * (tz.Bias + tz.StandardBias) { 0 } else { 1 };
110     }
111 }
112 
utc_tm_to_time(tm: &Tm) -> i64113 pub fn utc_tm_to_time(tm: &Tm) -> i64 {
114     unsafe {
115         let mut ft = mem::zeroed();
116         let sys_time = tm_to_system_time(tm);
117         call!(SystemTimeToFileTime(&sys_time, &mut ft));
118         file_time_to_unix_seconds(&ft)
119     }
120 }
121 
local_tm_to_time(tm: &Tm) -> i64122 pub fn local_tm_to_time(tm: &Tm) -> i64 {
123     unsafe {
124         let mut ft = mem::zeroed();
125         let mut utc = mem::zeroed();
126         let mut sys_time = tm_to_system_time(tm);
127         call!(TzSpecificLocalTimeToSystemTime(0 as *mut _, &mut sys_time, &mut utc));
128         call!(SystemTimeToFileTime(&utc, &mut ft));
129         file_time_to_unix_seconds(&ft)
130     }
131 }
132