|
| 1 | +import libc::{c_char, c_int, c_long, size_t, time_t}; |
| 2 | +import io::{reader, reader_util}; |
| 3 | +import result::{result, ok, err, extensions}; |
| 4 | + |
| 5 | +export |
| 6 | + timespec, |
| 7 | + get_time, |
| 8 | + precise_time_ns, |
| 9 | + precise_time_s, |
| 10 | + tm, |
| 11 | + empty_tm, |
| 12 | + now, |
| 13 | + at, |
| 14 | + now_utc, |
| 15 | + at_utc; |
| 16 | + |
1 | 17 | #[abi = "cdecl"]
|
2 | 18 | native mod rustrt {
|
3 | 19 | fn get_time(&sec: i64, &nsec: i32);
|
4 | 20 | fn precise_time_ns(&ns: u64);
|
| 21 | + |
| 22 | + // FIXME: The i64 values can be passed by-val when #2064 is fixed. |
| 23 | + fn rust_gmtime(&&sec: i64, &&nsec: i32, &&result: tm); |
| 24 | + fn rust_localtime(&&sec: i64, &&nsec: i32, &result: tm); |
| 25 | + fn rust_timegm(&&tm: tm, &sec: i64); |
| 26 | + fn rust_mktime(&&tm: tm, &sec: i64); |
5 | 27 | }
|
6 | 28 |
|
7 | 29 | #[doc = "A record specifying a time value in seconds and microseconds."]
|
@@ -36,6 +58,87 @@ fn precise_time_s() -> float {
|
36 | 58 | ret (precise_time_ns() as float) / 1000000000.;
|
37 | 59 | }
|
38 | 60 |
|
| 61 | +type tm = { |
| 62 | + tm_sec: i32, // seconds after the minute [0-60] |
| 63 | + tm_min: i32, // minutes after the hour [0-59] |
| 64 | + tm_hour: i32, // hours after midnight [0-23] |
| 65 | + tm_mday: i32, // days of the month [1-31] |
| 66 | + tm_mon: i32, // months since January [0-11] |
| 67 | + tm_year: i32, // years since 1900 |
| 68 | + tm_wday: i32, // days since Sunday [0-6] |
| 69 | + tm_yday: i32, // days since January 1 [0-365] |
| 70 | + tm_isdst: i32, // Daylight Savings Time flag |
| 71 | + tm_gmtoff: i32, // offset from UTC in seconds |
| 72 | + tm_zone: str, // timezone abbreviation |
| 73 | + tm_nsec: i32, // nanoseconds |
| 74 | +}; |
| 75 | + |
| 76 | +fn empty_tm() -> tm { |
| 77 | + { |
| 78 | + tm_sec: 0_i32, |
| 79 | + tm_min: 0_i32, |
| 80 | + tm_hour: 0_i32, |
| 81 | + tm_mday: 0_i32, |
| 82 | + tm_mon: 0_i32, |
| 83 | + tm_year: 0_i32, |
| 84 | + tm_wday: 0_i32, |
| 85 | + tm_yday: 0_i32, |
| 86 | + tm_isdst: 0_i32, |
| 87 | + tm_gmtoff: 0_i32, |
| 88 | + tm_zone: "", |
| 89 | + tm_nsec: 0_i32, |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +#[doc = "Returns the specified time in UTC"] |
| 94 | +fn at_utc(clock: timespec) -> tm { |
| 95 | + let mut {sec, nsec} = clock; |
| 96 | + let mut tm = empty_tm(); |
| 97 | + rustrt::rust_gmtime(sec, nsec, tm); |
| 98 | + tm |
| 99 | +} |
| 100 | + |
| 101 | +#[doc = "Returns the current time in UTC"] |
| 102 | +fn now_utc() -> tm { |
| 103 | + at_utc(get_time()) |
| 104 | +} |
| 105 | + |
| 106 | +#[doc = "Returns the specified time in the local timezone"] |
| 107 | +fn at(clock: timespec) -> tm { |
| 108 | + let mut {sec, nsec} = clock; |
| 109 | + let mut tm = empty_tm(); |
| 110 | + rustrt::rust_localtime(sec, nsec, tm); |
| 111 | + tm |
| 112 | +} |
| 113 | + |
| 114 | +#[doc = "Returns the current time in the local timezone"] |
| 115 | +fn now() -> tm { |
| 116 | + at(get_time()) |
| 117 | +} |
| 118 | + |
| 119 | +impl tm for tm { |
| 120 | + #[doc = "Convert time to the seconds from January 1, 1970"] |
| 121 | + fn to_timespec() -> timespec { |
| 122 | + let mut sec = 0i64; |
| 123 | + if self.tm_gmtoff == 0_i32 { |
| 124 | + rustrt::rust_timegm(self, sec); |
| 125 | + } else { |
| 126 | + rustrt::rust_mktime(self, sec); |
| 127 | + } |
| 128 | + { sec: sec, nsec: self.tm_nsec } |
| 129 | + } |
| 130 | + |
| 131 | + #[doc = "Convert time to the local timezone"] |
| 132 | + fn to_local() -> tm { |
| 133 | + at(self.to_timespec()) |
| 134 | + } |
| 135 | + |
| 136 | + #[doc = "Convert time to the UTC"] |
| 137 | + fn to_utc() -> tm { |
| 138 | + at_utc(self.to_timespec()) |
| 139 | + } |
| 140 | +} |
| 141 | + |
39 | 142 | #[cfg(test)]
|
40 | 143 | mod tests {
|
41 | 144 | import task;
|
@@ -81,4 +184,78 @@ mod tests {
|
81 | 184 | log(debug, "ns2=" + u64::str(ns2) + " ns");
|
82 | 185 | assert ns2 >= ns1;
|
83 | 186 | }
|
| 187 | + |
| 188 | + #[test] |
| 189 | + fn test_at_utc() { |
| 190 | + os::setenv("TZ", "America/Los_Angeles"); |
| 191 | + |
| 192 | + let time = { sec: 1234567890_i64, nsec: 54321_i32 }; |
| 193 | + let utc = at_utc(time); |
| 194 | + |
| 195 | + assert utc.tm_sec == 30_i32; |
| 196 | + assert utc.tm_min == 31_i32; |
| 197 | + assert utc.tm_hour == 23_i32; |
| 198 | + assert utc.tm_mday == 13_i32; |
| 199 | + assert utc.tm_mon == 1_i32; |
| 200 | + assert utc.tm_year == 109_i32; |
| 201 | + assert utc.tm_wday == 5_i32; |
| 202 | + assert utc.tm_yday == 43_i32; |
| 203 | + assert utc.tm_isdst == 0_i32; |
| 204 | + assert utc.tm_gmtoff == 0_i32; |
| 205 | + assert utc.tm_zone == "UTC"; |
| 206 | + assert utc.tm_nsec == 54321_i32; |
| 207 | + } |
| 208 | + |
| 209 | + #[test] |
| 210 | + fn test_at() { |
| 211 | + os::setenv("TZ", "America/Los_Angeles"); |
| 212 | + |
| 213 | + let time = { sec: 1234567890_i64, nsec: 54321_i32 }; |
| 214 | + let local = at(time); |
| 215 | + |
| 216 | + assert local.tm_sec == 30_i32; |
| 217 | + assert local.tm_min == 31_i32; |
| 218 | + assert local.tm_hour == 15_i32; |
| 219 | + assert local.tm_mday == 13_i32; |
| 220 | + assert local.tm_mon == 1_i32; |
| 221 | + assert local.tm_year == 109_i32; |
| 222 | + assert local.tm_wday == 5_i32; |
| 223 | + assert local.tm_yday == 43_i32; |
| 224 | + assert local.tm_isdst == 0_i32; |
| 225 | + assert local.tm_gmtoff == -28800_i32; |
| 226 | + |
| 227 | + // FIXME: We should probably standardize on the timezone |
| 228 | + // abbreviation. |
| 229 | + let zone = local.tm_zone; |
| 230 | + assert zone == "PST" || zone == "Pacific Standard Time"; |
| 231 | + |
| 232 | + assert local.tm_nsec == 54321_i32; |
| 233 | + } |
| 234 | + |
| 235 | + #[test] |
| 236 | + fn test_to_timespec() { |
| 237 | + os::setenv("TZ", "America/Los_Angeles"); |
| 238 | + |
| 239 | + let time = { sec: 1234567890_i64, nsec: 54321_i32 }; |
| 240 | + let utc = at_utc(time); |
| 241 | + |
| 242 | + assert utc.to_timespec() == time; |
| 243 | + assert utc.to_local().to_timespec() == time; |
| 244 | + } |
| 245 | + |
| 246 | + #[test] |
| 247 | + fn test_conversions() { |
| 248 | + os::setenv("TZ", "America/Los_Angeles"); |
| 249 | + |
| 250 | + let time = { sec: 1234567890_i64, nsec: 54321_i32 }; |
| 251 | + let utc = at_utc(time); |
| 252 | + let local = at(time); |
| 253 | + |
| 254 | + assert local.to_local() == local; |
| 255 | + assert local.to_utc() == utc; |
| 256 | + assert local.to_utc().to_local() == local; |
| 257 | + assert utc.to_utc() == utc; |
| 258 | + assert utc.to_local() == local; |
| 259 | + assert utc.to_local().to_utc() == utc; |
| 260 | + } |
84 | 261 | }
|
0 commit comments