• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
time_to_tm(ts: i64, tm: &mut Tm)13 fn time_to_tm(ts: i64, tm: &mut Tm) {
14     let leapyear = |year| -> bool { year % 4 == 0 && (year % 100 != 0 || year % 400 == 0) };
15 
16     static YTAB: [[i64; 12]; 2] = [
17         [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
18         [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
19     ];
20 
21     let mut year = 1970;
22 
23     let dayclock = ts % 86400;
24     let mut dayno = ts / 86400;
25 
26     tm.tm_sec = (dayclock % 60) as i32;
27     tm.tm_min = ((dayclock % 3600) / 60) as i32;
28     tm.tm_hour = (dayclock / 3600) as i32;
29     tm.tm_wday = ((dayno + 4) % 7) as i32;
30     loop {
31         let yearsize = if leapyear(year) { 366 } else { 365 };
32         if dayno >= yearsize {
33             dayno -= yearsize;
34             year += 1;
35         } else {
36             break;
37         }
38     }
39     tm.tm_year = (year - 1900) as i32;
40     tm.tm_yday = dayno as i32;
41     let mut mon = 0;
42     while dayno >= YTAB[if leapyear(year) { 1 } else { 0 }][mon] {
43         dayno -= YTAB[if leapyear(year) { 1 } else { 0 }][mon];
44         mon += 1;
45     }
46     tm.tm_mon = mon as i32;
47     tm.tm_mday = dayno as i32 + 1;
48     tm.tm_isdst = 0;
49 }
50 
tm_to_time(tm: &Tm) -> i6451 fn tm_to_time(tm: &Tm) -> i64 {
52     let mut y = tm.tm_year as i64 + 1900;
53     let mut m = tm.tm_mon as i64 + 1;
54     if m <= 2 {
55         y -= 1;
56         m += 12;
57     }
58     let d = tm.tm_mday as i64;
59     let h = tm.tm_hour as i64;
60     let mi = tm.tm_min as i64;
61     let s = tm.tm_sec as i64;
62     (365 * y + y / 4 - y / 100 + y / 400 + 3 * (m + 1) / 5 + 30 * m + d - 719561) * 86400
63         + 3600 * h
64         + 60 * mi
65         + s
66 }
67 
time_to_local_tm(sec: i64, tm: &mut Tm)68 pub fn time_to_local_tm(sec: i64, tm: &mut Tm) {
69     // FIXME: Add timezone logic
70     time_to_tm(sec, tm);
71 }
72 
utc_tm_to_time(tm: &Tm) -> i6473 pub fn utc_tm_to_time(tm: &Tm) -> i64 {
74     tm_to_time(tm)
75 }
76 
local_tm_to_time(tm: &Tm) -> i6477 pub fn local_tm_to_time(tm: &Tm) -> i64 {
78     // FIXME: Add timezone logic
79     tm_to_time(tm)
80 }
81