about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorBrian Anderson <banderson@mozilla.com>2012-11-28 14:08:19 -0800
committerBrian Anderson <banderson@mozilla.com>2012-11-28 14:49:58 -0800
commit16f72df7041602dd286c1c00b2b745d21bf10e9b (patch)
tree9227bb7e7db96be0d1dd6cd77d79f8a00e61ce06 /src/libstd
parent42c2c2ab2f4dfa350dd01d33025cd5027a358d33 (diff)
parent32763caa600857bc0116a9221e4a94431b5b6907 (diff)
Merge remote-tracking branch 'erickt/time'
Conflicts:
	src/libstd/time.rs
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/std.rc8
-rw-r--r--src/libstd/time.rs264
2 files changed, 162 insertions, 110 deletions
diff --git a/src/libstd/std.rc b/src/libstd/std.rc
index a56b1ac61a0..a22bbe1cd6f 100644
--- a/src/libstd/std.rc
+++ b/src/libstd/std.rc
@@ -96,6 +96,14 @@ mod unicode;
 pub mod test;
 pub mod serialization;
 
+// A curious inner-module that's not exported that contains the binding
+// 'std' so that macro-expanded references to std::serialization and such
+// can be resolved within libcore.
+#[doc(hidden)] // FIXME #3538
+mod std {
+    pub use serialization;
+}
+
 // Local Variables:
 // mode: rust;
 // fill-column: 78;
diff --git a/src/libstd/time.rs b/src/libstd/time.rs
index 7596f4282b7..d8cbfbadb1f 100644
--- a/src/libstd/time.rs
+++ b/src/libstd/time.rs
@@ -21,13 +21,21 @@ extern mod rustrt {
 }
 
 /// A record specifying a time value in seconds and nanoseconds.
