• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // This is a part of Chrono.
2 // See README.md and LICENSE.txt for details.
3 
4 //! The local (system) time zone.
5 
6 #[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind")))]
7 use sys::{self, Timespec};
8 
9 use super::fixed::FixedOffset;
10 use super::{LocalResult, TimeZone};
11 #[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind")))]
12 use naive::NaiveTime;
13 use naive::{NaiveDate, NaiveDateTime};
14 use {Date, DateTime};
15 #[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind")))]
16 use {Datelike, Timelike};
17 
18 /// Converts a `time::Tm` struct into the timezone-aware `DateTime`.
19 /// This assumes that `time` is working correctly, i.e. any error is fatal.
20 #[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind")))]
tm_to_datetime(mut tm: sys::Tm) -> DateTime<Local>21 fn tm_to_datetime(mut tm: sys::Tm) -> DateTime<Local> {
22     if tm.tm_sec >= 60 {
23         tm.tm_nsec += (tm.tm_sec - 59) * 1_000_000_000;
24         tm.tm_sec = 59;
25     }
26 
27     #[cfg(not(windows))]
28     fn tm_to_naive_date(tm: &sys::Tm) -> NaiveDate {
29         // from_yo is more efficient than from_ymd (since it's the internal representation).
30         NaiveDate::from_yo(tm.tm_year + 1900, tm.tm_yday as u32 + 1)
31     }
32 
33     #[cfg(windows)]
34     fn tm_to_naive_date(tm: &sys::Tm) -> NaiveDate {
35         // ...but tm_yday is broken in Windows (issue #85)
36         NaiveDate::from_ymd(tm.tm_year + 1900, tm.tm_mon as u32 + 1, tm.tm_mday as u32)
37     }
38 
39     let date = tm_to_naive_date(&tm);
40     let time = NaiveTime::from_hms_nano(
41         tm.tm_hour as u32,
42         tm.tm_min as u32,
43         tm.tm_sec as u32,
44         tm.tm_nsec as u32,
45     );
46     let offset = FixedOffset::east(tm.tm_utcoff);
47     DateTime::from_utc(date.and_time(time) - offset, offset)
48 }
49 
50 /// Converts a local `NaiveDateTime` to the `time::Timespec`.
51 #[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind")))]
datetime_to_timespec(d: &NaiveDateTime, local: bool) -> sys::Timespec52 fn datetime_to_timespec(d: &NaiveDateTime, local: bool) -> sys::Timespec {
53     // well, this exploits an undocumented `Tm::to_timespec` behavior
54     // to get the exact function we want (either `timegm` or `mktime`).
55     // the number 1 is arbitrary but should be non-zero to trigger `mktime`.
56     let tm_utcoff = if local { 1 } else { 0 };
57 
58     let tm = sys::Tm {
59         tm_sec: d.second() as i32,
60         tm_min: d.minute() as i32,
61         tm_hour: d.hour() as i32,
62         tm_mday: d.day() as i32,
63         tm_mon: d.month0() as i32, // yes, C is that strange...
64         tm_year: d.year() - 1900,  // this doesn't underflow, we know that d is `NaiveDateTime`.
65         tm_wday: 0,                // to_local ignores this
66         tm_yday: 0,                // and this
67         tm_isdst: -1,
68         tm_utcoff: tm_utcoff,
69         // do not set this, OS APIs are heavily inconsistent in terms of leap second handling
70         tm_nsec: 0,
71     };
72 
73     tm.to_timespec()
74 }
75 
76 /// The local timescale. This is implemented via the standard `time` crate.
77 ///
78 /// Using the [`TimeZone`](./trait.TimeZone.html) methods
79 /// on the Local struct is the preferred way to construct `DateTime<Local>`
80 /// instances.
81 ///
82 /// # Example
83 ///
84 /// ~~~~
85 /// use chrono::{Local, DateTime, TimeZone};
86 ///
87 /// let dt: DateTime<Local> = Local::now();
88 /// let dt: DateTime<Local> = Local.timestamp(0, 0);
89 /// ~~~~
90 #[derive(Copy, Clone, Debug)]
91 pub struct Local;
92 
93 impl Local {
94     /// Returns a `Date` which corresponds to the current date.
today() -> Date<Local>95     pub fn today() -> Date<Local> {
96         Local::now().date()
97     }
98 
99     /// Returns a `DateTime` which corresponds to the current date.
100     #[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind")))]
now() -> DateTime<Local>101     pub fn now() -> DateTime<Local> {
102         tm_to_datetime(Timespec::now().local())
103     }
104 
105     /// Returns a `DateTime` which corresponds to the current date.
106     #[cfg(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind"))]
now() -> DateTime<Local>107     pub fn now() -> DateTime<Local> {
108         use super::Utc;
109         let now: DateTime<Utc> = super::Utc::now();
110 
111         // Workaround missing timezone logic in `time` crate
112         let offset = FixedOffset::west((js_sys::Date::new_0().get_timezone_offset() as i32) * 60);
113         DateTime::from_utc(now.naive_utc(), offset)
114     }
115 }
116 
117 impl TimeZone for Local {
118     type Offset = FixedOffset;
119 
from_offset(_offset: &FixedOffset) -> Local120     fn from_offset(_offset: &FixedOffset) -> Local {
121         Local
122     }
123 
124     // they are easier to define in terms of the finished date and time unlike other offsets
offset_from_local_date(&self, local: &NaiveDate) -> LocalResult<FixedOffset>125     fn offset_from_local_date(&self, local: &NaiveDate) -> LocalResult<FixedOffset> {
126         self.from_local_date(local).map(|date| *date.offset())
127     }
128 
offset_from_local_datetime(&self, local: &NaiveDateTime) -> LocalResult<FixedOffset>129     fn offset_from_local_datetime(&self, local: &NaiveDateTime) -> LocalResult<FixedOffset> {
130         self.from_local_datetime(local).map(|datetime| *datetime.offset())
131     }
132 
offset_from_utc_date(&self, utc: &NaiveDate) -> FixedOffset133     fn offset_from_utc_date(&self, utc: &NaiveDate) -> FixedOffset {
134         *self.from_utc_date(utc).offset()
135     }
136 
offset_from_utc_datetime(&self, utc: &NaiveDateTime) -> FixedOffset137     fn offset_from_utc_datetime(&self, utc: &NaiveDateTime) -> FixedOffset {
138         *self.from_utc_datetime(utc).offset()
139     }
140 
141     // override them for avoiding redundant works
from_local_date(&self, local: &NaiveDate) -> LocalResult<Date<Local>>142     fn from_local_date(&self, local: &NaiveDate) -> LocalResult<Date<Local>> {
143         // this sounds very strange, but required for keeping `TimeZone::ymd` sane.
144         // in the other words, we use the offset at the local midnight
145         // but keep the actual date unaltered (much like `FixedOffset`).
146         let midnight = self.from_local_datetime(&local.and_hms(0, 0, 0));
147         midnight.map(|datetime| Date::from_utc(*local, *datetime.offset()))
148     }
149 
150     #[cfg(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind"))]
from_local_datetime(&self, local: &NaiveDateTime) -> LocalResult<DateTime<Local>>151     fn from_local_datetime(&self, local: &NaiveDateTime) -> LocalResult<DateTime<Local>> {
152         let mut local = local.clone();
153         // Get the offset from the js runtime
154         let offset = FixedOffset::west((js_sys::Date::new_0().get_timezone_offset() as i32) * 60);
155         local -= ::Duration::seconds(offset.local_minus_utc() as i64);
156         LocalResult::Single(DateTime::from_utc(local, offset))
157     }
158 
159     #[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind")))]
from_local_datetime(&self, local: &NaiveDateTime) -> LocalResult<DateTime<Local>>160     fn from_local_datetime(&self, local: &NaiveDateTime) -> LocalResult<DateTime<Local>> {
161         let timespec = datetime_to_timespec(local, true);
162 
163         // datetime_to_timespec completely ignores leap seconds, so we need to adjust for them
164         let mut tm = timespec.local();
165         assert_eq!(tm.tm_nsec, 0);
166         tm.tm_nsec = local.nanosecond() as i32;
167 
168         LocalResult::Single(tm_to_datetime(tm))
169     }
170 
from_utc_date(&self, utc: &NaiveDate) -> Date<Local>171     fn from_utc_date(&self, utc: &NaiveDate) -> Date<Local> {
172         let midnight = self.from_utc_datetime(&utc.and_hms(0, 0, 0));
173         Date::from_utc(*utc, *midnight.offset())
174     }
175 
176     #[cfg(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind"))]
from_utc_datetime(&self, utc: &NaiveDateTime) -> DateTime<Local>177     fn from_utc_datetime(&self, utc: &NaiveDateTime) -> DateTime<Local> {
178         // Get the offset from the js runtime
179         let offset = FixedOffset::west((js_sys::Date::new_0().get_timezone_offset() as i32) * 60);
180         DateTime::from_utc(*utc, offset)
181     }
182 
183     #[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind")))]
from_utc_datetime(&self, utc: &NaiveDateTime) -> DateTime<Local>184     fn from_utc_datetime(&self, utc: &NaiveDateTime) -> DateTime<Local> {
185         let timespec = datetime_to_timespec(utc, false);
186 
187         // datetime_to_timespec completely ignores leap seconds, so we need to adjust for them
188         let mut tm = timespec.local();
189         assert_eq!(tm.tm_nsec, 0);
190         tm.tm_nsec = utc.nanosecond() as i32;
191 
192         tm_to_datetime(tm)
193     }
194 }
195 
196 #[cfg(test)]
197 mod tests {
198     use super::Local;
199     use offset::TimeZone;
200     use Datelike;
201 
202     #[test]
test_local_date_sanity_check()203     fn test_local_date_sanity_check() {
204         // issue #27
205         assert_eq!(Local.ymd(2999, 12, 28).day(), 28);
206     }
207 
208     #[test]
test_leap_second()209     fn test_leap_second() {
210         // issue #123
211         let today = Local::today();
212 
213         let dt = today.and_hms_milli(1, 2, 59, 1000);
214         let timestr = dt.time().to_string();
215         // the OS API may or may not support the leap second,
216         // but there are only two sensible options.
217         assert!(timestr == "01:02:60" || timestr == "01:03:00", "unexpected timestr {:?}", timestr);
218 
219         let dt = today.and_hms_milli(1, 2, 3, 1234);
220         let timestr = dt.time().to_string();
221         assert!(
222             timestr == "01:02:03.234" || timestr == "01:02:04.234",
223             "unexpected timestr {:?}",
224             timestr
225         );
226     }
227 }
228