1 // Copyright 2012-2013 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.
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.
11 //! Simple time handling.
13 #![crate_name = "time"]
14 #![deprecated = "use the http://github.com/rust-lang/time crate instead"]
17 #![crate_type = "rlib"]
18 #![crate_type = "dylib"]
19 #![doc(html_logo_url = "http://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
20 html_favicon_url = "http://www.rust-lang.org/favicon.ico",
21 html_root_url = "http://doc.rust-lang.org/nightly/",
22 html_playground_url = "http://play.rust-lang.org/")]
23 #![feature(phase, globs)]
25 #[cfg(test)] #[phase(plugin, link)] extern crate log;
27 extern crate serialize;
30 pub use self::ParseError::*;
35 use std::num::SignedInt;
36 use std::string::String;
37 use std::time::Duration;
39 static NSEC_PER_SEC: i32 = 1_000_000_000_i32;
46 pub fn rust_gmtime(sec: i64, nsec: i32, result: &mut Tm);
47 pub fn rust_localtime(sec: i64, nsec: i32, result: &mut Tm);
48 pub fn rust_timegm(tm: &Tm) -> i64;
49 pub fn rust_mktime(tm: &Tm) -> i64;
53 #[cfg(all(unix, not(target_os = "macos"), not(target_os = "ios")))]
55 use libc::{c_int, timespec};
57 // Apparently android provides this in some other library?
58 #[cfg(not(target_os = "android"))]
63 pub fn clock_gettime(clk_id: c_int, tp: *mut timespec) -> c_int;
67 #[cfg(any(target_os = "macos", target_os = "ios"))]
69 use libc::{timeval, timezone, c_int, mach_timebase_info};
72 pub fn gettimeofday(tp: *mut timeval, tzp: *mut timezone) -> c_int;
73 pub fn mach_absolute_time() -> u64;
74 pub fn mach_timebase_info(info: *mut mach_timebase_info) -> c_int;
78 /// A record specifying a time value in seconds and nanoseconds.
79 #[deriving(Clone, PartialEq, Eq, PartialOrd, Ord, Encodable, Decodable, Show)]
85 impl Copy for Timespec {}
88 * Timespec assumes that pre-epoch Timespecs have negative sec and positive
89 * nsec fields. Darwin's and Linux's struct timespec functions handle pre-
90 * epoch timestamps using a "two steps back, one step forward" representation,
91 * though the man pages do not actually document this. For example, the time
92 * -1.2 seconds before the epoch is represented by `Timespec { sec: -2_i64,
93 * nsec: 800_000_000_i32 }`.
96 pub fn new(sec: i64, nsec: i32) -> Timespec {
97 assert!(nsec >= 0 && nsec < NSEC_PER_SEC);
98 Timespec { sec: sec, nsec: nsec }
102 impl Add<Duration, Timespec> for Timespec {
103 fn add(&self, other: &Duration) -> Timespec {
104 let d_sec = other.num_seconds();
105 // It is safe to unwrap the nanoseconds, because there cannot be
106 // more than one second left, which fits in i64 and in i32.
107 let d_nsec = (*other - Duration::seconds(d_sec))
108 .num_nanoseconds().unwrap() as i32;
109 let mut sec = self.sec + d_sec;
110 let mut nsec = self.nsec + d_nsec;
111 if nsec >= NSEC_PER_SEC {
112 nsec -= NSEC_PER_SEC;
115 nsec += NSEC_PER_SEC;
118 Timespec::new(sec, nsec)
122 impl Sub<Timespec, Duration> for Timespec {
123 fn sub(&self, other: &Timespec) -> Duration {
124 let sec = self.sec - other.sec;
125 let nsec = self.nsec - other.nsec;
126 Duration::seconds(sec) + Duration::nanoseconds(nsec as i64)
130 /// Returns the current time as a `timespec` containing the seconds and
131 /// nanoseconds since 1970-01-01T00:00:00Z.
132 pub fn get_time() -> Timespec {
134 let (sec, nsec) = os_get_time();
135 return Timespec::new(sec, nsec);
139 unsafe fn os_get_time() -> (i64, i32) {
140 static NANOSECONDS_FROM_1601_TO_1970: u64 = 11644473600000000;
142 let mut time = libc::FILETIME {
146 libc::GetSystemTimeAsFileTime(&mut time);
148 // A FILETIME contains a 64-bit value representing the number of
149 // hectonanosecond (100-nanosecond) intervals since 1601-01-01T00:00:00Z.
150 // http://support.microsoft.com/kb/167296/en-us
151 let ns_since_1601 = ((time.dwHighDateTime as u64 << 32) |
152 (time.dwLowDateTime as u64 << 0)) / 10;
153 let ns_since_1970 = ns_since_1601 - NANOSECONDS_FROM_1601_TO_1970;
155 ((ns_since_1970 / 1000000) as i64,
156 ((ns_since_1970 % 1000000) * 1000) as i32)
159 #[cfg(any(target_os = "macos", target_os = "ios"))]
160 unsafe fn os_get_time() -> (i64, i32) {
162 let mut tv = libc::timeval { tv_sec: 0, tv_usec: 0 };
163 imp::gettimeofday(&mut tv, ptr::null_mut());
164 (tv.tv_sec as i64, tv.tv_usec * 1000)
167 #[cfg(not(any(target_os = "macos", target_os = "ios", windows)))]
168 unsafe fn os_get_time() -> (i64, i32) {
169 let mut tv = libc::timespec { tv_sec: 0, tv_nsec: 0 };
170 imp::clock_gettime(libc::CLOCK_REALTIME, &mut tv);
171 (tv.tv_sec as i64, tv.tv_nsec as i32)
176 /// Returns the current value of a high-resolution performance counter
177 /// in nanoseconds since an unspecified epoch.
178 pub fn precise_time_ns() -> u64 {
179 return os_precise_time_ns();
182 fn os_precise_time_ns() -> u64 {
183 let mut ticks_per_s = 0;
185 libc::QueryPerformanceFrequency(&mut ticks_per_s)
187 let ticks_per_s = if ticks_per_s == 0 {1} else {ticks_per_s};
190 libc::QueryPerformanceCounter(&mut ticks)
193 return (ticks as u64 * 1000000000) / (ticks_per_s as u64);
196 #[cfg(any(target_os = "macos", target_os = "ios"))]
197 fn os_precise_time_ns() -> u64 {
198 static mut TIMEBASE: libc::mach_timebase_info = libc::mach_timebase_info { numer: 0,
200 static ONCE: std::sync::Once = std::sync::ONCE_INIT;
203 imp::mach_timebase_info(&mut TIMEBASE);
205 let time = imp::mach_absolute_time();
206 time * TIMEBASE.numer as u64 / TIMEBASE.denom as u64
210 #[cfg(not(any(windows, target_os = "macos", target_os = "ios")))]
211 fn os_precise_time_ns() -> u64 {
212 let mut ts = libc::timespec { tv_sec: 0, tv_nsec: 0 };
214 imp::clock_gettime(libc::CLOCK_MONOTONIC, &mut ts);
216 return (ts.tv_sec as u64) * 1000000000 + (ts.tv_nsec as u64)
221 /// Returns the current value of a high-resolution performance counter
222 /// in seconds since an unspecified epoch.
223 pub fn precise_time_s() -> f64 {
224 return (precise_time_ns() as f64) / 1000000000.;
229 rustrt::rust_tzset();
233 /// Holds a calendar date and time broken down into its components (year, month, day, and so on),
234 /// also called a broken-down time value.
235 // FIXME: use c_int instead of i32?
237 #[deriving(Clone, PartialEq, Eq, Show)]
239 /// Seconds after the minute - [0, 60]
242 /// Minutes after the hour - [0, 59]
245 /// Hours after midnight - [0, 23]
248 /// Day of the month - [1, 31]
251 /// Months since January - [0, 11]
257 /// Days since Sunday - [0, 6]. 0 = Sunday, 1 = Monday, ..., 6 = Saturday.
260 /// Days since January 1 - [0, 365]
263 /// Daylight Saving Time flag.
265 /// This value is positive if Daylight Saving Time is in effect, zero if Daylight Saving Time
266 /// is not in effect, and negative if this information is not available.
269 /// Identifies the time zone that was used to compute this broken-down time value, including any
270 /// adjustment for Daylight Saving Time. This is the number of seconds east of UTC. For example,
271 /// for U.S. Pacific Daylight Time, the value is -7*60*60 = -25200.
274 /// Nanoseconds after the second - [0, 10<sup>9</sup> - 1]
280 pub fn empty_tm() -> Tm {
296 /// Returns the specified time in UTC
297 pub fn at_utc(clock: Timespec) -> Tm {
299 let Timespec { sec, nsec } = clock;
300 let mut tm = empty_tm();
301 rustrt::rust_gmtime(sec, nsec, &mut tm);
306 /// Returns the current time in UTC
307 pub fn now_utc() -> Tm {
311 /// Returns the specified time in the local timezone
312 pub fn at(clock: Timespec) -> Tm {
314 let Timespec { sec, nsec } = clock;
315 let mut tm = empty_tm();
316 rustrt::rust_localtime(sec, nsec, &mut tm);
321 /// Returns the current time in the local timezone
327 /// Convert time to the seconds from January 1, 1970
328 pub fn to_timespec(&self) -> Timespec {
330 let sec = match self.tm_gmtoff {
331 0_i32 => rustrt::rust_timegm(self),
332 _ => rustrt::rust_mktime(self)
335 Timespec::new(sec, self.tm_nsec)
339 /// Convert time to the local timezone
340 pub fn to_local(&self) -> Tm {
341 at(self.to_timespec())
344 /// Convert time to the UTC
345 pub fn to_utc(&self) -> Tm {
346 at_utc(self.to_timespec())
349 /// Returns a TmFmt that outputs according to the `asctime` format in ISO
350 /// C, in the local timezone.
352 /// Example: "Thu Jan 1 00:00:00 1970"
353 pub fn ctime(&self) -> TmFmt {
360 /// Returns a TmFmt that outputs according to the `asctime` format in ISO
363 /// Example: "Thu Jan 1 00:00:00 1970"
364 pub fn asctime(&self) -> TmFmt {
367 format: FmtStr("%c"),
371 /// Formats the time according to the format string.
372 pub fn strftime<'a>(&'a self, format: &'a str) -> Result<TmFmt<'a>, ParseError> {
373 validate_format(TmFmt {
375 format: FmtStr(format),
379 /// Returns a TmFmt that outputs according to RFC 822.
381 /// local: "Thu, 22 Mar 2012 07:53:18 PST"
382 /// utc: "Thu, 22 Mar 2012 14:53:18 GMT"
383 pub fn rfc822(&self) -> TmFmt {
384 if self.tm_gmtoff == 0_i32 {
387 format: FmtStr("%a, %d %b %Y %T GMT"),
392 format: FmtStr("%a, %d %b %Y %T %Z"),
397 /// Returns a TmFmt that outputs according to RFC 822 with Zulu time.
399 /// local: "Thu, 22 Mar 2012 07:53:18 -0700"
400 /// utc: "Thu, 22 Mar 2012 14:53:18 -0000"
401 pub fn rfc822z(&self) -> TmFmt {
404 format: FmtStr("%a, %d %b %Y %T %z"),
408 /// Returns a TmFmt that outputs according to RFC 3339. RFC 3339 is
409 /// compatible with ISO 8601.
411 /// local: "2012-02-22T07:53:18-07:00"
412 /// utc: "2012-02-22T14:53:18Z"
413 pub fn rfc3339<'a>(&'a self) -> TmFmt {
421 #[deriving(PartialEq)]
422 pub enum ParseError {
434 MissingFormatConverter,
435 InvalidFormatSpecifier(char),
436 UnexpectedCharacter(char, char),
439 impl Copy for ParseError {}
441 impl Show for ParseError {
442 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
444 InvalidSecond => write!(f, "Invalid second."),
445 InvalidMinute => write!(f, "Invalid minute."),
446 InvalidHour => write!(f, "Invalid hour."),
447 InvalidDay => write!(f, "Invalid day."),
448 InvalidMonth => write!(f, "Invalid month."),
449 InvalidYear => write!(f, "Invalid year."),
450 InvalidDayOfWeek => write!(f, "Invalid day of the week."),
451 InvalidDayOfMonth => write!(f, "Invalid day of the month."),
452 InvalidDayOfYear => write!(f, "Invalid day of the year."),
453 InvalidZoneOffset => write!(f, "Invalid zone offset."),
454 InvalidTime => write!(f, "Invalid time."),
455 MissingFormatConverter => write!(f, "Missing format converter after `%`"),
456 InvalidFormatSpecifier(ch) => write!(f, "Invalid format specifier: %{}", ch),
457 UnexpectedCharacter(a, b) => write!(f, "Expected: {}, found: {}.", a, b),
462 /// A wrapper around a `Tm` and format string that implements Show.
463 pub struct TmFmt<'a> {
474 fn validate_format<'a>(fmt: TmFmt<'a>) -> Result<TmFmt<'a>, ParseError> {
476 match (fmt.tm.tm_wday, fmt.tm.tm_mon) {
477 (0...6, 0...11) => (),
478 (_wday, 0...11) => return Err(InvalidDayOfWeek),
479 (0...6, _mon) => return Err(InvalidMonth),
480 _ => return Err(InvalidDay)
484 let mut chars = s.chars();
534 Some(c) => return Err(InvalidFormatSpecifier(c)),
535 None => return Err(MissingFormatConverter),
548 impl<'a> fmt::Show for TmFmt<'a> {
549 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
550 fn days_in_year(year: int) -> i32 {
551 if (year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0)) {
552 366 /* Days in a leap year */
554 365 /* Days in a non-leap year */
558 fn iso_week_days(yday: i32, wday: i32) -> int {
559 /* The number of days from the first day of the first ISO week of this
560 * year to the year day YDAY with week day WDAY.
561 * ISO weeks start on Monday. The first ISO week has the year's first
563 * YDAY may be as small as yday_minimum.
565 let yday: int = yday as int;
566 let wday: int = wday as int;
567 let iso_week_start_wday: int = 1; /* Monday */
568 let iso_week1_wday: int = 4; /* Thursday */
569 let yday_minimum: int = 366;
570 /* Add enough to the first operand of % to make it nonnegative. */
571 let big_enough_multiple_of_7: int = (yday_minimum / 7 + 2) * 7;
573 yday - (yday - wday + iso_week1_wday + big_enough_multiple_of_7) % 7
574 + iso_week1_wday - iso_week_start_wday
577 fn iso_week(fmt: &mut fmt::Formatter, ch:char, tm: &Tm) -> fmt::Result {
578 let mut year: int = tm.tm_year as int + 1900;
579 let mut days: int = iso_week_days (tm.tm_yday, tm.tm_wday);
582 /* This ISO week belongs to the previous year. */
584 days = iso_week_days (tm.tm_yday + (days_in_year(year)), tm.tm_wday);
586 let d: int = iso_week_days (tm.tm_yday - (days_in_year(year)),
589 /* This ISO week belongs to the next year. */
596 'G' => write!(fmt, "{}", year),
597 'g' => write!(fmt, "{:02}", (year % 100 + 100) % 100),
598 'V' => write!(fmt, "{:02}", days / 7 + 1),
603 fn parse_type(fmt: &mut fmt::Formatter, ch: char, tm: &Tm) -> fmt::Result {
608 'A' => match tm.tm_wday as int {
618 'a' => match tm.tm_wday as int {
628 'B' => match tm.tm_mon as int {
643 'b' | 'h' => match tm.tm_mon as int {
658 'C' => return write!(fmt, "{:02}", (tm.tm_year as int + 1900) / 100),
660 try!(parse_type(fmt, 'a', tm));
662 try!(parse_type(fmt, 'b', tm));
664 try!(parse_type(fmt, 'e', tm));
666 try!(parse_type(fmt, 'T', tm));
668 return parse_type(fmt, 'Y', tm);
671 try!(parse_type(fmt, 'm', tm));
673 try!(parse_type(fmt, 'd', tm));
675 return parse_type(fmt, 'y', tm);
677 'd' => return write!(fmt, "{:02}", tm.tm_mday),
678 'e' => return write!(fmt, "{:2}", tm.tm_mday),
679 'f' => return write!(fmt, "{:09}", tm.tm_nsec),
681 try!(parse_type(fmt, 'Y', tm));
683 try!(parse_type(fmt, 'm', tm));
685 return parse_type(fmt, 'd', tm);
687 'G' => return iso_week(fmt, 'G', tm),
688 'g' => return iso_week(fmt, 'g', tm),
689 'H' => return write!(fmt, "{:02}", tm.tm_hour),
691 let mut h = tm.tm_hour;
693 if h > 12 { h -= 12 }
694 return write!(fmt, "{:02}", h)
696 'j' => return write!(fmt, "{:03}", tm.tm_yday + 1),
697 'k' => return write!(fmt, "{:2}", tm.tm_hour),
699 let mut h = tm.tm_hour;
701 if h > 12 { h -= 12 }
702 return write!(fmt, "{:2}", h)
704 'M' => return write!(fmt, "{:02}", tm.tm_min),
705 'm' => return write!(fmt, "{:02}", tm.tm_mon + 1),
707 'P' => if (tm.tm_hour as int) < 12 { "am" } else { "pm" },
708 'p' => if (tm.tm_hour as int) < 12 { "AM" } else { "PM" },
710 try!(parse_type(fmt, 'H', tm));
712 return parse_type(fmt, 'M', tm);
715 try!(parse_type(fmt, 'I', tm));
717 try!(parse_type(fmt, 'M', tm));
719 try!(parse_type(fmt, 'S', tm));
721 return parse_type(fmt, 'p', tm);
723 'S' => return write!(fmt, "{:02}", tm.tm_sec),
724 's' => return write!(fmt, "{}", tm.to_timespec().sec),
726 try!(parse_type(fmt, 'H', tm));
728 try!(parse_type(fmt, 'M', tm));
730 return parse_type(fmt, 'S', tm);
733 'U' => return write!(fmt, "{:02}", (tm.tm_yday - tm.tm_wday + 7) / 7),
735 let i = tm.tm_wday as int;
736 return (if i == 0 { 7 } else { i }).fmt(fmt);
738 'V' => return iso_week(fmt, 'V', tm),
740 try!(parse_type(fmt, 'e', tm));
742 try!(parse_type(fmt, 'b', tm));
744 return parse_type(fmt, 'Y', tm);
747 return write!(fmt, "{:02}",
748 (tm.tm_yday - (tm.tm_wday - 1 + 7) % 7 + 7) / 7)
750 'w' => return (tm.tm_wday as int).fmt(fmt),
751 'Y' => return (tm.tm_year as int + 1900).fmt(fmt),
752 'y' => return write!(fmt, "{:02}", (tm.tm_year as int + 1900) % 100),
753 'Z' => if tm.tm_gmtoff == 0_i32 { "GMT"} else { "" }, // FIXME (#2350): support locale
755 let sign = if tm.tm_gmtoff > 0_i32 { '+' } else { '-' };
756 let mut m = tm.tm_gmtoff.abs() / 60_i32;
759 return write!(fmt, "{}{:02}{:02}", sign, h, m);
761 '+' => return tm.rfc3339().fmt(fmt),
769 let mut chars = s.chars();
773 // we've already validated that % always precedes another char
774 try!(parse_type(fmt, chars.next().unwrap(), self.tm));
776 Some(ch) => try!(ch.fmt(fmt)),
784 self.tm.to_local().asctime().fmt(fmt)
787 if self.tm.tm_gmtoff == 0_i32 {
790 format: FmtStr("%Y-%m-%dT%H:%M:%SZ"),
795 format: FmtStr("%Y-%m-%dT%H:%M:%S"),
797 let sign = if self.tm.tm_gmtoff > 0_i32 { '+' } else { '-' };
798 let mut m = self.tm.tm_gmtoff.abs() / 60_i32;
801 write!(fmt, "{}{}{:02}:{:02}", s, sign, h as int, m as int)
808 /// Parses the time from the string according to the format string.
809 pub fn strptime(s: &str, format: &str) -> Result<Tm, ParseError> {
810 fn match_str(s: &str, pos: uint, needle: &str) -> bool {
811 s.slice_from(pos).starts_with(needle)
814 fn match_strs(ss: &str, pos: uint, strs: &[(&str, i32)])
815 -> Option<(i32, uint)> {
816 for &(needle, value) in strs.iter() {
817 if match_str(ss, pos, needle) {
818 return Some((value, pos + needle.len()));
825 fn match_digits(ss: &str, pos: uint, digits: uint, ws: bool)
826 -> Option<(i32, uint)> {
829 let mut value = 0_i32;
836 let range = ss.char_range_at(pos);
841 value = value * 10_i32 + (range.ch as i32 - '0' as i32);
852 fn match_fractional_seconds(ss: &str, pos: uint) -> (i32, uint) {
854 let mut value = 0_i32;
855 let mut multiplier = NSEC_PER_SEC / 10;
862 let range = ss.char_range_at(pos);
867 // This will drop digits after the nanoseconds place
868 let digit = range.ch as i32 - '0' as i32;
869 value += digit * multiplier;
879 fn match_digits_in_range(ss: &str, pos: uint, digits: uint, ws: bool,
880 min: i32, max: i32) -> Option<(i32, uint)> {
881 match match_digits(ss, pos, digits, ws) {
882 Some((val, pos)) if val >= min && val <= max => {
889 fn parse_char(s: &str, pos: uint, c: char) -> Result<uint, ParseError> {
890 let range = s.char_range_at(pos);
895 Err(UnexpectedCharacter(c, range.ch))
899 fn parse_type(s: &str, pos: uint, ch: char, tm: &mut Tm)
900 -> Result<uint, ParseError> {
902 'A' => match match_strs(s, pos, &[
906 ("Wednesday", 3_i32),
911 Some(item) => { let (v, pos) = item; tm.tm_wday = v; Ok(pos) }
912 None => Err(InvalidDay)
914 'a' => match match_strs(s, pos, &[
923 Some(item) => { let (v, pos) = item; tm.tm_wday = v; Ok(pos) }
924 None => Err(InvalidDay)
926 'B' => match match_strs(s, pos, &[
935 ("September", 8_i32),
937 ("November", 10_i32),
940 Some(item) => { let (v, pos) = item; tm.tm_mon = v; Ok(pos) }
941 None => Err(InvalidMonth)
943 'b' | 'h' => match match_strs(s, pos, &[
957 Some(item) => { let (v, pos) = item; tm.tm_mon = v; Ok(pos) }
958 None => Err(InvalidMonth)
960 'C' => match match_digits_in_range(s, pos, 2u, false, 0_i32,
964 tm.tm_year += (v * 100_i32) - 1900_i32;
967 None => Err(InvalidYear)
970 parse_type(s, pos, 'a', &mut *tm)
971 .and_then(|pos| parse_char(s, pos, ' '))
972 .and_then(|pos| parse_type(s, pos, 'b', &mut *tm))
973 .and_then(|pos| parse_char(s, pos, ' '))
974 .and_then(|pos| parse_type(s, pos, 'e', &mut *tm))
975 .and_then(|pos| parse_char(s, pos, ' '))
976 .and_then(|pos| parse_type(s, pos, 'T', &mut *tm))
977 .and_then(|pos| parse_char(s, pos, ' '))
978 .and_then(|pos| parse_type(s, pos, 'Y', &mut *tm))
981 parse_type(s, pos, 'm', &mut *tm)
982 .and_then(|pos| parse_char(s, pos, '/'))
983 .and_then(|pos| parse_type(s, pos, 'd', &mut *tm))
984 .and_then(|pos| parse_char(s, pos, '/'))
985 .and_then(|pos| parse_type(s, pos, 'y', &mut *tm))
987 'd' => match match_digits_in_range(s, pos, 2u, false, 1_i32,
989 Some(item) => { let (v, pos) = item; tm.tm_mday = v; Ok(pos) }
990 None => Err(InvalidDayOfMonth)
992 'e' => match match_digits_in_range(s, pos, 2u, true, 1_i32,
994 Some(item) => { let (v, pos) = item; tm.tm_mday = v; Ok(pos) }
995 None => Err(InvalidDayOfMonth)
998 let (val, pos) = match_fractional_seconds(s, pos);
1003 parse_type(s, pos, 'Y', &mut *tm)
1004 .and_then(|pos| parse_char(s, pos, '-'))
1005 .and_then(|pos| parse_type(s, pos, 'm', &mut *tm))
1006 .and_then(|pos| parse_char(s, pos, '-'))
1007 .and_then(|pos| parse_type(s, pos, 'd', &mut *tm))
1010 match match_digits_in_range(s, pos, 2u, false, 0_i32, 23_i32) {
1011 Some(item) => { let (v, pos) = item; tm.tm_hour = v; Ok(pos) }
1012 None => Err(InvalidHour)
1016 match match_digits_in_range(s, pos, 2u, false, 1_i32, 12_i32) {
1018 let (v, pos) = item;
1019 tm.tm_hour = if v == 12_i32 { 0_i32 } else { v };
1022 None => Err(InvalidHour)
1026 match match_digits_in_range(s, pos, 3u, false, 1_i32, 366_i32) {
1028 let (v, pos) = item;
1029 tm.tm_yday = v - 1_i32;
1032 None => Err(InvalidDayOfYear)
1036 match match_digits_in_range(s, pos, 2u, true, 0_i32, 23_i32) {
1037 Some(item) => { let (v, pos) = item; tm.tm_hour = v; Ok(pos) }
1038 None => Err(InvalidHour)
1042 match match_digits_in_range(s, pos, 2u, true, 1_i32, 12_i32) {
1044 let (v, pos) = item;
1045 tm.tm_hour = if v == 12_i32 { 0_i32 } else { v };
1048 None => Err(InvalidHour)
1052 match match_digits_in_range(s, pos, 2u, false, 0_i32, 59_i32) {
1053 Some(item) => { let (v, pos) = item; tm.tm_min = v; Ok(pos) }
1054 None => Err(InvalidMinute)
1058 match match_digits_in_range(s, pos, 2u, false, 1_i32, 12_i32) {
1060 let (v, pos) = item;
1061 tm.tm_mon = v - 1_i32;
1064 None => Err(InvalidMonth)
1067 'n' => parse_char(s, pos, '\n'),
1068 'P' => match match_strs(s, pos,
1069 &[("am", 0_i32), ("pm", 12_i32)]) {
1071 Some(item) => { let (v, pos) = item; tm.tm_hour += v; Ok(pos) }
1072 None => Err(InvalidHour)
1074 'p' => match match_strs(s, pos,
1075 &[("AM", 0_i32), ("PM", 12_i32)]) {
1077 Some(item) => { let (v, pos) = item; tm.tm_hour += v; Ok(pos) }
1078 None => Err(InvalidHour)
1081 parse_type(s, pos, 'H', &mut *tm)
1082 .and_then(|pos| parse_char(s, pos, ':'))
1083 .and_then(|pos| parse_type(s, pos, 'M', &mut *tm))
1086 parse_type(s, pos, 'I', &mut *tm)
1087 .and_then(|pos| parse_char(s, pos, ':'))
1088 .and_then(|pos| parse_type(s, pos, 'M', &mut *tm))
1089 .and_then(|pos| parse_char(s, pos, ':'))
1090 .and_then(|pos| parse_type(s, pos, 'S', &mut *tm))
1091 .and_then(|pos| parse_char(s, pos, ' '))
1092 .and_then(|pos| parse_type(s, pos, 'p', &mut *tm))
1095 match match_digits_in_range(s, pos, 2u, false, 0_i32, 60_i32) {
1097 let (v, pos) = item;
1101 None => Err(InvalidSecond)
1106 parse_type(s, pos, 'H', &mut *tm)
1107 .and_then(|pos| parse_char(s, pos, ':'))
1108 .and_then(|pos| parse_type(s, pos, 'M', &mut *tm))
1109 .and_then(|pos| parse_char(s, pos, ':'))
1110 .and_then(|pos| parse_type(s, pos, 'S', &mut *tm))
1112 't' => parse_char(s, pos, '\t'),
1114 match match_digits_in_range(s, pos, 1u, false, 1_i32, 7_i32) {
1116 let (v, pos) = item;
1117 tm.tm_wday = if v == 7 { 0 } else { v };
1120 None => Err(InvalidDayOfWeek)
1124 parse_type(s, pos, 'e', &mut *tm)
1125 .and_then(|pos| parse_char(s, pos, '-'))
1126 .and_then(|pos| parse_type(s, pos, 'b', &mut *tm))
1127 .and_then(|pos| parse_char(s, pos, '-'))
1128 .and_then(|pos| parse_type(s, pos, 'Y', &mut *tm))
1132 match match_digits_in_range(s, pos, 1u, false, 0_i32, 6_i32) {
1133 Some(item) => { let (v, pos) = item; tm.tm_wday = v; Ok(pos) }
1134 None => Err(InvalidDayOfWeek)
1138 match match_digits(s, pos, 4u, false) {
1140 let (v, pos) = item;
1141 tm.tm_year = v - 1900_i32;
1144 None => Err(InvalidYear)
1148 match match_digits_in_range(s, pos, 2u, false, 0_i32, 99_i32) {
1150 let (v, pos) = item;
1154 None => Err(InvalidYear)
1158 if match_str(s, pos, "UTC") || match_str(s, pos, "GMT") {
1159 tm.tm_gmtoff = 0_i32;
1162 // It's odd, but to maintain compatibility with c's
1163 // strptime we ignore the timezone.
1167 let range = s.char_range_at(pos);
1169 if range.ch == ' ' { break; }
1176 let range = s.char_range_at(pos);
1178 if range.ch == '+' || range.ch == '-' {
1179 match match_digits(s, range.next, 4u, false) {
1181 let (v, pos) = item;
1183 tm.tm_gmtoff = 0_i32;
1188 None => Err(InvalidZoneOffset)
1191 Err(InvalidZoneOffset)
1194 '%' => parse_char(s, pos, '%'),
1195 ch => Err(InvalidFormatSpecifier(ch))
1199 let mut rdr: &[u8] = format.as_bytes();
1215 let mut result = Err(InvalidTime);
1218 let range = s.char_range_at(pos);
1220 let next = range.next;
1223 let c = match (&mut rdr).read(&mut buf) {
1224 Ok(..) => buf[0] as char,
1229 let ch = match (&mut rdr).read(&mut buf) {
1230 Ok(..) => buf[0] as char,
1233 match parse_type(s, pos, ch, &mut tm) {
1234 Ok(next) => pos = next,
1235 Err(e) => { result = Err(e); break; }
1239 if c != ch { break }
1245 if pos == len && (&mut rdr).is_empty() {
1249 tm_hour: tm.tm_hour,
1250 tm_mday: tm.tm_mday,
1252 tm_year: tm.tm_year,
1253 tm_wday: tm.tm_wday,
1254 tm_yday: tm.tm_yday,
1255 tm_isdst: tm.tm_isdst,
1256 tm_gmtoff: tm.tm_gmtoff,
1257 tm_nsec: tm.tm_nsec,
1262 /// Formats the time according to the format string.
1263 pub fn strftime(format: &str, tm: &Tm) -> Result<String, ParseError> {
1264 tm.strftime(format).map(|fmt| fmt.to_string())
1270 use super::{Timespec, InvalidTime, InvalidYear, get_time, precise_time_ns,
1271 precise_time_s, tzset, at_utc, at, strptime, MissingFormatConverter,
1272 InvalidFormatSpecifier};
1275 use std::result::Result::{Err, Ok};
1276 use std::time::Duration;
1277 use self::test::Bencher;
1280 fn set_time_zone() {
1282 // Windows crt doesn't see any environment variable set by
1283 // `SetEnvironmentVariable`, which `os::setenv` internally uses.
1284 // It is why we use `putenv` here.
1286 fn _putenv(envstring: *const libc::c_char) -> libc::c_int;
1290 // Windows does not understand "America/Los_Angeles".
1291 // PST+08 may look wrong, but not! "PST" indicates
1292 // the name of timezone. "+08" means UTC = local + 08.
1293 "TZ=PST+08".with_c_str(|env| {
1299 #[cfg(not(windows))]
1300 fn set_time_zone() {
1302 os::setenv("TZ", "America/Los_Angeles");
1306 fn test_get_time() {
1307 static SOME_RECENT_DATE: i64 = 1325376000i64; // 2012-01-01T00:00:00Z
1308 static SOME_FUTURE_DATE: i64 = 1577836800i64; // 2020-01-01T00:00:00Z
1310 let tv1 = get_time();
1311 debug!("tv1={} sec + {} nsec", tv1.sec as uint, tv1.nsec as uint);
1313 assert!(tv1.sec > SOME_RECENT_DATE);
1314 assert!(tv1.nsec < 1000000000i32);
1316 let tv2 = get_time();
1317 debug!("tv2={} sec + {} nsec", tv2.sec as uint, tv2.nsec as uint);
1319 assert!(tv2.sec >= tv1.sec);
1320 assert!(tv2.sec < SOME_FUTURE_DATE);
1321 assert!(tv2.nsec < 1000000000i32);
1322 if tv2.sec == tv1.sec {
1323 assert!(tv2.nsec >= tv1.nsec);
1327 fn test_precise_time() {
1328 let s0 = precise_time_s();
1329 debug!("s0={} sec", f64::to_str_digits(s0, 9u));
1332 let ns0 = precise_time_ns();
1333 let ns1 = precise_time_ns();
1334 debug!("ns0={} ns", ns0);
1335 debug!("ns1={} ns", ns1);
1336 assert!(ns1 >= ns0);
1338 let ns2 = precise_time_ns();
1339 debug!("ns2={} ns", ns2);
1340 assert!(ns2 >= ns1);
1346 let time = Timespec::new(1234567890, 54321);
1347 let utc = at_utc(time);
1349 assert_eq!(utc.tm_sec, 30_i32);
1350 assert_eq!(utc.tm_min, 31_i32);
1351 assert_eq!(utc.tm_hour, 23_i32);
1352 assert_eq!(utc.tm_mday, 13_i32);
1353 assert_eq!(utc.tm_mon, 1_i32);
1354 assert_eq!(utc.tm_year, 109_i32);
1355 assert_eq!(utc.tm_wday, 5_i32);
1356 assert_eq!(utc.tm_yday, 43_i32);
1357 assert_eq!(utc.tm_isdst, 0_i32);
1358 assert_eq!(utc.tm_gmtoff, 0_i32);
1359 assert_eq!(utc.tm_nsec, 54321_i32);
1365 let time = Timespec::new(1234567890, 54321);
1366 let local = at(time);
1368 debug!("time_at: {}", local);
1370 assert_eq!(local.tm_sec, 30_i32);
1371 assert_eq!(local.tm_min, 31_i32);
1372 assert_eq!(local.tm_hour, 15_i32);
1373 assert_eq!(local.tm_mday, 13_i32);
1374 assert_eq!(local.tm_mon, 1_i32);
1375 assert_eq!(local.tm_year, 109_i32);
1376 assert_eq!(local.tm_wday, 5_i32);
1377 assert_eq!(local.tm_yday, 43_i32);
1378 assert_eq!(local.tm_isdst, 0_i32);
1379 assert_eq!(local.tm_gmtoff, -28800_i32);
1380 assert_eq!(local.tm_nsec, 54321_i32);
1383 fn test_to_timespec() {
1386 let time = Timespec::new(1234567890, 54321);
1387 let utc = at_utc(time);
1389 assert_eq!(utc.to_timespec(), time);
1390 assert_eq!(utc.to_local().to_timespec(), time);
1393 fn test_conversions() {
1396 let time = Timespec::new(1234567890, 54321);
1397 let utc = at_utc(time);
1398 let local = at(time);
1400 assert!(local.to_local() == local);
1401 assert!(local.to_utc() == utc);
1402 assert!(local.to_utc().to_local() == local);
1403 assert!(utc.to_utc() == utc);
1404 assert!(utc.to_local() == local);
1405 assert!(utc.to_local().to_utc() == utc);
1408 fn test_strptime() {
1411 match strptime("", "") {
1413 assert!(tm.tm_sec == 0_i32);
1414 assert!(tm.tm_min == 0_i32);
1415 assert!(tm.tm_hour == 0_i32);
1416 assert!(tm.tm_mday == 0_i32);
1417 assert!(tm.tm_mon == 0_i32);
1418 assert!(tm.tm_year == 0_i32);
1419 assert!(tm.tm_wday == 0_i32);
1420 assert!(tm.tm_isdst == 0_i32);
1421 assert!(tm.tm_gmtoff == 0_i32);
1422 assert!(tm.tm_nsec == 0_i32);
1427 let format = "%a %b %e %T.%f %Y";
1428 assert_eq!(strptime("", format), Err(InvalidTime));
1429 assert!(strptime("Fri Feb 13 15:31:30", format)
1430 == Err(InvalidTime));
1432 match strptime("Fri Feb 13 15:31:30.01234 2009", format) {
1433 Err(e) => panic!(e),
1435 assert!(tm.tm_sec == 30_i32);
1436 assert!(tm.tm_min == 31_i32);
1437 assert!(tm.tm_hour == 15_i32);
1438 assert!(tm.tm_mday == 13_i32);
1439 assert!(tm.tm_mon == 1_i32);
1440 assert!(tm.tm_year == 109_i32);
1441 assert!(tm.tm_wday == 5_i32);
1442 assert!(tm.tm_yday == 0_i32);
1443 assert!(tm.tm_isdst == 0_i32);
1444 assert!(tm.tm_gmtoff == 0_i32);
1445 assert!(tm.tm_nsec == 12340000_i32);
1449 fn test(s: &str, format: &str) -> bool {
1450 match strptime(s, format) {
1452 tm.strftime(format).unwrap().to_string() == s.to_string()
1459 "Sunday".to_string(),
1460 "Monday".to_string(),
1461 "Tuesday".to_string(),
1462 "Wednesday".to_string(),
1463 "Thursday".to_string(),
1464 "Friday".to_string(),
1465 "Saturday".to_string()
1467 for day in days.iter() {
1468 assert!(test(day.as_slice(), "%A"));
1480 for day in days.iter() {
1481 assert!(test(day.as_slice(), "%a"));
1485 "January".to_string(),
1486 "February".to_string(),
1487 "March".to_string(),
1488 "April".to_string(),
1492 "August".to_string(),
1493 "September".to_string(),
1494 "October".to_string(),
1495 "November".to_string(),
1496 "December".to_string()
1498 for day in months.iter() {
1499 assert!(test(day.as_slice(), "%B"));
1516 for day in months.iter() {
1517 assert!(test(day.as_slice(), "%b"));
1520 assert!(test("19", "%C"));
1521 assert!(test("Fri Feb 13 23:31:30 2009", "%c"));
1522 assert!(test("02/13/09", "%D"));
1523 assert!(test("03", "%d"));
1524 assert!(test("13", "%d"));
1525 assert!(test(" 3", "%e"));
1526 assert!(test("13", "%e"));
1527 assert!(test("2009-02-13", "%F"));
1528 assert!(test("03", "%H"));
1529 assert!(test("13", "%H"));
1530 assert!(test("03", "%I")); // FIXME (#2350): flesh out
1531 assert!(test("11", "%I")); // FIXME (#2350): flesh out
1532 assert!(test("044", "%j"));
1533 assert!(test(" 3", "%k"));
1534 assert!(test("13", "%k"));
1535 assert!(test(" 1", "%l"));
1536 assert!(test("11", "%l"));
1537 assert!(test("03", "%M"));
1538 assert!(test("13", "%M"));
1539 assert!(test("\n", "%n"));
1540 assert!(test("am", "%P"));
1541 assert!(test("pm", "%P"));
1542 assert!(test("AM", "%p"));
1543 assert!(test("PM", "%p"));
1544 assert!(test("23:31", "%R"));
1545 assert!(test("11:31:30 AM", "%r"));
1546 assert!(test("11:31:30 PM", "%r"));
1547 assert!(test("03", "%S"));
1548 assert!(test("13", "%S"));
1549 assert!(test("15:31:30", "%T"));
1550 assert!(test("\t", "%t"));
1551 assert!(test("1", "%u"));
1552 assert!(test("7", "%u"));
1553 assert!(test("13-Feb-2009", "%v"));
1554 assert!(test("0", "%w"));
1555 assert!(test("6", "%w"));
1556 assert!(test("2009", "%Y"));
1557 assert!(test("09", "%y"));
1558 assert!(strptime("-0000", "%z").unwrap().tm_gmtoff ==
1560 assert!(strptime("-0800", "%z").unwrap().tm_gmtoff ==
1562 assert!(test("%", "%%"));
1565 assert_eq!(strptime("360", "%Y-%m-%d"), Err(InvalidYear));
1571 let time = Timespec::new(1234567890, 54321);
1572 let utc = at_utc(time);
1573 let local = at(time);
1575 debug!("test_ctime: {} {}", utc.asctime(), local.asctime());
1577 assert_eq!(utc.asctime().to_string(), "Fri Feb 13 23:31:30 2009");
1578 assert_eq!(local.asctime().to_string(), "Fri Feb 13 15:31:30 2009");
1584 let time = Timespec::new(1234567890, 54321);
1585 let utc = at_utc(time);
1586 let local = at(time);
1588 debug!("test_ctime: {} {}", utc.ctime(), local.ctime());
1590 assert_eq!(utc.ctime().to_string(), "Fri Feb 13 15:31:30 2009");
1591 assert_eq!(local.ctime().to_string(), "Fri Feb 13 15:31:30 2009");
1594 fn test_strftime() {
1597 let time = Timespec::new(1234567890, 54321);
1598 let utc = at_utc(time);
1599 let local = at(time);
1601 assert_eq!(local.strftime("").unwrap().to_string(), "");
1602 assert_eq!(local.strftime("%A").unwrap().to_string(), "Friday");
1603 assert_eq!(local.strftime("%a").unwrap().to_string(), "Fri");
1604 assert_eq!(local.strftime("%B").unwrap().to_string(), "February");
1605 assert_eq!(local.strftime("%b").unwrap().to_string(), "Feb");
1606 assert_eq!(local.strftime("%C").unwrap().to_string(), "20");
1607 assert_eq!(local.strftime("%c").unwrap().to_string(),
1608 "Fri Feb 13 15:31:30 2009");
1609 assert_eq!(local.strftime("%D").unwrap().to_string(), "02/13/09");
1610 assert_eq!(local.strftime("%d").unwrap().to_string(), "13");
1611 assert_eq!(local.strftime("%e").unwrap().to_string(), "13");
1612 assert_eq!(local.strftime("%F").unwrap().to_string(), "2009-02-13");
1613 assert_eq!(local.strftime("%f").unwrap().to_string(), "000054321");
1614 assert_eq!(local.strftime("%G").unwrap().to_string(), "2009");
1615 assert_eq!(local.strftime("%g").unwrap().to_string(), "09");
1616 assert_eq!(local.strftime("%H").unwrap().to_string(), "15");
1617 assert_eq!(local.strftime("%h").unwrap().to_string(), "Feb");
1618 assert_eq!(local.strftime("%I").unwrap().to_string(), "03");
1619 assert_eq!(local.strftime("%j").unwrap().to_string(), "044");
1620 assert_eq!(local.strftime("%k").unwrap().to_string(), "15");
1621 assert_eq!(local.strftime("%l").unwrap().to_string(), " 3");
1622 assert_eq!(local.strftime("%M").unwrap().to_string(), "31");
1623 assert_eq!(local.strftime("%m").unwrap().to_string(), "02");
1624 assert_eq!(local.strftime("%n").unwrap().to_string(), "\n");
1625 assert_eq!(local.strftime("%P").unwrap().to_string(), "pm");
1626 assert_eq!(local.strftime("%p").unwrap().to_string(), "PM");
1627 assert_eq!(local.strftime("%R").unwrap().to_string(), "15:31");
1628 assert_eq!(local.strftime("%r").unwrap().to_string(), "03:31:30 PM");
1629 assert_eq!(local.strftime("%S").unwrap().to_string(), "30");
1630 assert_eq!(local.strftime("%s").unwrap().to_string(), "1234567890");
1631 assert_eq!(local.strftime("%T").unwrap().to_string(), "15:31:30");
1632 assert_eq!(local.strftime("%t").unwrap().to_string(), "\t");
1633 assert_eq!(local.strftime("%U").unwrap().to_string(), "06");
1634 assert_eq!(local.strftime("%u").unwrap().to_string(), "5");
1635 assert_eq!(local.strftime("%V").unwrap().to_string(), "07");
1636 assert_eq!(local.strftime("%v").unwrap().to_string(), "13-Feb-2009");
1637 assert_eq!(local.strftime("%W").unwrap().to_string(), "06");
1638 assert_eq!(local.strftime("%w").unwrap().to_string(), "5");
1639 // FIXME (#2350): support locale
1640 assert_eq!(local.strftime("%X").unwrap().to_string(), "15:31:30");
1641 // FIXME (#2350): support locale
1642 assert_eq!(local.strftime("%x").unwrap().to_string(), "02/13/09");
1643 assert_eq!(local.strftime("%Y").unwrap().to_string(), "2009");
1644 assert_eq!(local.strftime("%y").unwrap().to_string(), "09");
1645 // FIXME (#2350): support locale
1646 assert_eq!(local.strftime("%Z").unwrap().to_string(), "");
1647 assert_eq!(local.strftime("%z").unwrap().to_string(), "-0800");
1648 assert_eq!(local.strftime("%+").unwrap().to_string(),
1649 "2009-02-13T15:31:30-08:00");
1650 assert_eq!(local.strftime("%%").unwrap().to_string(), "%");
1652 let invalid_specifiers = ["%E", "%J", "%K", "%L", "%N", "%O", "%o", "%Q", "%q"];
1653 for &sp in invalid_specifiers.iter() {
1654 assert_eq!(local.strftime(sp).unwrap_err(), InvalidFormatSpecifier(sp.char_at(1)));
1656 assert_eq!(local.strftime("%").unwrap_err(), MissingFormatConverter);
1657 assert_eq!(local.strftime("%A %").unwrap_err(), MissingFormatConverter);
1659 assert_eq!(local.asctime().to_string(), "Fri Feb 13 15:31:30 2009");
1660 assert_eq!(local.ctime().to_string(), "Fri Feb 13 15:31:30 2009");
1661 assert_eq!(local.rfc822z().to_string(), "Fri, 13 Feb 2009 15:31:30 -0800");
1662 assert_eq!(local.rfc3339().to_string(), "2009-02-13T15:31:30-08:00");
1664 assert_eq!(utc.asctime().to_string(), "Fri Feb 13 23:31:30 2009");
1665 assert_eq!(utc.ctime().to_string(), "Fri Feb 13 15:31:30 2009");
1666 assert_eq!(utc.rfc822().to_string(), "Fri, 13 Feb 2009 23:31:30 GMT");
1667 assert_eq!(utc.rfc822z().to_string(), "Fri, 13 Feb 2009 23:31:30 -0000");
1668 assert_eq!(utc.rfc3339().to_string(), "2009-02-13T23:31:30Z");
1671 fn test_timespec_eq_ord() {
1672 let a = &Timespec::new(-2, 1);
1673 let b = &Timespec::new(-1, 2);
1674 let c = &Timespec::new(1, 2);
1675 let d = &Timespec::new(2, 1);
1676 let e = &Timespec::new(2, 1);
1702 fn test_timespec_add() {
1703 let a = Timespec::new(1, 2);
1704 let b = Duration::seconds(2) + Duration::nanoseconds(3);
1706 assert_eq!(c.sec, 3);
1707 assert_eq!(c.nsec, 5);
1709 let p = Timespec::new(1, super::NSEC_PER_SEC - 2);
1710 let q = Duration::seconds(2) + Duration::nanoseconds(2);
1712 assert_eq!(r.sec, 4);
1713 assert_eq!(r.nsec, 0);
1715 let u = Timespec::new(1, super::NSEC_PER_SEC - 2);
1716 let v = Duration::seconds(2) + Duration::nanoseconds(3);
1718 assert_eq!(w.sec, 4);
1719 assert_eq!(w.nsec, 1);
1721 let k = Timespec::new(1, 0);
1722 let l = Duration::nanoseconds(-1);
1724 assert_eq!(m.sec, 0);
1725 assert_eq!(m.nsec, 999_999_999);
1728 fn test_timespec_sub() {
1729 let a = Timespec::new(2, 3);
1730 let b = Timespec::new(1, 2);
1732 assert_eq!(c.num_nanoseconds(), Some(super::NSEC_PER_SEC as i64 + 1));
1734 let p = Timespec::new(2, 0);
1735 let q = Timespec::new(1, 2);
1737 assert_eq!(r.num_nanoseconds(), Some(super::NSEC_PER_SEC as i64 - 2));
1739 let u = Timespec::new(1, 2);
1740 let v = Timespec::new(2, 3);
1742 assert_eq!(w.num_nanoseconds(), Some(-super::NSEC_PER_SEC as i64 - 1));
1746 #[cfg_attr(target_os = "android", ignore)] // FIXME #10958
1748 // The tests race on tzset. So instead of having many independent
1749 // tests, we will just call the functions now.
1751 test_precise_time();
1760 test_timespec_eq_ord();
1761 test_timespec_add();
1762 test_timespec_sub();
1766 fn bench_precise_time_ns(b: &mut Bencher) {
1767 b.iter(|| precise_time_ns())