-pub type Timespec = {sec: i64, nsec: i32};
+#[auto_serialize]
+#[auto_deserialize]
+pub struct Timespec { sec: i64, nsec: i32 }
+
+impl Timespec {
+    static fn new(sec: i64, nsec: i32) -> Timespec {
+        Timespec { sec: sec, nsec: nsec }
+    }
+}
 
 impl Timespec : Eq {
     pure fn eq(&self, other: &Timespec) -> bool {
-        (*self).sec == (*other).sec && (*self).nsec == (*other).nsec
+        self.sec == other.sec && self.nsec == other.nsec
     }
-    pure fn ne(&self, other: &Timespec) -> bool { !(*self).eq(other) }
+    pure fn ne(&self, other: &Timespec) -> bool { !self.eq(other) }
 }
 
 /**
@@ -38,7 +46,7 @@ pub fn get_time() -> Timespec {
     let mut sec = 0i64;
     let mut nsec = 0i32;
     rustrt::get_time(&mut sec, &mut nsec);
-    return {sec: sec, nsec: nsec};
+    return Timespec::new(sec, nsec);
 }
 
 
@@ -65,7 +73,9 @@ pub fn tzset() {
     rustrt::rust_tzset();
 }
 
-type Tm_ = {
+#[auto_serialize]
+#[auto_deserialize]
+pub struct Tm {
     tm_sec: i32, // seconds after the minute ~[0-60]
     tm_min: i32, // minutes after the hour ~[0-59]
     tm_hour: i32, // hours after midnight ~[0-23]
@@ -78,37 +88,28 @@ type Tm_ = {
     tm_gmtoff: i32, // offset from UTC in seconds
     tm_zone: ~str, // timezone abbreviation
     tm_nsec: i32, // nanoseconds
-};
-
-impl Tm_ : Eq {
-    pure fn eq(&self, other: &Tm_) -> bool {
-        (*self).tm_sec == (*other).tm_sec &&
-        (*self).tm_min == (*other).tm_min &&
-        (*self).tm_hour == (*other).tm_hour &&
-        (*self).tm_mday == (*other).tm_mday &&
-        (*self).tm_mon == (*other).tm_mon &&
-        (*self).tm_year == (*other).tm_year &&
-        (*self).tm_wday == (*other).tm_wday &&
-        (*self).tm_yday == (*other).tm_yday &&
-        (*self).tm_isdst == (*other).tm_isdst &&
-        (*self).tm_gmtoff == (*other).tm_gmtoff &&
-        (*self).tm_zone == (*other).tm_zone &&
-        (*self).tm_nsec == (*other).tm_nsec
-    }
-    pure fn ne(&self, other: &Tm_) -> bool { !(*self).eq(other) }
-}
-
-pub enum Tm {
-    Tm_(Tm_)
 }
 
 impl Tm : Eq {
-    pure fn eq(&self, other: &Tm) -> bool { *(*self) == *(*other) }
-    pure fn ne(&self, other: &Tm) -> bool { *(*self) != *(*other) }
+    pure fn eq(&self, other: &Tm) -> bool {
+        self.tm_sec == (*other).tm_sec &&
+        self.tm_min == (*other).tm_min &&
+        self.tm_hour == (*other).tm_hour &&
+        self.tm_mday == (*other).tm_mday &&
+        self.tm_mon == (*other).tm_mon &&
+        self.tm_year == (*other).tm_year &&
+        self.tm_wday == (*other).tm_wday &&
+        self.tm_yday == (*other).tm_yday &&
+        self.tm_isdst == (*other).tm_isdst &&
+        self.tm_gmtoff == (*other).tm_gmtoff &&
+        self.tm_zone == (*other).tm_zone &&
+        self.tm_nsec == (*other).tm_nsec
+    }
+    pure fn ne(&self, other: &Tm) -> bool { !self.eq(other) }
 }
 
 pub pure fn empty_tm() -> Tm {
-    Tm_({
+    Tm {
         tm_sec: 0_i32,
         tm_min: 0_i32,
         tm_hour: 0_i32,
@@ -121,15 +122,15 @@ pub pure fn empty_tm() -> Tm {
         tm_gmtoff: 0_i32,
         tm_zone: ~"",
         tm_nsec: 0_i32,
-    })
+    }
 }
 
 /// Returns the specified time in UTC
 pub fn at_utc(clock: Timespec) -> Tm {
-    let mut {sec, nsec} = clock;
+    let mut Timespec { sec, nsec } = clock;
     let mut tm = empty_tm();
     rustrt::rust_gmtime(sec, nsec, tm);
-    tm
+    move tm
 }
 
 /// Returns the current time in UTC
@@ -139,10 +140,10 @@ pub fn now_utc() -> Tm {
 
 /// Returns the specified time in the local timezone
 pub fn at(clock: Timespec) -> Tm {
-    let mut {sec, nsec} = clock;
+    let mut Timespec { sec, nsec } = clock;
     let mut tm = empty_tm();
     rustrt::rust_localtime(sec, nsec, tm);
-    tm
+    move tm
 }
 
 /// Returns the current time in the local timezone
@@ -158,10 +159,10 @@ pub pure fn strptime(s: &str, format: &str) -> Result<Tm, ~str> {
 }
 
 /// Formats the time according to the format string.
-pub pure fn strftime(format: &str, tm: Tm) -> ~str {
+pub pure fn strftime(format: &str, tm: &Tm) -> ~str {
     // unsafe only because do_strftime is annoying to make pure
     // (it does IO with a str_reader)
-    unsafe {do_strftime(format, tm)}
+    move unsafe { do_strftime(format, tm) }
 }
 
 impl Tm {
@@ -173,7 +174,7 @@ impl Tm {
         } else {
             rustrt::rust_mktime(self, &mut sec);
         }
-        { sec: sec, nsec: self.tm_nsec }
+        Timespec::new(sec, self.tm_nsec)
     }
 
     /// Convert time to the local timezone
@@ -193,7 +194,9 @@ impl Tm {
     pure fn ctime() -> ~str { self.strftime(~"%c") }
 
     /// Formats the time according to the format string.
-    pure fn strftime(format: &str) -> ~str { strftime(format, self) }
+    pure fn strftime(&self, format: &str) -> ~str {
+        move strftime(format, self)
+    }
 
     /**
      * Returns a time string formatted according to RFC 822.
@@ -254,9 +257,9 @@ priv fn do_strptime(s: &str, format: &str) -> Result<Tm, ~str> {
     fn match_strs(ss: &str, pos: uint, strs: &[(~str, i32)])
       -> Option<(i32, uint)> {
         let mut i = 0u;
-        let len = vec::len(strs);
+        let len = strs.len();
         while i < len {
-            let (needle, value) = strs[i];
+            let &(needle, value) = &strs[i];
 
             if match_str(ss, pos, needle) {
                 return Some((value, pos + str::len(needle)));
@@ -302,7 +305,7 @@ priv fn do_strptime(s: &str, format: &str) -> Result<Tm, ~str> {
         }
     }
 
-    fn parse_type(s: &str, pos: uint, ch: char, tm: &mut Tm_)
+    fn parse_type(s: &str, pos: uint, ch: char, tm: &mut Tm)
       -> Result<uint, ~str> {
         match ch {
           'A' => match match_strs(s, pos, ~[
@@ -372,22 +375,36 @@ priv fn do_strptime(s: &str, format: &str) -> Result<Tm, ~str> {
             None => Err(~"Invalid year")
           },
           'c' => {
-            parse_type(s, pos, 'a', tm)
-                .chain(|pos| parse_char(s, pos, ' '))
-                .chain(|pos| parse_type(s, pos, 'b', tm))
-                .chain(|pos| parse_char(s, pos, ' '))
-                .chain(|pos| parse_type(s, pos, 'e', tm))
-                .chain(|pos| parse_char(s, pos, ' '))
-                .chain(|pos| parse_type(s, pos, 'T', tm))
-                .chain(|pos| parse_char(s, pos, ' '))
-                .chain(|pos| parse_type(s, pos, 'Y', tm))
+                // FIXME(#3724): cleanup
+                result::chain(
+                result::chain(
+                result::chain(
+                result::chain(
+                result::chain(
+                result::chain(
+                result::chain(
+                result::chain(
+                    move parse_type(s, pos, 'a', tm),
+                    |pos| parse_char(s, pos, ' ')),
+                    |pos| parse_type(s, pos, 'b', tm)),
+                    |pos| parse_char(s, pos, ' ')),
+                    |pos| parse_type(s, pos, 'e', tm)),
+                    |pos| parse_char(s, pos, ' ')),
+                    |pos| parse_type(s, pos, 'T', tm)),
+                    |pos| parse_char(s, pos, ' ')),
+                    |pos| parse_type(s, pos, 'Y', tm))
           }
           'D' | 'x' => {
-            parse_type(s, pos, 'm', tm)
-                .chain(|pos| parse_char(s, pos, '/'))
-                .chain(|pos| parse_type(s, pos, 'd', tm))
-                .chain(|pos| parse_char(s, pos, '/'))
-                .chain(|pos| parse_type(s, pos, 'y', tm))
+                // FIXME(#3724): cleanup
+                result::chain(
+                result::chain(
+                result::chain(
+                result::chain(
+                    move parse_type(s, pos, 'm', tm),
+                    |pos| parse_char(s, pos, '/')),
+                    |pos| parse_type(s, pos, 'd', tm)),
+                    |pos| parse_char(s, pos, '/')),
+                    |pos| parse_type(s, pos, 'y', tm))
           }
           'd' => match match_digits(s, pos, 2u, false) {
             Some(item) => { let (v, pos) = item; tm.tm_mday = v; Ok(pos) }
@@ -398,11 +415,16 @@ priv fn do_strptime(s: &str, format: &str) -> Result<Tm, ~str> {
             None => Err(~"Invalid day of the month")
           },
           'F' => {
-            parse_type(s, pos, 'Y', tm)
-                .chain(|pos| parse_char(s, pos, '-'))
-                .chain(|pos| parse_type(s, pos, 'm', tm))
-                .chain(|pos| parse_char(s, pos, '-'))
-                .chain(|pos| parse_type(s, pos, 'd', tm))
+                // FIXME(#3724): cleanup
+                result::chain(
+                result::chain(
+                result::chain(
+                result::chain(
+                    move parse_type(s, pos, 'Y', tm),
+                    |pos| parse_char(s, pos, '-')),
+                    |pos| parse_type(s, pos, 'm', tm)),
+                    |pos| parse_char(s, pos, '-')),
+                    |pos| parse_type(s, pos, 'd', tm))
           }
           'H' => {
             // FIXME (#2350): range check.
@@ -483,18 +505,28 @@ priv fn do_strptime(s: &str, format: &str) -> Result<Tm, ~str> {
             None => Err(~"Invalid hour")
           },
           'R' => {
-            parse_type(s, pos, 'H', tm)
-                .chain(|pos| parse_char(s, pos, ':'))
-                .chain(|pos| parse_type(s, pos, 'M', tm))
+                // FIXME(#3724): cleanup
+                result::chain(
+                result::chain(
+                    move parse_type(s, pos, 'H', tm),
+                    |pos| parse_char(s, pos, ':')),
+                    |pos| parse_type(s, pos, 'M', tm))
           }
           'r' => {
-            parse_type(s, pos, 'I', tm)
-                .chain(|pos| parse_char(s, pos, ':'))
-                .chain(|pos| parse_type(s, pos, 'M', tm))
-                .chain(|pos| parse_char(s, pos, ':'))
-                .chain(|pos| parse_type(s, pos, 'S', tm))
-                .chain(|pos| parse_char(s, pos, ' '))
-                .chain(|pos| parse_type(s, pos, 'p', tm))
+                // FIXME(#3724): cleanup
+                result::chain(
+                result::chain(
+                result::chain(
+                result::chain(
+                result::chain(
+                result::chain(
+                    move parse_type(s, pos, 'I', tm),
+                    |pos| parse_char(s, pos, ':')),
+                    |pos| parse_type(s, pos, 'M', tm)),
+                    |pos| parse_char(s, pos, ':')),
+                    |pos| parse_type(s, pos, 'S', tm)),
+                    |pos| parse_char(s, pos, ' ')),
+                    |pos| parse_type(s, pos, 'p', tm))
           }
           'S' => {
             // FIXME (#2350): range check.
@@ -509,11 +541,16 @@ priv fn do_strptime(s: &str, format: &str) -> Result<Tm, ~str> {
           }
           //'s' {}
           'T' | 'X' => {
-            parse_type(s, pos, 'H', tm)
-                .chain(|pos| parse_char(s, pos, ':'))
-                .chain(|pos| parse_type(s, pos, 'M', tm))
-                .chain(|pos| parse_char(s, pos, ':'))
-                .chain(|pos| parse_type(s, pos, 'S', tm))
+                // FIXME(#3724): cleanup
+                result::chain(
+                result::chain(
+                result::chain(
+                result::chain(
+                    move parse_type(s, pos, 'H', tm),
+                    |pos| parse_char(s, pos, ':')),
+                    |pos| parse_type(s, pos, 'M', tm)),
+                    |pos| parse_char(s, pos, ':')),
+                    |pos| parse_type(s, pos, 'S', tm))
           }
           't' => parse_char(s, pos, '\t'),
           'u' => {
@@ -528,11 +565,16 @@ priv fn do_strptime(s: &str, format: &str) -> Result<Tm, ~str> {
             }
           }
           'v' => {
-            parse_type(s, pos, 'e', tm)
-                .chain(|pos| parse_char(s, pos, '-'))
-                .chain(|pos| parse_type(s, pos, 'b', tm))
-                .chain(|pos| parse_char(s, pos, '-'))
-                .chain(|pos| parse_type(s, pos, 'Y', tm))
+                // FIXME(#3724): cleanup
+                result::chain(
+                result::chain(
+                result::chain(
+                result::chain(
+                    move parse_type(s, pos, 'e', tm),
+                    |pos| parse_char(s, pos, '-')),
+                    |pos| parse_type(s, pos, 'b', tm)),
+                    |pos| parse_char(s, pos, '-')),
+                    |pos| parse_type(s, pos, 'Y', tm))
           }
           //'W' {}
           'w' => {
@@ -613,7 +655,7 @@ priv fn do_strptime(s: &str, format: &str) -> Result<Tm, ~str> {
     }
 
     do io::with_str_reader(str::from_slice(format)) |rdr| {
-        let mut tm = {
+        let mut tm = Tm {
             tm_sec: 0_i32,
             tm_min: 0_i32,
             tm_hour: 0_i32,
@@ -635,19 +677,21 @@ priv fn do_strptime(s: &str, format: &str) -> Result<Tm, ~str> {
             let {ch, next} = str::char_range_at(s, pos);
 
             match rdr.read_char() {
-              '%' => match parse_type(s, pos, rdr.read_char(), &mut tm) {
-                Ok(next) => pos = next,
-                  Err(copy e) => { result = Err(e); break; }
-              },
-              c => {
-                if c != ch { break }
-                pos = next;
-              }
+                '%' => {
+                    match parse_type(s, pos, rdr.read_char(), &mut tm) {
+                        Ok(next) => pos = next,
+                        Err(move e) => { result = Err(move e); break; }
+                    }
+                },
+                c => {
+                    if c != ch { break }
+                    pos = next;
+                }
             }
         }
 
         if pos == len && rdr.eof() {
-            Ok(Tm_({
+            Ok(Tm {
                 tm_sec: tm.tm_sec,
                 tm_min: tm.tm_min,
                 tm_hour: tm.tm_hour,
@@ -658,14 +702,14 @@ priv fn do_strptime(s: &str, format: &str) -> Result<Tm, ~str> {
                 tm_yday: tm.tm_yday,
                 tm_isdst: tm.tm_isdst,
                 tm_gmtoff: tm.tm_gmtoff,
-                tm_zone: tm.tm_zone,
+                tm_zone: copy tm.tm_zone,
                 tm_nsec: tm.tm_nsec,
-            }))
-        } else { result }
+            })
+        } else { move result }
     }
 }
 
-priv fn do_strftime(format: &str, tm: Tm) -> ~str {
+priv fn do_strftime(format: &str, tm: &Tm) -> ~str {
     fn parse_type(ch: char, tm: &Tm) -> ~str {
         //FIXME (#2350): Implement missing types.
       let die = || fmt!("strftime: can't understand this format %c ", ch);
@@ -804,7 +848,7 @@ priv fn do_strftime(format: &str, tm: Tm) -> ~str {
           //'x' {}
           'Y' => int::str(tm.tm_year as int + 1900),
           'y' => fmt!("%02d", (tm.tm_year as int + 1900) % 100),
-          'Z' => tm.tm_zone,
+          'Z' => copy tm.tm_zone,
           'z' => {
             let sign = if tm.tm_gmtoff > 0_i32 { '+' } else { '-' };
             let mut m = i32::abs(tm.tm_gmtoff) / 60_i32;
@@ -823,13 +867,13 @@ priv fn do_strftime(format: &str, tm: Tm) -> ~str {
     do io::with_str_reader(str::from_slice(format)) |rdr| {
         while !rdr.eof() {
             match rdr.read_char() {
-                '%' => buf += parse_type(rdr.read_char(), &tm),
+                '%' => buf += parse_type(rdr.read_char(), tm),
                 ch => str::push_char(&mut buf, ch)
             }
         }
     }
 
-    buf
+    move buf
 }
 
 #[cfg(test)]
@@ -883,7 +927,7 @@ mod tests {
         os::setenv(~"TZ", ~"America/Los_Angeles");
         tzset();
 
-        let time = { sec: 1234567890_i64, nsec: 54321_i32 };
+        let time = Timespec::new(1234567890, 54321);
         let utc = at_utc(time);
 
         assert utc.tm_sec == 30_i32;
@@ -905,7 +949,7 @@ mod tests {
         os::setenv(~"TZ", ~"America/Los_Angeles");
         tzset();
 
-        let time = { sec: 1234567890_i64, nsec: 54321_i32 };
+        let time = Timespec::new(1234567890, 54321);
         let local = at(time);
 
         error!("time_at: %?", local);
@@ -923,8 +967,8 @@ mod tests {
 
         // FIXME (#2350): We should probably standardize on the timezone
         // abbreviation.
-        let zone = local.tm_zone;
-        assert zone == ~"PST" || zone == ~"Pacific Standard Time";
+        let zone = &local.tm_zone;
+        assert *zone == ~"PST" || *zone == ~"Pacific Standard Time";
 
         assert local.tm_nsec == 54321_i32;
     }
@@ -934,7 +978,7 @@ mod tests {
         os::setenv(~"TZ", ~"America/Los_Angeles");
         tzset();
 
-        let time = { sec: 1234567890_i64, nsec: 54321_i32 };
+        let time = Timespec::new(1234567890, 54321);
         let utc = at_utc(time);
 
         assert utc.to_timespec() == time;
@@ -946,7 +990,7 @@ mod tests {
         os::setenv(~"TZ", ~"America/Los_Angeles");
         tzset();
 
-        let time = { sec: 1234567890_i64, nsec: 54321_i32 };
+        let time = Timespec::new(1234567890, 54321);
         let utc = at_utc(time);
         let local = at(time);
 
@@ -1106,10 +1150,10 @@ mod tests {
         assert test(~"6", ~"%w");
         assert test(~"2009", ~"%Y");
         assert test(~"09", ~"%y");
-        assert strptime(~"UTC", ~"%Z").get().tm_zone == ~"UTC";
-        assert strptime(~"PST", ~"%Z").get().tm_zone == ~"";
-        assert strptime(~"-0000", ~"%z").get().tm_gmtoff == 0_i32;
-        assert strptime(~"-0800", ~"%z").get().tm_gmtoff == 0_i32;
+        assert result::unwrap(strptime(~"UTC", ~"%Z")).tm_zone == ~"UTC";
+        assert result::unwrap(strptime(~"PST", ~"%Z")).tm_zone == ~"";
+        assert result::unwrap(strptime(~"-0000", ~"%z")).tm_gmtoff == 0;
+        assert result::unwrap(strptime(~"-0800", ~"%z")).tm_gmtoff == 0;
         assert test(~"%", ~"%%");
     }
 
@@ -1118,7 +1162,7 @@ mod tests {
         os::setenv(~"TZ", ~"America/Los_Angeles");
         tzset();
 
-        let time = { sec: 1234567890_i64, nsec: 54321_i32 };
+        let time = Timespec::new(1234567890, 54321);
         let utc   = at_utc(time);
         let local = at(time);
 
@@ -1133,7 +1177,7 @@ mod tests {
         os::setenv(~"TZ", ~"America/Los_Angeles");
         tzset();
 
-        let time = { sec: 1234567890_i64, nsec: 54321_i32 };
+        let time = Timespec::new(1234567890, 54321);
         let utc = at_utc(time);
         let local = at(time);