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 #[allow(missing_doc)];
19 static NSEC_PER_SEC: i32 = 1_000_000_000_i32;
26 pub fn get_time(sec: &mut i64, nsec: &mut i32);
27 pub fn precise_time_ns(ns: &mut u64);
29 pub fn rust_gmtime(sec: i64, nsec: i32, result: &mut Tm);
30 pub fn rust_localtime(sec: i64, nsec: i32, result: &mut Tm);
31 pub fn rust_timegm(tm: &Tm) -> i64;
32 pub fn rust_mktime(tm: &Tm) -> i64;
36 /// A record specifying a time value in seconds and nanoseconds.
37 #[deriving(Eq, Encodable, Decodable)]
38 pub struct Timespec { sec: i64, nsec: i32 }
41 * Timespec assumes that pre-epoch Timespecs have negative sec and positive
42 * nsec fields. Darwin's and Linux's struct timespec functions handle pre-
43 * epoch timestamps using a "two steps back, one step forward" representation,
44 * though the man pages do not actually document this. For example, the time
45 * -1.2 seconds before the epoch is represented by `Timespec { sec: -2_i64,
46 * nsec: 800_000_000_i32 }`.
49 pub fn new(sec: i64, nsec: i32) -> Timespec {
50 assert!(nsec >= 0 && nsec < NSEC_PER_SEC);
51 Timespec { sec: sec, nsec: nsec }
55 impl Ord for Timespec {
56 fn lt(&self, other: &Timespec) -> bool {
57 self.sec < other.sec ||
58 (self.sec == other.sec && self.nsec < other.nsec)
60 fn le(&self, other: &Timespec) -> bool { !other.lt(self) }
61 fn ge(&self, other: &Timespec) -> bool { !self.lt(other) }
62 fn gt(&self, other: &Timespec) -> bool { !self.le(other) }
66 * Returns the current time as a `timespec` containing the seconds and
67 * nanoseconds since 1970-01-01T00:00:00Z.
69 pub fn get_time() -> Timespec {
73 rustrt::get_time(&mut sec, &mut nsec);
74 return Timespec::new(sec, nsec);
80 * Returns the current value of a high-resolution performance counter
81 * in nanoseconds since an unspecified epoch.
83 pub fn precise_time_ns() -> u64 {
86 rustrt::precise_time_ns(&mut ns);
93 * Returns the current value of a high-resolution performance counter
94 * in seconds since an unspecified epoch.
96 pub fn precise_time_s() -> float {
97 return (precise_time_ns() as float) / 1000000000.;
102 rustrt::rust_tzset();
106 #[deriving(Eq, Encodable, Decodable)]
108 tm_sec: i32, // seconds after the minute ~[0-60]
109 tm_min: i32, // minutes after the hour ~[0-59]
110 tm_hour: i32, // hours after midnight ~[0-23]
111 tm_mday: i32, // days of the month ~[1-31]
112 tm_mon: i32, // months since January ~[0-11]
113 tm_year: i32, // years since 1900
114 tm_wday: i32, // days since Sunday ~[0-6]
115 tm_yday: i32, // days since January 1 ~[0-365]
116 tm_isdst: i32, // Daylight Savings Time flag
117 tm_gmtoff: i32, // offset from UTC in seconds
118 tm_zone: ~str, // timezone abbreviation
119 tm_nsec: i32, // nanoseconds
122 pub fn empty_tm() -> Tm {
139 /// Returns the specified time in UTC
140 pub fn at_utc(clock: Timespec) -> Tm {
142 let Timespec { sec, nsec } = clock;
143 let mut tm = empty_tm();
144 rustrt::rust_gmtime(sec, nsec, &mut tm);
149 /// Returns the current time in UTC
150 pub fn now_utc() -> Tm {
154 /// Returns the specified time in the local timezone
155 pub fn at(clock: Timespec) -> Tm {
157 let Timespec { sec, nsec } = clock;
158 let mut tm = empty_tm();
159 rustrt::rust_localtime(sec, nsec, &mut tm);
164 /// Returns the current time in the local timezone
169 /// Parses the time from the string according to the format string.
170 pub fn strptime(s: &str, format: &str) -> Result<Tm, ~str> {
171 do_strptime(s, format)
174 /// Formats the time according to the format string.
175 pub fn strftime(format: &str, tm: &Tm) -> ~str {
176 do_strftime(format, tm)
180 /// Convert time to the seconds from January 1, 1970
181 pub fn to_timespec(&self) -> Timespec {
183 let sec = match self.tm_gmtoff {
184 0_i32 => rustrt::rust_timegm(self),
185 _ => rustrt::rust_mktime(self)
188 Timespec::new(sec, self.tm_nsec)
192 /// Convert time to the local timezone
193 pub fn to_local(&self) -> Tm {
194 at(self.to_timespec())
197 /// Convert time to the UTC
198 pub fn to_utc(&self) -> Tm {
199 at_utc(self.to_timespec())
203 * Return a string of the current time in the form
204 * "Thu Jan 1 00:00:00 1970".
206 pub fn ctime(&self) -> ~str { self.strftime("%c") }
208 /// Formats the time according to the format string.
209 pub fn strftime(&self, format: &str) -> ~str {
210 strftime(format, self)
214 * Returns a time string formatted according to RFC 822.
216 * local: "Thu, 22 Mar 2012 07:53:18 PST"
217 * utc: "Thu, 22 Mar 2012 14:53:18 UTC"
219 pub fn rfc822(&self) -> ~str {
220 if self.tm_gmtoff == 0_i32 {
221 self.strftime("%a, %d %b %Y %T GMT")
223 self.strftime("%a, %d %b %Y %T %Z")
228 * Returns a time string formatted according to RFC 822 with Zulu time.
230 * local: "Thu, 22 Mar 2012 07:53:18 -0700"
231 * utc: "Thu, 22 Mar 2012 14:53:18 -0000"
233 pub fn rfc822z(&self) -> ~str {
234 self.strftime("%a, %d %b %Y %T %z")
238 * Returns a time string formatted according to ISO 8601.
240 * local: "2012-02-22T07:53:18-07:00"
241 * utc: "2012-02-22T14:53:18Z"
243 pub fn rfc3339(&self) -> ~str {
244 if self.tm_gmtoff == 0_i32 {
245 self.strftime("%Y-%m-%dT%H:%M:%SZ")
247 let s = self.strftime("%Y-%m-%dT%H:%M:%S");
248 let sign = if self.tm_gmtoff > 0_i32 { '+' } else { '-' };
249 let mut m = num::abs(self.tm_gmtoff) / 60_i32;
252 s + fmt!("%c%02d:%02d", sign, h as int, m as int)
257 priv fn do_strptime(s: &str, format: &str) -> Result<Tm, ~str> {
258 fn match_str(s: &str, pos: uint, needle: &str) -> bool {
260 for ch in needle.byte_iter() {
269 fn match_strs(ss: &str, pos: uint, strs: &[(~str, i32)])
270 -> Option<(i32, uint)> {
272 let len = strs.len();
274 match strs[i] { // can't use let due to let-pattern bugs
275 (ref needle, value) => {
276 if match_str(ss, pos, *needle) {
277 return Some((value, pos + needle.len()));
287 fn match_digits(ss: &str, pos: uint, digits: uint, ws: bool)
288 -> Option<(i32, uint)> {
290 let mut value = 0_i32;
294 let range = ss.char_range_at(pos);
299 value = value * 10_i32 + (range.ch as i32 - '0' as i32);
310 fn match_digits_in_range(ss: &str, pos: uint, digits: uint, ws: bool,
311 min: i32, max: i32) -> Option<(i32, uint)> {
312 match match_digits(ss, pos, digits, ws) {
313 Some((val, pos)) if val >= min && val <= max => {
320 fn parse_char(s: &str, pos: uint, c: char) -> Result<uint, ~str> {
321 let range = s.char_range_at(pos);
326 Err(fmt!("Expected %?, found %?",
328 str::from_char(range.ch)))
332 fn parse_type(s: &str, pos: uint, ch: char, tm: &mut Tm)
333 -> Result<uint, ~str> {
335 'A' => match match_strs(s, pos, [
339 (~"Wednesday", 3_i32),
340 (~"Thursday", 4_i32),
344 Some(item) => { let (v, pos) = item; tm.tm_wday = v; Ok(pos) }
345 None => Err(~"Invalid day")
347 'a' => match match_strs(s, pos, [
356 Some(item) => { let (v, pos) = item; tm.tm_wday = v; Ok(pos) }
357 None => Err(~"Invalid day")
359 'B' => match match_strs(s, pos, [
361 (~"February", 1_i32),
368 (~"September", 8_i32),
370 (~"November", 10_i32),
371 (~"December", 11_i32)
373 Some(item) => { let (v, pos) = item; tm.tm_mon = v; Ok(pos) }
374 None => Err(~"Invalid month")
376 'b' | 'h' => match match_strs(s, pos, [
390 Some(item) => { let (v, pos) = item; tm.tm_mon = v; Ok(pos) }
391 None => Err(~"Invalid month")
393 'C' => match match_digits_in_range(s, pos, 2u, false, 0_i32,
397 tm.tm_year += (v * 100_i32) - 1900_i32;
400 None => Err(~"Invalid year")
403 parse_type(s, pos, 'a', &mut *tm)
404 .chain(|pos| parse_char(s, pos, ' '))
405 .chain(|pos| parse_type(s, pos, 'b', &mut *tm))
406 .chain(|pos| parse_char(s, pos, ' '))
407 .chain(|pos| parse_type(s, pos, 'e', &mut *tm))
408 .chain(|pos| parse_char(s, pos, ' '))
409 .chain(|pos| parse_type(s, pos, 'T', &mut *tm))
410 .chain(|pos| parse_char(s, pos, ' '))
411 .chain(|pos| parse_type(s, pos, 'Y', &mut *tm))
414 parse_type(s, pos, 'm', &mut *tm)
415 .chain(|pos| parse_char(s, pos, '/'))
416 .chain(|pos| parse_type(s, pos, 'd', &mut *tm))
417 .chain(|pos| parse_char(s, pos, '/'))
418 .chain(|pos| parse_type(s, pos, 'y', &mut *tm))
420 'd' => match match_digits_in_range(s, pos, 2u, false, 1_i32,
422 Some(item) => { let (v, pos) = item; tm.tm_mday = v; Ok(pos) }
423 None => Err(~"Invalid day of the month")
425 'e' => match match_digits_in_range(s, pos, 2u, true, 1_i32,
427 Some(item) => { let (v, pos) = item; tm.tm_mday = v; Ok(pos) }
428 None => Err(~"Invalid day of the month")
431 parse_type(s, pos, 'Y', &mut *tm)
432 .chain(|pos| parse_char(s, pos, '-'))
433 .chain(|pos| parse_type(s, pos, 'm', &mut *tm))
434 .chain(|pos| parse_char(s, pos, '-'))
435 .chain(|pos| parse_type(s, pos, 'd', &mut *tm))
438 match match_digits_in_range(s, pos, 2u, false, 0_i32, 23_i32) {
439 Some(item) => { let (v, pos) = item; tm.tm_hour = v; Ok(pos) }
440 None => Err(~"Invalid hour")
444 match match_digits_in_range(s, pos, 2u, false, 1_i32, 12_i32) {
447 tm.tm_hour = if v == 12_i32 { 0_i32 } else { v };
450 None => Err(~"Invalid hour")
454 match match_digits_in_range(s, pos, 3u, false, 1_i32, 366_i32) {
457 tm.tm_yday = v - 1_i32;
460 None => Err(~"Invalid day of year")
464 match match_digits_in_range(s, pos, 2u, true, 0_i32, 23_i32) {
465 Some(item) => { let (v, pos) = item; tm.tm_hour = v; Ok(pos) }
466 None => Err(~"Invalid hour")
470 match match_digits_in_range(s, pos, 2u, true, 1_i32, 12_i32) {
473 tm.tm_hour = if v == 12_i32 { 0_i32 } else { v };
476 None => Err(~"Invalid hour")
480 match match_digits_in_range(s, pos, 2u, false, 0_i32, 59_i32) {
481 Some(item) => { let (v, pos) = item; tm.tm_min = v; Ok(pos) }
482 None => Err(~"Invalid minute")
486 match match_digits_in_range(s, pos, 2u, false, 1_i32, 12_i32) {
489 tm.tm_mon = v - 1_i32;
492 None => Err(~"Invalid month")
495 'n' => parse_char(s, pos, '\n'),
496 'P' => match match_strs(s, pos,
497 [(~"am", 0_i32), (~"pm", 12_i32)]) {
499 Some(item) => { let (v, pos) = item; tm.tm_hour += v; Ok(pos) }
500 None => Err(~"Invalid hour")
502 'p' => match match_strs(s, pos,
503 [(~"AM", 0_i32), (~"PM", 12_i32)]) {
505 Some(item) => { let (v, pos) = item; tm.tm_hour += v; Ok(pos) }
506 None => Err(~"Invalid hour")
509 parse_type(s, pos, 'H', &mut *tm)
510 .chain(|pos| parse_char(s, pos, ':'))
511 .chain(|pos| parse_type(s, pos, 'M', &mut *tm))
514 parse_type(s, pos, 'I', &mut *tm)
515 .chain(|pos| parse_char(s, pos, ':'))
516 .chain(|pos| parse_type(s, pos, 'M', &mut *tm))
517 .chain(|pos| parse_char(s, pos, ':'))
518 .chain(|pos| parse_type(s, pos, 'S', &mut *tm))
519 .chain(|pos| parse_char(s, pos, ' '))
520 .chain(|pos| parse_type(s, pos, 'p', &mut *tm))
523 match match_digits_in_range(s, pos, 2u, false, 0_i32, 60_i32) {
529 None => Err(~"Invalid second")
534 parse_type(s, pos, 'H', &mut *tm)
535 .chain(|pos| parse_char(s, pos, ':'))
536 .chain(|pos| parse_type(s, pos, 'M', &mut *tm))
537 .chain(|pos| parse_char(s, pos, ':'))
538 .chain(|pos| parse_type(s, pos, 'S', &mut *tm))
540 't' => parse_char(s, pos, '\t'),
542 match match_digits_in_range(s, pos, 1u, false, 1_i32, 7_i32) {
545 tm.tm_wday = if v == 7 { 0 } else { v };
548 None => Err(~"Invalid day of week")
552 parse_type(s, pos, 'e', &mut *tm)
553 .chain(|pos| parse_char(s, pos, '-'))
554 .chain(|pos| parse_type(s, pos, 'b', &mut *tm))
555 .chain(|pos| parse_char(s, pos, '-'))
556 .chain(|pos| parse_type(s, pos, 'Y', &mut *tm))
560 match match_digits_in_range(s, pos, 1u, false, 0_i32, 6_i32) {
561 Some(item) => { let (v, pos) = item; tm.tm_wday = v; Ok(pos) }
562 None => Err(~"Invalid day of week")
568 match match_digits(s, pos, 4u, false) {
571 tm.tm_year = v - 1900_i32;
574 None => Err(~"Invalid year")
578 match match_digits_in_range(s, pos, 2u, false, 0_i32, 99_i32) {
584 None => Err(~"Invalid year")
588 if match_str(s, pos, "UTC") || match_str(s, pos, "GMT") {
589 tm.tm_gmtoff = 0_i32;
593 // It's odd, but to maintain compatibility with c's
594 // strptime we ignore the timezone.
598 let range = s.char_range_at(pos);
600 if range.ch == ' ' { break; }
607 let range = s.char_range_at(pos);
609 if range.ch == '+' || range.ch == '-' {
610 match match_digits(s, range.next, 4u, false) {
614 tm.tm_gmtoff = 0_i32;
620 None => Err(~"Invalid zone offset")
623 Err(~"Invalid zone offset")
626 '%' => parse_char(s, pos, '%'),
628 Err(fmt!("unknown formatting type: %?", str::from_char(ch)))
633 do io::with_str_reader(format) |rdr| {
650 let mut result = Err(~"Invalid time");
652 while !rdr.eof() && pos < len {
653 let range = s.char_range_at(pos);
655 let next = range.next;
657 match rdr.read_char() {
659 match parse_type(s, pos, rdr.read_char(), &mut tm) {
660 Ok(next) => pos = next,
661 Err(e) => { result = Err(e); break; }
671 if pos == len && rdr.eof() {
681 tm_isdst: tm.tm_isdst,
682 tm_gmtoff: tm.tm_gmtoff,
683 tm_zone: tm.tm_zone.clone(),
690 priv fn do_strftime(format: &str, tm: &Tm) -> ~str {
691 fn parse_type(ch: char, tm: &Tm) -> ~str {
692 //FIXME (#2350): Implement missing types.
693 let die = || fmt!("strftime: can't understand this format %c ", ch);
695 'A' => match tm.tm_wday as int {
705 'a' => match tm.tm_wday as int {
715 'B' => match tm.tm_mon as int {
730 'b' | 'h' => match tm.tm_mon as int {
745 'C' => fmt!("%02d", (tm.tm_year as int + 1900) / 100),
747 fmt!("%s %s %s %s %s",
760 'd' => fmt!("%02d", tm.tm_mday as int),
761 'e' => fmt!("%2d", tm.tm_mday as int),
770 'H' => fmt!("%02d", tm.tm_hour as int),
772 let mut h = tm.tm_hour as int;
774 if h > 12 { h -= 12 }
777 'j' => fmt!("%03d", tm.tm_yday as int + 1),
778 'k' => fmt!("%2d", tm.tm_hour as int),
780 let mut h = tm.tm_hour as int;
782 if h > 12 { h -= 12 }
785 'M' => fmt!("%02d", tm.tm_min as int),
786 'm' => fmt!("%02d", tm.tm_mon as int + 1),
788 'P' => if (tm.tm_hour as int) < 12 { ~"am" } else { ~"pm" },
789 'p' => if (tm.tm_hour as int) < 12 { ~"AM" } else { ~"PM" },
802 'S' => fmt!("%02d", tm.tm_sec as int),
803 's' => fmt!("%d", tm.to_timespec().sec as int),
813 let i = tm.tm_wday as int;
814 int::to_str(if i == 0 { 7 } else { i })
824 'w' => int::to_str(tm.tm_wday as int),
827 'Y' => int::to_str(tm.tm_year as int + 1900),
828 'y' => fmt!("%02d", (tm.tm_year as int + 1900) % 100),
829 'Z' => tm.tm_zone.clone(),
831 let sign = if tm.tm_gmtoff > 0_i32 { '+' } else { '-' };
832 let mut m = num::abs(tm.tm_gmtoff) / 60_i32;
835 fmt!("%c%02d%02d", sign, h as int, m as int)
845 do io::with_str_reader(format) |rdr| {
847 match rdr.read_char() {
848 '%' => buf.push_str(parse_type(rdr.read_char(), tm)),
849 ch => buf.push_char(ch)
863 use std::result::{Err, Ok};
866 static SOME_RECENT_DATE: i64 = 1325376000i64; // 2012-01-01T00:00:00Z
867 static SOME_FUTURE_DATE: i64 = 1577836800i64; // 2020-01-01T00:00:00Z
869 let tv1 = get_time();
870 debug!("tv1=%? sec + %? nsec", tv1.sec as uint, tv1.nsec as uint);
872 assert!(tv1.sec > SOME_RECENT_DATE);
873 assert!(tv1.nsec < 1000000000i32);
875 let tv2 = get_time();
876 debug!("tv2=%? sec + %? nsec", tv2.sec as uint, tv2.nsec as uint);
878 assert!(tv2.sec >= tv1.sec);
879 assert!(tv2.sec < SOME_FUTURE_DATE);
880 assert!(tv2.nsec < 1000000000i32);
881 if tv2.sec == tv1.sec {
882 assert!(tv2.nsec >= tv1.nsec);
886 fn test_precise_time() {
887 let s0 = precise_time_s();
888 let ns1 = precise_time_ns();
890 debug!("s0=%s sec", float::to_str_digits(s0, 9u));
892 let ns0 = (s0 * 1000000000.) as u64;
893 debug!("ns0=%? ns", ns0);
895 debug!("ns1=%? ns", ns0);
898 let ns2 = precise_time_ns();
899 debug!("ns2=%? ns", ns0);
904 os::setenv("TZ", "America/Los_Angeles");
907 let time = ::time::Timespec::new(1234567890, 54321);
908 let utc = at_utc(time);
910 assert!(utc.tm_sec == 30_i32);
911 assert!(utc.tm_min == 31_i32);
912 assert!(utc.tm_hour == 23_i32);
913 assert!(utc.tm_mday == 13_i32);
914 assert!(utc.tm_mon == 1_i32);
915 assert!(utc.tm_year == 109_i32);
916 assert!(utc.tm_wday == 5_i32);
917 assert!(utc.tm_yday == 43_i32);
918 assert!(utc.tm_isdst == 0_i32);
919 assert!(utc.tm_gmtoff == 0_i32);
920 assert!(utc.tm_zone == ~"UTC");
921 assert!(utc.tm_nsec == 54321_i32);
925 os::setenv("TZ", "America/Los_Angeles");
928 let time = ::time::Timespec::new(1234567890, 54321);
929 let local = at(time);
931 error!("time_at: %?", local);
933 assert!(local.tm_sec == 30_i32);
934 assert!(local.tm_min == 31_i32);
935 assert!(local.tm_hour == 15_i32);
936 assert!(local.tm_mday == 13_i32);
937 assert!(local.tm_mon == 1_i32);
938 assert!(local.tm_year == 109_i32);
939 assert!(local.tm_wday == 5_i32);
940 assert!(local.tm_yday == 43_i32);
941 assert!(local.tm_isdst == 0_i32);
942 assert!(local.tm_gmtoff == -28800_i32);
944 // FIXME (#2350): We should probably standardize on the timezone
946 let zone = &local.tm_zone;
947 assert!(*zone == ~"PST" || *zone == ~"Pacific Standard Time");
949 assert!(local.tm_nsec == 54321_i32);
952 fn test_to_timespec() {
953 os::setenv("TZ", "America/Los_Angeles");
956 let time = ::time::Timespec::new(1234567890, 54321);
957 let utc = at_utc(time);
959 assert_eq!(utc.to_timespec(), time);
960 assert_eq!(utc.to_local().to_timespec(), time);
963 fn test_conversions() {
964 os::setenv("TZ", "America/Los_Angeles");
967 let time = ::time::Timespec::new(1234567890, 54321);
968 let utc = at_utc(time);
969 let local = at(time);
971 assert!(local.to_local() == local);
972 assert!(local.to_utc() == utc);
973 assert!(local.to_utc().to_local() == local);
974 assert!(utc.to_utc() == utc);
975 assert!(utc.to_local() == local);
976 assert!(utc.to_local().to_utc() == utc);
980 os::setenv("TZ", "America/Los_Angeles");
983 match strptime("", "") {
985 assert!(tm.tm_sec == 0_i32);
986 assert!(tm.tm_min == 0_i32);
987 assert!(tm.tm_hour == 0_i32);
988 assert!(tm.tm_mday == 0_i32);
989 assert!(tm.tm_mon == 0_i32);
990 assert!(tm.tm_year == 0_i32);
991 assert!(tm.tm_wday == 0_i32);
992 assert!(tm.tm_isdst == 0_i32);
993 assert!(tm.tm_gmtoff == 0_i32);
994 assert!(tm.tm_zone == ~"");
995 assert!(tm.tm_nsec == 0_i32);
1000 let format = "%a %b %e %T %Y";
1001 assert_eq!(strptime("", format), Err(~"Invalid time"));
1002 assert!(strptime("Fri Feb 13 15:31:30", format)
1003 == Err(~"Invalid time"));
1005 match strptime("Fri Feb 13 15:31:30 2009", format) {
1008 assert!(tm.tm_sec == 30_i32);
1009 assert!(tm.tm_min == 31_i32);
1010 assert!(tm.tm_hour == 15_i32);
1011 assert!(tm.tm_mday == 13_i32);
1012 assert!(tm.tm_mon == 1_i32);
1013 assert!(tm.tm_year == 109_i32);
1014 assert!(tm.tm_wday == 5_i32);
1015 assert!(tm.tm_yday == 0_i32);
1016 assert!(tm.tm_isdst == 0_i32);
1017 assert!(tm.tm_gmtoff == 0_i32);
1018 assert!(tm.tm_zone == ~"");
1019 assert!(tm.tm_nsec == 0_i32);
1023 fn test(s: &str, format: &str) -> bool {
1024 match strptime(s, format) {
1025 Ok(ref tm) => tm.strftime(format) == s.to_owned(),
1039 for day in days.iter() {
1040 assert!(test(*day, "%A"));
1052 for day in days.iter() {
1053 assert!(test(*day, "%a"));
1070 for day in months.iter() {
1071 assert!(test(*day, "%B"));
1088 for day in months.iter() {
1089 assert!(test(*day, "%b"));
1092 assert!(test("19", "%C"));
1093 assert!(test("Fri Feb 13 23:31:30 2009", "%c"));
1094 assert!(test("02/13/09", "%D"));
1095 assert!(test("03", "%d"));
1096 assert!(test("13", "%d"));
1097 assert!(test(" 3", "%e"));
1098 assert!(test("13", "%e"));
1099 assert!(test("2009-02-13", "%F"));
1100 assert!(test("03", "%H"));
1101 assert!(test("13", "%H"));
1102 assert!(test("03", "%I")); // FIXME (#2350): flesh out
1103 assert!(test("11", "%I")); // FIXME (#2350): flesh out
1104 assert!(test("044", "%j"));
1105 assert!(test(" 3", "%k"));
1106 assert!(test("13", "%k"));
1107 assert!(test(" 1", "%l"));
1108 assert!(test("11", "%l"));
1109 assert!(test("03", "%M"));
1110 assert!(test("13", "%M"));
1111 assert!(test("\n", "%n"));
1112 assert!(test("am", "%P"));
1113 assert!(test("pm", "%P"));
1114 assert!(test("AM", "%p"));
1115 assert!(test("PM", "%p"));
1116 assert!(test("23:31", "%R"));
1117 assert!(test("11:31:30 AM", "%r"));
1118 assert!(test("11:31:30 PM", "%r"));
1119 assert!(test("03", "%S"));
1120 assert!(test("13", "%S"));
1121 assert!(test("15:31:30", "%T"));
1122 assert!(test("\t", "%t"));
1123 assert!(test("1", "%u"));
1124 assert!(test("7", "%u"));
1125 assert!(test("13-Feb-2009", "%v"));
1126 assert!(test("0", "%w"));
1127 assert!(test("6", "%w"));
1128 assert!(test("2009", "%Y"));
1129 assert!(test("09", "%y"));
1130 assert!(strptime("UTC", "%Z").unwrap().tm_zone ==
1132 assert!(strptime("PST", "%Z").unwrap().tm_zone ==
1134 assert!(strptime("-0000", "%z").unwrap().tm_gmtoff ==
1136 assert!(strptime("-0800", "%z").unwrap().tm_gmtoff ==
1138 assert!(test("%", "%%"));
1141 assert_eq!(strptime("360", "%Y-%m-%d"), Err(~"Invalid year"))
1145 os::setenv("TZ", "America/Los_Angeles");
1148 let time = ::time::Timespec::new(1234567890, 54321);
1149 let utc = at_utc(time);
1150 let local = at(time);
1152 error!("test_ctime: %? %?", utc.ctime(), local.ctime());
1154 assert_eq!(utc.ctime(), ~"Fri Feb 13 23:31:30 2009");
1155 assert_eq!(local.ctime(), ~"Fri Feb 13 15:31:30 2009");
1158 fn test_strftime() {
1159 os::setenv("TZ", "America/Los_Angeles");
1162 let time = ::time::Timespec::new(1234567890, 54321);
1163 let utc = at_utc(time);
1164 let local = at(time);
1166 assert_eq!(local.strftime(""), ~"");
1167 assert_eq!(local.strftime("%A"), ~"Friday");
1168 assert_eq!(local.strftime("%a"), ~"Fri");
1169 assert_eq!(local.strftime("%B"), ~"February");
1170 assert_eq!(local.strftime("%b"), ~"Feb");
1171 assert_eq!(local.strftime("%C"), ~"20");
1172 assert_eq!(local.strftime("%c"), ~"Fri Feb 13 15:31:30 2009");
1173 assert_eq!(local.strftime("%D"), ~"02/13/09");
1174 assert_eq!(local.strftime("%d"), ~"13");
1175 assert_eq!(local.strftime("%e"), ~"13");
1176 assert_eq!(local.strftime("%F"), ~"2009-02-13");
1177 // assert!(local.strftime("%G") == "2009");
1178 // assert!(local.strftime("%g") == "09");
1179 assert_eq!(local.strftime("%H"), ~"15");
1180 assert_eq!(local.strftime("%I"), ~"03");
1181 assert_eq!(local.strftime("%j"), ~"044");
1182 assert_eq!(local.strftime("%k"), ~"15");
1183 assert_eq!(local.strftime("%l"), ~" 3");
1184 assert_eq!(local.strftime("%M"), ~"31");
1185 assert_eq!(local.strftime("%m"), ~"02");
1186 assert_eq!(local.strftime("%n"), ~"\n");
1187 assert_eq!(local.strftime("%P"), ~"pm");
1188 assert_eq!(local.strftime("%p"), ~"PM");
1189 assert_eq!(local.strftime("%R"), ~"15:31");
1190 assert_eq!(local.strftime("%r"), ~"03:31:30 PM");
1191 assert_eq!(local.strftime("%S"), ~"30");
1192 assert_eq!(local.strftime("%s"), ~"1234567890");
1193 assert_eq!(local.strftime("%T"), ~"15:31:30");
1194 assert_eq!(local.strftime("%t"), ~"\t");
1195 // assert!(local.strftime("%U") == "06");
1196 assert_eq!(local.strftime("%u"), ~"5");
1197 // assert!(local.strftime("%V") == "07");
1198 assert_eq!(local.strftime("%v"), ~"13-Feb-2009");
1199 // assert!(local.strftime("%W") == "06");
1200 assert_eq!(local.strftime("%w"), ~"5");
1203 assert_eq!(local.strftime("%Y"), ~"2009");
1204 assert_eq!(local.strftime("%y"), ~"09");
1206 // FIXME (#2350): We should probably standardize on the timezone
1208 let zone = local.strftime("%Z");
1209 assert!(zone == ~"PST" || zone == ~"Pacific Standard Time");
1211 assert_eq!(local.strftime("%z"), ~"-0800");
1212 assert_eq!(local.strftime("%%"), ~"%");
1214 // FIXME (#2350): We should probably standardize on the timezone
1216 let rfc822 = local.rfc822();
1217 let prefix = ~"Fri, 13 Feb 2009 15:31:30 ";
1218 assert!(rfc822 == prefix + "PST" ||
1219 rfc822 == prefix + "Pacific Standard Time");
1221 assert_eq!(local.ctime(), ~"Fri Feb 13 15:31:30 2009");
1222 assert_eq!(local.rfc822z(), ~"Fri, 13 Feb 2009 15:31:30 -0800");
1223 assert_eq!(local.rfc3339(), ~"2009-02-13T15:31:30-08:00");
1225 assert_eq!(utc.ctime(), ~"Fri Feb 13 23:31:30 2009");
1226 assert_eq!(utc.rfc822(), ~"Fri, 13 Feb 2009 23:31:30 GMT");
1227 assert_eq!(utc.rfc822z(), ~"Fri, 13 Feb 2009 23:31:30 -0000");
1228 assert_eq!(utc.rfc3339(), ~"2009-02-13T23:31:30Z");
1231 fn test_timespec_eq_ord() {
1232 let a = &Timespec::new(-2, 1);
1233 let b = &Timespec::new(-1, 2);
1234 let c = &Timespec::new(1, 2);
1235 let d = &Timespec::new(2, 1);
1236 let e = &Timespec::new(2, 1);
1264 // The tests race on tzset. So instead of having many independent
1265 // tests, we will just call the functions now.
1267 test_precise_time();
1275 test_timespec_eq_ord();