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)> {
291 let mut value = 0_i32;
298 let range = ss.char_range_at(pos);
303 value = value * 10_i32 + (range.ch as i32 - '0' as i32);
314 fn match_digits_in_range(ss: &str, pos: uint, digits: uint, ws: bool,
315 min: i32, max: i32) -> Option<(i32, uint)> {
316 match match_digits(ss, pos, digits, ws) {
317 Some((val, pos)) if val >= min && val <= max => {
324 fn parse_char(s: &str, pos: uint, c: char) -> Result<uint, ~str> {
325 let range = s.char_range_at(pos);
330 Err(fmt!("Expected %?, found %?",
332 str::from_char(range.ch)))
336 fn parse_type(s: &str, pos: uint, ch: char, tm: &mut Tm)
337 -> Result<uint, ~str> {
339 'A' => match match_strs(s, pos, [
343 (~"Wednesday", 3_i32),
344 (~"Thursday", 4_i32),
348 Some(item) => { let (v, pos) = item; tm.tm_wday = v; Ok(pos) }
349 None => Err(~"Invalid day")
351 'a' => match match_strs(s, pos, [
360 Some(item) => { let (v, pos) = item; tm.tm_wday = v; Ok(pos) }
361 None => Err(~"Invalid day")
363 'B' => match match_strs(s, pos, [
365 (~"February", 1_i32),
372 (~"September", 8_i32),
374 (~"November", 10_i32),
375 (~"December", 11_i32)
377 Some(item) => { let (v, pos) = item; tm.tm_mon = v; Ok(pos) }
378 None => Err(~"Invalid month")
380 'b' | 'h' => match match_strs(s, pos, [
394 Some(item) => { let (v, pos) = item; tm.tm_mon = v; Ok(pos) }
395 None => Err(~"Invalid month")
397 'C' => match match_digits_in_range(s, pos, 2u, false, 0_i32,
401 tm.tm_year += (v * 100_i32) - 1900_i32;
404 None => Err(~"Invalid year")
407 parse_type(s, pos, 'a', &mut *tm)
408 .chain(|pos| parse_char(s, pos, ' '))
409 .chain(|pos| parse_type(s, pos, 'b', &mut *tm))
410 .chain(|pos| parse_char(s, pos, ' '))
411 .chain(|pos| parse_type(s, pos, 'e', &mut *tm))
412 .chain(|pos| parse_char(s, pos, ' '))
413 .chain(|pos| parse_type(s, pos, 'T', &mut *tm))
414 .chain(|pos| parse_char(s, pos, ' '))
415 .chain(|pos| parse_type(s, pos, 'Y', &mut *tm))
418 parse_type(s, pos, 'm', &mut *tm)
419 .chain(|pos| parse_char(s, pos, '/'))
420 .chain(|pos| parse_type(s, pos, 'd', &mut *tm))
421 .chain(|pos| parse_char(s, pos, '/'))
422 .chain(|pos| parse_type(s, pos, 'y', &mut *tm))
424 'd' => match match_digits_in_range(s, pos, 2u, false, 1_i32,
426 Some(item) => { let (v, pos) = item; tm.tm_mday = v; Ok(pos) }
427 None => Err(~"Invalid day of the month")
429 'e' => match match_digits_in_range(s, pos, 2u, true, 1_i32,
431 Some(item) => { let (v, pos) = item; tm.tm_mday = v; Ok(pos) }
432 None => Err(~"Invalid day of the month")
435 parse_type(s, pos, 'Y', &mut *tm)
436 .chain(|pos| parse_char(s, pos, '-'))
437 .chain(|pos| parse_type(s, pos, 'm', &mut *tm))
438 .chain(|pos| parse_char(s, pos, '-'))
439 .chain(|pos| parse_type(s, pos, 'd', &mut *tm))
442 match match_digits_in_range(s, pos, 2u, false, 0_i32, 23_i32) {
443 Some(item) => { let (v, pos) = item; tm.tm_hour = v; Ok(pos) }
444 None => Err(~"Invalid hour")
448 match match_digits_in_range(s, pos, 2u, false, 1_i32, 12_i32) {
451 tm.tm_hour = if v == 12_i32 { 0_i32 } else { v };
454 None => Err(~"Invalid hour")
458 match match_digits_in_range(s, pos, 3u, false, 1_i32, 366_i32) {
461 tm.tm_yday = v - 1_i32;
464 None => Err(~"Invalid day of year")
468 match match_digits_in_range(s, pos, 2u, true, 0_i32, 23_i32) {
469 Some(item) => { let (v, pos) = item; tm.tm_hour = v; Ok(pos) }
470 None => Err(~"Invalid hour")
474 match match_digits_in_range(s, pos, 2u, true, 1_i32, 12_i32) {
477 tm.tm_hour = if v == 12_i32 { 0_i32 } else { v };
480 None => Err(~"Invalid hour")
484 match match_digits_in_range(s, pos, 2u, false, 0_i32, 59_i32) {
485 Some(item) => { let (v, pos) = item; tm.tm_min = v; Ok(pos) }
486 None => Err(~"Invalid minute")
490 match match_digits_in_range(s, pos, 2u, false, 1_i32, 12_i32) {
493 tm.tm_mon = v - 1_i32;
496 None => Err(~"Invalid month")
499 'n' => parse_char(s, pos, '\n'),
500 'P' => match match_strs(s, pos,
501 [(~"am", 0_i32), (~"pm", 12_i32)]) {
503 Some(item) => { let (v, pos) = item; tm.tm_hour += v; Ok(pos) }
504 None => Err(~"Invalid hour")
506 'p' => match match_strs(s, pos,
507 [(~"AM", 0_i32), (~"PM", 12_i32)]) {
509 Some(item) => { let (v, pos) = item; tm.tm_hour += v; Ok(pos) }
510 None => Err(~"Invalid hour")
513 parse_type(s, pos, 'H', &mut *tm)
514 .chain(|pos| parse_char(s, pos, ':'))
515 .chain(|pos| parse_type(s, pos, 'M', &mut *tm))
518 parse_type(s, pos, 'I', &mut *tm)
519 .chain(|pos| parse_char(s, pos, ':'))
520 .chain(|pos| parse_type(s, pos, 'M', &mut *tm))
521 .chain(|pos| parse_char(s, pos, ':'))
522 .chain(|pos| parse_type(s, pos, 'S', &mut *tm))
523 .chain(|pos| parse_char(s, pos, ' '))
524 .chain(|pos| parse_type(s, pos, 'p', &mut *tm))
527 match match_digits_in_range(s, pos, 2u, false, 0_i32, 60_i32) {
533 None => Err(~"Invalid second")
538 parse_type(s, pos, 'H', &mut *tm)
539 .chain(|pos| parse_char(s, pos, ':'))
540 .chain(|pos| parse_type(s, pos, 'M', &mut *tm))
541 .chain(|pos| parse_char(s, pos, ':'))
542 .chain(|pos| parse_type(s, pos, 'S', &mut *tm))
544 't' => parse_char(s, pos, '\t'),
546 match match_digits_in_range(s, pos, 1u, false, 1_i32, 7_i32) {
549 tm.tm_wday = if v == 7 { 0 } else { v };
552 None => Err(~"Invalid day of week")
556 parse_type(s, pos, 'e', &mut *tm)
557 .chain(|pos| parse_char(s, pos, '-'))
558 .chain(|pos| parse_type(s, pos, 'b', &mut *tm))
559 .chain(|pos| parse_char(s, pos, '-'))
560 .chain(|pos| parse_type(s, pos, 'Y', &mut *tm))
564 match match_digits_in_range(s, pos, 1u, false, 0_i32, 6_i32) {
565 Some(item) => { let (v, pos) = item; tm.tm_wday = v; Ok(pos) }
566 None => Err(~"Invalid day of week")
572 match match_digits(s, pos, 4u, false) {
575 tm.tm_year = v - 1900_i32;
578 None => Err(~"Invalid year")
582 match match_digits_in_range(s, pos, 2u, false, 0_i32, 99_i32) {
588 None => Err(~"Invalid year")
592 if match_str(s, pos, "UTC") || match_str(s, pos, "GMT") {
593 tm.tm_gmtoff = 0_i32;
597 // It's odd, but to maintain compatibility with c's
598 // strptime we ignore the timezone.
602 let range = s.char_range_at(pos);
604 if range.ch == ' ' { break; }
611 let range = s.char_range_at(pos);
613 if range.ch == '+' || range.ch == '-' {
614 match match_digits(s, range.next, 4u, false) {
618 tm.tm_gmtoff = 0_i32;
624 None => Err(~"Invalid zone offset")
627 Err(~"Invalid zone offset")
630 '%' => parse_char(s, pos, '%'),
632 Err(fmt!("unknown formatting type: %?", str::from_char(ch)))
637 do io::with_str_reader(format) |rdr| {
654 let mut result = Err(~"Invalid time");
656 while !rdr.eof() && pos < len {
657 let range = s.char_range_at(pos);
659 let next = range.next;
661 match rdr.read_char() {
663 match parse_type(s, pos, rdr.read_char(), &mut tm) {
664 Ok(next) => pos = next,
665 Err(e) => { result = Err(e); break; }
675 if pos == len && rdr.eof() {
685 tm_isdst: tm.tm_isdst,
686 tm_gmtoff: tm.tm_gmtoff,
687 tm_zone: tm.tm_zone.clone(),
694 priv fn do_strftime(format: &str, tm: &Tm) -> ~str {
695 fn parse_type(ch: char, tm: &Tm) -> ~str {
696 //FIXME (#2350): Implement missing types.
697 let die = || fmt!("strftime: can't understand this format %c ", ch);
699 'A' => match tm.tm_wday as int {
709 'a' => match tm.tm_wday as int {
719 'B' => match tm.tm_mon as int {
734 'b' | 'h' => match tm.tm_mon as int {
749 'C' => fmt!("%02d", (tm.tm_year as int + 1900) / 100),
751 fmt!("%s %s %s %s %s",
764 'd' => fmt!("%02d", tm.tm_mday as int),
765 'e' => fmt!("%2d", tm.tm_mday as int),
774 'H' => fmt!("%02d", tm.tm_hour as int),
776 let mut h = tm.tm_hour as int;
778 if h > 12 { h -= 12 }
781 'j' => fmt!("%03d", tm.tm_yday as int + 1),
782 'k' => fmt!("%2d", tm.tm_hour as int),
784 let mut h = tm.tm_hour as int;
786 if h > 12 { h -= 12 }
789 'M' => fmt!("%02d", tm.tm_min as int),
790 'm' => fmt!("%02d", tm.tm_mon as int + 1),
792 'P' => if (tm.tm_hour as int) < 12 { ~"am" } else { ~"pm" },
793 'p' => if (tm.tm_hour as int) < 12 { ~"AM" } else { ~"PM" },
806 'S' => fmt!("%02d", tm.tm_sec as int),
807 's' => fmt!("%d", tm.to_timespec().sec as int),
817 let i = tm.tm_wday as int;
818 int::to_str(if i == 0 { 7 } else { i })
828 'w' => int::to_str(tm.tm_wday as int),
831 'Y' => int::to_str(tm.tm_year as int + 1900),
832 'y' => fmt!("%02d", (tm.tm_year as int + 1900) % 100),
833 'Z' => tm.tm_zone.clone(),
835 let sign = if tm.tm_gmtoff > 0_i32 { '+' } else { '-' };
836 let mut m = num::abs(tm.tm_gmtoff) / 60_i32;
839 fmt!("%c%02d%02d", sign, h as int, m as int)
849 do io::with_str_reader(format) |rdr| {
851 match rdr.read_char() {
852 '%' => buf.push_str(parse_type(rdr.read_char(), tm)),
853 ch => buf.push_char(ch)
867 use std::result::{Err, Ok};
870 static SOME_RECENT_DATE: i64 = 1325376000i64; // 2012-01-01T00:00:00Z
871 static SOME_FUTURE_DATE: i64 = 1577836800i64; // 2020-01-01T00:00:00Z
873 let tv1 = get_time();
874 debug!("tv1=%? sec + %? nsec", tv1.sec as uint, tv1.nsec as uint);
876 assert!(tv1.sec > SOME_RECENT_DATE);
877 assert!(tv1.nsec < 1000000000i32);
879 let tv2 = get_time();
880 debug!("tv2=%? sec + %? nsec", tv2.sec as uint, tv2.nsec as uint);
882 assert!(tv2.sec >= tv1.sec);
883 assert!(tv2.sec < SOME_FUTURE_DATE);
884 assert!(tv2.nsec < 1000000000i32);
885 if tv2.sec == tv1.sec {
886 assert!(tv2.nsec >= tv1.nsec);
890 fn test_precise_time() {
891 let s0 = precise_time_s();
892 let ns1 = precise_time_ns();
894 debug!("s0=%s sec", float::to_str_digits(s0, 9u));
896 let ns0 = (s0 * 1000000000.) as u64;
897 debug!("ns0=%? ns", ns0);
899 debug!("ns1=%? ns", ns0);
902 let ns2 = precise_time_ns();
903 debug!("ns2=%? ns", ns0);
908 os::setenv("TZ", "America/Los_Angeles");
911 let time = Timespec::new(1234567890, 54321);
912 let utc = at_utc(time);
914 assert!(utc.tm_sec == 30_i32);
915 assert!(utc.tm_min == 31_i32);
916 assert!(utc.tm_hour == 23_i32);
917 assert!(utc.tm_mday == 13_i32);
918 assert!(utc.tm_mon == 1_i32);
919 assert!(utc.tm_year == 109_i32);
920 assert!(utc.tm_wday == 5_i32);
921 assert!(utc.tm_yday == 43_i32);
922 assert!(utc.tm_isdst == 0_i32);
923 assert!(utc.tm_gmtoff == 0_i32);
924 assert!(utc.tm_zone == ~"UTC");
925 assert!(utc.tm_nsec == 54321_i32);
929 os::setenv("TZ", "America/Los_Angeles");
932 let time = Timespec::new(1234567890, 54321);
933 let local = at(time);
935 error!("time_at: %?", local);
937 assert!(local.tm_sec == 30_i32);
938 assert!(local.tm_min == 31_i32);
939 assert!(local.tm_hour == 15_i32);
940 assert!(local.tm_mday == 13_i32);
941 assert!(local.tm_mon == 1_i32);
942 assert!(local.tm_year == 109_i32);
943 assert!(local.tm_wday == 5_i32);
944 assert!(local.tm_yday == 43_i32);
945 assert!(local.tm_isdst == 0_i32);
946 assert!(local.tm_gmtoff == -28800_i32);
948 // FIXME (#2350): We should probably standardize on the timezone
950 let zone = &local.tm_zone;
951 assert!(*zone == ~"PST" || *zone == ~"Pacific Standard Time");
953 assert!(local.tm_nsec == 54321_i32);
956 fn test_to_timespec() {
957 os::setenv("TZ", "America/Los_Angeles");
960 let time = Timespec::new(1234567890, 54321);
961 let utc = at_utc(time);
963 assert_eq!(utc.to_timespec(), time);
964 assert_eq!(utc.to_local().to_timespec(), time);
967 fn test_conversions() {
968 os::setenv("TZ", "America/Los_Angeles");
971 let time = Timespec::new(1234567890, 54321);
972 let utc = at_utc(time);
973 let local = at(time);
975 assert!(local.to_local() == local);
976 assert!(local.to_utc() == utc);
977 assert!(local.to_utc().to_local() == local);
978 assert!(utc.to_utc() == utc);
979 assert!(utc.to_local() == local);
980 assert!(utc.to_local().to_utc() == utc);
984 os::setenv("TZ", "America/Los_Angeles");
987 match strptime("", "") {
989 assert!(tm.tm_sec == 0_i32);
990 assert!(tm.tm_min == 0_i32);
991 assert!(tm.tm_hour == 0_i32);
992 assert!(tm.tm_mday == 0_i32);
993 assert!(tm.tm_mon == 0_i32);
994 assert!(tm.tm_year == 0_i32);
995 assert!(tm.tm_wday == 0_i32);
996 assert!(tm.tm_isdst == 0_i32);
997 assert!(tm.tm_gmtoff == 0_i32);
998 assert!(tm.tm_zone == ~"");
999 assert!(tm.tm_nsec == 0_i32);
1004 let format = "%a %b %e %T %Y";
1005 assert_eq!(strptime("", format), Err(~"Invalid time"));
1006 assert!(strptime("Fri Feb 13 15:31:30", format)
1007 == Err(~"Invalid time"));
1009 match strptime("Fri Feb 13 15:31:30 2009", format) {
1012 assert!(tm.tm_sec == 30_i32);
1013 assert!(tm.tm_min == 31_i32);
1014 assert!(tm.tm_hour == 15_i32);
1015 assert!(tm.tm_mday == 13_i32);
1016 assert!(tm.tm_mon == 1_i32);
1017 assert!(tm.tm_year == 109_i32);
1018 assert!(tm.tm_wday == 5_i32);
1019 assert!(tm.tm_yday == 0_i32);
1020 assert!(tm.tm_isdst == 0_i32);
1021 assert!(tm.tm_gmtoff == 0_i32);
1022 assert!(tm.tm_zone == ~"");
1023 assert!(tm.tm_nsec == 0_i32);
1027 fn test(s: &str, format: &str) -> bool {
1028 match strptime(s, format) {
1029 Ok(ref tm) => tm.strftime(format) == s.to_owned(),
1043 for day in days.iter() {
1044 assert!(test(*day, "%A"));
1056 for day in days.iter() {
1057 assert!(test(*day, "%a"));
1074 for day in months.iter() {
1075 assert!(test(*day, "%B"));
1092 for day in months.iter() {
1093 assert!(test(*day, "%b"));
1096 assert!(test("19", "%C"));
1097 assert!(test("Fri Feb 13 23:31:30 2009", "%c"));
1098 assert!(test("02/13/09", "%D"));
1099 assert!(test("03", "%d"));
1100 assert!(test("13", "%d"));
1101 assert!(test(" 3", "%e"));
1102 assert!(test("13", "%e"));
1103 assert!(test("2009-02-13", "%F"));
1104 assert!(test("03", "%H"));
1105 assert!(test("13", "%H"));
1106 assert!(test("03", "%I")); // FIXME (#2350): flesh out
1107 assert!(test("11", "%I")); // FIXME (#2350): flesh out
1108 assert!(test("044", "%j"));
1109 assert!(test(" 3", "%k"));
1110 assert!(test("13", "%k"));
1111 assert!(test(" 1", "%l"));
1112 assert!(test("11", "%l"));
1113 assert!(test("03", "%M"));
1114 assert!(test("13", "%M"));
1115 assert!(test("\n", "%n"));
1116 assert!(test("am", "%P"));
1117 assert!(test("pm", "%P"));
1118 assert!(test("AM", "%p"));
1119 assert!(test("PM", "%p"));
1120 assert!(test("23:31", "%R"));
1121 assert!(test("11:31:30 AM", "%r"));
1122 assert!(test("11:31:30 PM", "%r"));
1123 assert!(test("03", "%S"));
1124 assert!(test("13", "%S"));
1125 assert!(test("15:31:30", "%T"));
1126 assert!(test("\t", "%t"));
1127 assert!(test("1", "%u"));
1128 assert!(test("7", "%u"));
1129 assert!(test("13-Feb-2009", "%v"));
1130 assert!(test("0", "%w"));
1131 assert!(test("6", "%w"));
1132 assert!(test("2009", "%Y"));
1133 assert!(test("09", "%y"));
1134 assert!(strptime("UTC", "%Z").unwrap().tm_zone ==
1136 assert!(strptime("PST", "%Z").unwrap().tm_zone ==
1138 assert!(strptime("-0000", "%z").unwrap().tm_gmtoff ==
1140 assert!(strptime("-0800", "%z").unwrap().tm_gmtoff ==
1142 assert!(test("%", "%%"));
1145 assert_eq!(strptime("360", "%Y-%m-%d"), Err(~"Invalid year"))
1149 os::setenv("TZ", "America/Los_Angeles");
1152 let time = Timespec::new(1234567890, 54321);
1153 let utc = at_utc(time);
1154 let local = at(time);
1156 error!("test_ctime: %? %?", utc.ctime(), local.ctime());
1158 assert_eq!(utc.ctime(), ~"Fri Feb 13 23:31:30 2009");
1159 assert_eq!(local.ctime(), ~"Fri Feb 13 15:31:30 2009");
1162 fn test_strftime() {
1163 os::setenv("TZ", "America/Los_Angeles");
1166 let time = Timespec::new(1234567890, 54321);
1167 let utc = at_utc(time);
1168 let local = at(time);
1170 assert_eq!(local.strftime(""), ~"");
1171 assert_eq!(local.strftime("%A"), ~"Friday");
1172 assert_eq!(local.strftime("%a"), ~"Fri");
1173 assert_eq!(local.strftime("%B"), ~"February");
1174 assert_eq!(local.strftime("%b"), ~"Feb");
1175 assert_eq!(local.strftime("%C"), ~"20");
1176 assert_eq!(local.strftime("%c"), ~"Fri Feb 13 15:31:30 2009");
1177 assert_eq!(local.strftime("%D"), ~"02/13/09");
1178 assert_eq!(local.strftime("%d"), ~"13");
1179 assert_eq!(local.strftime("%e"), ~"13");
1180 assert_eq!(local.strftime("%F"), ~"2009-02-13");
1181 // assert!(local.strftime("%G") == "2009");
1182 // assert!(local.strftime("%g") == "09");
1183 assert_eq!(local.strftime("%H"), ~"15");
1184 assert_eq!(local.strftime("%I"), ~"03");
1185 assert_eq!(local.strftime("%j"), ~"044");
1186 assert_eq!(local.strftime("%k"), ~"15");
1187 assert_eq!(local.strftime("%l"), ~" 3");
1188 assert_eq!(local.strftime("%M"), ~"31");
1189 assert_eq!(local.strftime("%m"), ~"02");
1190 assert_eq!(local.strftime("%n"), ~"\n");
1191 assert_eq!(local.strftime("%P"), ~"pm");
1192 assert_eq!(local.strftime("%p"), ~"PM");
1193 assert_eq!(local.strftime("%R"), ~"15:31");
1194 assert_eq!(local.strftime("%r"), ~"03:31:30 PM");
1195 assert_eq!(local.strftime("%S"), ~"30");
1196 assert_eq!(local.strftime("%s"), ~"1234567890");
1197 assert_eq!(local.strftime("%T"), ~"15:31:30");
1198 assert_eq!(local.strftime("%t"), ~"\t");
1199 // assert!(local.strftime("%U") == "06");
1200 assert_eq!(local.strftime("%u"), ~"5");
1201 // assert!(local.strftime("%V") == "07");
1202 assert_eq!(local.strftime("%v"), ~"13-Feb-2009");
1203 // assert!(local.strftime("%W") == "06");
1204 assert_eq!(local.strftime("%w"), ~"5");
1207 assert_eq!(local.strftime("%Y"), ~"2009");
1208 assert_eq!(local.strftime("%y"), ~"09");
1210 // FIXME (#2350): We should probably standardize on the timezone
1212 let zone = local.strftime("%Z");
1213 assert!(zone == ~"PST" || zone == ~"Pacific Standard Time");
1215 assert_eq!(local.strftime("%z"), ~"-0800");
1216 assert_eq!(local.strftime("%%"), ~"%");
1218 // FIXME (#2350): We should probably standardize on the timezone
1220 let rfc822 = local.rfc822();
1221 let prefix = ~"Fri, 13 Feb 2009 15:31:30 ";
1222 assert!(rfc822 == prefix + "PST" ||
1223 rfc822 == prefix + "Pacific Standard Time");
1225 assert_eq!(local.ctime(), ~"Fri Feb 13 15:31:30 2009");
1226 assert_eq!(local.rfc822z(), ~"Fri, 13 Feb 2009 15:31:30 -0800");
1227 assert_eq!(local.rfc3339(), ~"2009-02-13T15:31:30-08:00");
1229 assert_eq!(utc.ctime(), ~"Fri Feb 13 23:31:30 2009");
1230 assert_eq!(utc.rfc822(), ~"Fri, 13 Feb 2009 23:31:30 GMT");
1231 assert_eq!(utc.rfc822z(), ~"Fri, 13 Feb 2009 23:31:30 -0000");
1232 assert_eq!(utc.rfc3339(), ~"2009-02-13T23:31:30Z");
1235 fn test_timespec_eq_ord() {
1236 let a = &Timespec::new(-2, 1);
1237 let b = &Timespec::new(-1, 2);
1238 let c = &Timespec::new(1, 2);
1239 let d = &Timespec::new(2, 1);
1240 let e = &Timespec::new(2, 1);
1268 // The tests race on tzset. So instead of having many independent
1269 // tests, we will just call the functions now.
1271 test_precise_time();
1279 test_timespec_eq_ord();