1 // Copyright 2012-2014 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 //! Temporal quantification
16 use ops::{Add, Sub, Mul, Div, Neg, FnOnce};
18 use option::Option::{Some, None};
20 use result::Result::Ok;
22 // NOTE: for old macros; remove after the next snapshot
23 #[cfg(stage0)] use result::Result::Err;
25 /// The number of nanoseconds in a microsecond.
26 const NANOS_PER_MICRO: i32 = 1000;
27 /// The number of nanoseconds in a millisecond.
28 const NANOS_PER_MILLI: i32 = 1000_000;
29 /// The number of nanoseconds in seconds.
30 const NANOS_PER_SEC: i32 = 1_000_000_000;
31 /// The number of microseconds per second.
32 const MICROS_PER_SEC: i64 = 1000_000;
33 /// The number of milliseconds per second.
34 const MILLIS_PER_SEC: i64 = 1000;
35 /// The number of seconds in a minute.
36 const SECS_PER_MINUTE: i64 = 60;
37 /// The number of seconds in an hour.
38 const SECS_PER_HOUR: i64 = 3600;
39 /// The number of (non-leap) seconds in days.
40 const SECS_PER_DAY: i64 = 86400;
41 /// The number of (non-leap) seconds in a week.
42 const SECS_PER_WEEK: i64 = 604800;
44 macro_rules! try_opt {
45 ($e:expr) => (match $e { Some(v) => v, None => return None })
49 /// ISO 8601 time duration with nanosecond precision.
50 /// This also allows for the negative duration; see individual methods for details.
51 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
54 nanos: i32, // Always 0 <= nanos < NANOS_PER_SEC
57 /// The minimum possible `Duration`: `i64::MIN` milliseconds.
58 pub const MIN: Duration = Duration {
59 secs: i64::MIN / MILLIS_PER_SEC - 1,
60 nanos: NANOS_PER_SEC + (i64::MIN % MILLIS_PER_SEC) as i32 * NANOS_PER_MILLI
63 /// The maximum possible `Duration`: `i64::MAX` milliseconds.
64 pub const MAX: Duration = Duration {
65 secs: i64::MAX / MILLIS_PER_SEC,
66 nanos: (i64::MAX % MILLIS_PER_SEC) as i32 * NANOS_PER_MILLI
70 /// Makes a new `Duration` with given number of weeks.
71 /// Equivalent to `Duration::seconds(weeks * 7 * 24 * 60 * 60), with overflow checks.
72 /// Panics when the duration is out of bounds.
74 pub fn weeks(weeks: i64) -> Duration {
75 let secs = weeks.checked_mul(SECS_PER_WEEK).expect("Duration::weeks out of bounds");
76 Duration::seconds(secs)
79 /// Makes a new `Duration` with given number of days.
80 /// Equivalent to `Duration::seconds(days * 24 * 60 * 60)` with overflow checks.
81 /// Panics when the duration is out of bounds.
83 pub fn days(days: i64) -> Duration {
84 let secs = days.checked_mul(SECS_PER_DAY).expect("Duration::days out of bounds");
85 Duration::seconds(secs)
88 /// Makes a new `Duration` with given number of hours.
89 /// Equivalent to `Duration::seconds(hours * 60 * 60)` with overflow checks.
90 /// Panics when the duration is out of bounds.
92 pub fn hours(hours: i64) -> Duration {
93 let secs = hours.checked_mul(SECS_PER_HOUR).expect("Duration::hours ouf of bounds");
94 Duration::seconds(secs)
97 /// Makes a new `Duration` with given number of minutes.
98 /// Equivalent to `Duration::seconds(minutes * 60)` with overflow checks.
99 /// Panics when the duration is out of bounds.
101 pub fn minutes(minutes: i64) -> Duration {
102 let secs = minutes.checked_mul(SECS_PER_MINUTE).expect("Duration::minutes out of bounds");
103 Duration::seconds(secs)
106 /// Makes a new `Duration` with given number of seconds.
107 /// Panics when the duration is more than `i64::MAX` milliseconds
108 /// or less than `i64::MIN` milliseconds.
110 pub fn seconds(seconds: i64) -> Duration {
111 let d = Duration { secs: seconds, nanos: 0 };
112 if d < MIN || d > MAX {
113 panic!("Duration::seconds out of bounds");
118 /// Makes a new `Duration` with given number of milliseconds.
120 pub fn milliseconds(milliseconds: i64) -> Duration {
121 let (secs, millis) = div_mod_floor_64(milliseconds, MILLIS_PER_SEC);
122 let nanos = millis as i32 * NANOS_PER_MILLI;
123 Duration { secs: secs, nanos: nanos }
126 /// Makes a new `Duration` with given number of microseconds.
128 pub fn microseconds(microseconds: i64) -> Duration {
129 let (secs, micros) = div_mod_floor_64(microseconds, MICROS_PER_SEC);
130 let nanos = micros as i32 * NANOS_PER_MICRO;
131 Duration { secs: secs, nanos: nanos }
134 /// Makes a new `Duration` with given number of nanoseconds.
136 pub fn nanoseconds(nanos: i64) -> Duration {
137 let (secs, nanos) = div_mod_floor_64(nanos, NANOS_PER_SEC as i64);
138 Duration { secs: secs, nanos: nanos as i32 }
141 /// Runs a closure, returning the duration of time it took to run the
143 pub fn span<F>(f: F) -> Duration where F: FnOnce() {
144 let before = super::precise_time_ns();
146 Duration::nanoseconds((super::precise_time_ns() - before) as i64)
149 /// Returns the total number of whole weeks in the duration.
151 pub fn num_weeks(&self) -> i64 {
155 /// Returns the total number of whole days in the duration.
156 pub fn num_days(&self) -> i64 {
157 self.num_seconds() / SECS_PER_DAY
160 /// Returns the total number of whole hours in the duration.
162 pub fn num_hours(&self) -> i64 {
163 self.num_seconds() / SECS_PER_HOUR
166 /// Returns the total number of whole minutes in the duration.
168 pub fn num_minutes(&self) -> i64 {
169 self.num_seconds() / SECS_PER_MINUTE
172 /// Returns the total number of whole seconds in the duration.
173 pub fn num_seconds(&self) -> i64 {
174 // If secs is negative, nanos should be subtracted from the duration.
175 if self.secs < 0 && self.nanos > 0 {
182 /// Returns the number of nanoseconds such that
183 /// `nanos_mod_sec() + num_seconds() * NANOS_PER_SEC` is the total number of
184 /// nanoseconds in the duration.
185 fn nanos_mod_sec(&self) -> i32 {
186 if self.secs < 0 && self.nanos > 0 {
187 self.nanos - NANOS_PER_SEC
193 /// Returns the total number of whole milliseconds in the duration,
194 pub fn num_milliseconds(&self) -> i64 {
195 // A proper Duration will not overflow, because MIN and MAX are defined
196 // such that the range is exactly i64 milliseconds.
197 let secs_part = self.num_seconds() * MILLIS_PER_SEC;
198 let nanos_part = self.nanos_mod_sec() / NANOS_PER_MILLI;
199 secs_part + nanos_part as i64
202 /// Returns the total number of whole microseconds in the duration,
203 /// or `None` on overflow (exceeding 2^63 microseconds in either direction).
204 pub fn num_microseconds(&self) -> Option<i64> {
205 let secs_part = try_opt!(self.num_seconds().checked_mul(MICROS_PER_SEC));
206 let nanos_part = self.nanos_mod_sec() / NANOS_PER_MICRO;
207 secs_part.checked_add(nanos_part as i64)
210 /// Returns the total number of whole nanoseconds in the duration,
211 /// or `None` on overflow (exceeding 2^63 nanoseconds in either direction).
212 pub fn num_nanoseconds(&self) -> Option<i64> {
213 let secs_part = try_opt!(self.num_seconds().checked_mul(NANOS_PER_SEC as i64));
214 let nanos_part = self.nanos_mod_sec();
215 secs_part.checked_add(nanos_part as i64)
218 /// Add two durations, returning `None` if overflow occured.
219 pub fn checked_add(&self, rhs: &Duration) -> Option<Duration> {
220 let mut secs = try_opt!(self.secs.checked_add(rhs.secs));
221 let mut nanos = self.nanos + rhs.nanos;
222 if nanos >= NANOS_PER_SEC {
223 nanos -= NANOS_PER_SEC;
224 secs = try_opt!(secs.checked_add(1));
226 let d = Duration { secs: secs, nanos: nanos };
227 // Even if d is within the bounds of i64 seconds,
228 // it might still overflow i64 milliseconds.
229 if d < MIN || d > MAX { None } else { Some(d) }
232 /// Subtract two durations, returning `None` if overflow occured.
233 pub fn checked_sub(&self, rhs: &Duration) -> Option<Duration> {
234 let mut secs = try_opt!(self.secs.checked_sub(rhs.secs));
235 let mut nanos = self.nanos - rhs.nanos;
237 nanos += NANOS_PER_SEC;
238 secs = try_opt!(secs.checked_sub(1));
240 let d = Duration { secs: secs, nanos: nanos };
241 // Even if d is within the bounds of i64 seconds,
242 // it might still overflow i64 milliseconds.
243 if d < MIN || d > MAX { None } else { Some(d) }
246 /// The minimum possible `Duration`: `i64::MIN` milliseconds.
248 pub fn min_value() -> Duration { MIN }
250 /// The maximum possible `Duration`: `i64::MAX` milliseconds.
252 pub fn max_value() -> Duration { MAX }
254 /// A duration where the stored seconds and nanoseconds are equal to zero.
256 pub fn zero() -> Duration {
257 Duration { secs: 0, nanos: 0 }
260 /// Returns `true` if the duration equals `Duration::zero()`.
262 pub fn is_zero(&self) -> bool {
263 self.secs == 0 && self.nanos == 0
267 impl Neg for Duration {
268 type Output = Duration;
271 fn neg(self) -> Duration {
273 Duration { secs: -self.secs, nanos: 0 }
275 Duration { secs: -self.secs - 1, nanos: NANOS_PER_SEC - self.nanos }
280 impl Add for Duration {
281 type Output = Duration;
283 fn add(self, rhs: Duration) -> Duration {
284 let mut secs = self.secs + rhs.secs;
285 let mut nanos = self.nanos + rhs.nanos;
286 if nanos >= NANOS_PER_SEC {
287 nanos -= NANOS_PER_SEC;
290 Duration { secs: secs, nanos: nanos }
294 impl Sub for Duration {
295 type Output = Duration;
297 fn sub(self, rhs: Duration) -> Duration {
298 let mut secs = self.secs - rhs.secs;
299 let mut nanos = self.nanos - rhs.nanos;
301 nanos += NANOS_PER_SEC;
304 Duration { secs: secs, nanos: nanos }
308 impl Mul<i32> for Duration {
309 type Output = Duration;
311 fn mul(self, rhs: i32) -> Duration {
312 // Multiply nanoseconds as i64, because it cannot overflow that way.
313 let total_nanos = self.nanos as i64 * rhs as i64;
314 let (extra_secs, nanos) = div_mod_floor_64(total_nanos, NANOS_PER_SEC as i64);
315 let secs = self.secs * rhs as i64 + extra_secs;
316 Duration { secs: secs, nanos: nanos as i32 }
320 impl Div<i32> for Duration {
321 type Output = Duration;
323 fn div(self, rhs: i32) -> Duration {
324 let mut secs = self.secs / rhs as i64;
325 let carry = self.secs - secs * rhs as i64;
326 let extra_nanos = carry * NANOS_PER_SEC as i64 / rhs as i64;
327 let mut nanos = self.nanos / rhs + extra_nanos as i32;
328 if nanos >= NANOS_PER_SEC {
329 nanos -= NANOS_PER_SEC;
333 nanos += NANOS_PER_SEC;
336 Duration { secs: secs, nanos: nanos }
340 impl fmt::Show for Duration {
341 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
342 // technically speaking, negative duration is not valid ISO 8601,
343 // but we need to print it anyway.
344 let (abs, sign) = if self.secs < 0 { (-*self, "-") } else { (*self, "") };
346 let days = abs.secs / SECS_PER_DAY;
347 let secs = abs.secs - days * SECS_PER_DAY;
348 let hasdate = days != 0;
349 let hastime = (secs != 0 || abs.nanos != 0) || !hasdate;
351 try!(write!(f, "{}P", sign));
354 try!(write!(f, "{}D", days));
358 try!(write!(f, "T{}S", secs));
359 } else if abs.nanos % NANOS_PER_MILLI == 0 {
360 try!(write!(f, "T{}.{:03}S", secs, abs.nanos / NANOS_PER_MILLI));
361 } else if abs.nanos % NANOS_PER_MICRO == 0 {
362 try!(write!(f, "T{}.{:06}S", secs, abs.nanos / NANOS_PER_MICRO));
364 try!(write!(f, "T{}.{:09}S", secs, abs.nanos));
371 // Copied from libnum
373 fn div_mod_floor_64(this: i64, other: i64) -> (i64, i64) {
374 (div_floor_64(this, other), mod_floor_64(this, other))
378 fn div_floor_64(this: i64, other: i64) -> i64 {
379 match div_rem_64(this, other) {
380 (d, r) if (r > 0 && other < 0)
381 || (r < 0 && other > 0) => d - 1,
387 fn mod_floor_64(this: i64, other: i64) -> i64 {
389 r if (r > 0 && other < 0)
390 || (r < 0 && other > 0) => r + other,
396 fn div_rem_64(this: i64, other: i64) -> (i64, i64) {
397 (this / other, this % other)
402 use super::{Duration, MIN, MAX};
404 use option::Option::{Some, None};
405 use string::ToString;
409 assert!(Duration::seconds(1) != Duration::zero());
410 assert_eq!(Duration::seconds(1) + Duration::seconds(2), Duration::seconds(3));
411 assert_eq!(Duration::seconds(86399) + Duration::seconds(4),
412 Duration::days(1) + Duration::seconds(3));
413 assert_eq!(Duration::days(10) - Duration::seconds(1000), Duration::seconds(863000));
414 assert_eq!(Duration::days(10) - Duration::seconds(1000000), Duration::seconds(-136000));
415 assert_eq!(Duration::days(2) + Duration::seconds(86399) +
416 Duration::nanoseconds(1234567890),
417 Duration::days(3) + Duration::nanoseconds(234567890));
418 assert_eq!(-Duration::days(3), Duration::days(-3));
419 assert_eq!(-(Duration::days(3) + Duration::seconds(70)),
420 Duration::days(-4) + Duration::seconds(86400-70));
424 fn test_duration_num_days() {
425 assert_eq!(Duration::zero().num_days(), 0);
426 assert_eq!(Duration::days(1).num_days(), 1);
427 assert_eq!(Duration::days(-1).num_days(), -1);
428 assert_eq!(Duration::seconds(86399).num_days(), 0);
429 assert_eq!(Duration::seconds(86401).num_days(), 1);
430 assert_eq!(Duration::seconds(-86399).num_days(), 0);
431 assert_eq!(Duration::seconds(-86401).num_days(), -1);
432 assert_eq!(Duration::days(i32::MAX as i64).num_days(), i32::MAX as i64);
433 assert_eq!(Duration::days(i32::MIN as i64).num_days(), i32::MIN as i64);
437 fn test_duration_num_seconds() {
438 assert_eq!(Duration::zero().num_seconds(), 0);
439 assert_eq!(Duration::seconds(1).num_seconds(), 1);
440 assert_eq!(Duration::seconds(-1).num_seconds(), -1);
441 assert_eq!(Duration::milliseconds(999).num_seconds(), 0);
442 assert_eq!(Duration::milliseconds(1001).num_seconds(), 1);
443 assert_eq!(Duration::milliseconds(-999).num_seconds(), 0);
444 assert_eq!(Duration::milliseconds(-1001).num_seconds(), -1);
448 fn test_duration_num_milliseconds() {
449 assert_eq!(Duration::zero().num_milliseconds(), 0);
450 assert_eq!(Duration::milliseconds(1).num_milliseconds(), 1);
451 assert_eq!(Duration::milliseconds(-1).num_milliseconds(), -1);
452 assert_eq!(Duration::microseconds(999).num_milliseconds(), 0);
453 assert_eq!(Duration::microseconds(1001).num_milliseconds(), 1);
454 assert_eq!(Duration::microseconds(-999).num_milliseconds(), 0);
455 assert_eq!(Duration::microseconds(-1001).num_milliseconds(), -1);
456 assert_eq!(Duration::milliseconds(i64::MAX).num_milliseconds(), i64::MAX);
457 assert_eq!(Duration::milliseconds(i64::MIN).num_milliseconds(), i64::MIN);
458 assert_eq!(MAX.num_milliseconds(), i64::MAX);
459 assert_eq!(MIN.num_milliseconds(), i64::MIN);
463 fn test_duration_num_microseconds() {
464 assert_eq!(Duration::zero().num_microseconds(), Some(0));
465 assert_eq!(Duration::microseconds(1).num_microseconds(), Some(1));
466 assert_eq!(Duration::microseconds(-1).num_microseconds(), Some(-1));
467 assert_eq!(Duration::nanoseconds(999).num_microseconds(), Some(0));
468 assert_eq!(Duration::nanoseconds(1001).num_microseconds(), Some(1));
469 assert_eq!(Duration::nanoseconds(-999).num_microseconds(), Some(0));
470 assert_eq!(Duration::nanoseconds(-1001).num_microseconds(), Some(-1));
471 assert_eq!(Duration::microseconds(i64::MAX).num_microseconds(), Some(i64::MAX));
472 assert_eq!(Duration::microseconds(i64::MIN).num_microseconds(), Some(i64::MIN));
473 assert_eq!(MAX.num_microseconds(), None);
474 assert_eq!(MIN.num_microseconds(), None);
477 const MICROS_PER_DAY: i64 = 86400_000_000;
478 assert_eq!(Duration::days(i64::MAX / MICROS_PER_DAY).num_microseconds(),
479 Some(i64::MAX / MICROS_PER_DAY * MICROS_PER_DAY));
480 assert_eq!(Duration::days(i64::MIN / MICROS_PER_DAY).num_microseconds(),
481 Some(i64::MIN / MICROS_PER_DAY * MICROS_PER_DAY));
482 assert_eq!(Duration::days(i64::MAX / MICROS_PER_DAY + 1).num_microseconds(), None);
483 assert_eq!(Duration::days(i64::MIN / MICROS_PER_DAY - 1).num_microseconds(), None);
487 fn test_duration_num_nanoseconds() {
488 assert_eq!(Duration::zero().num_nanoseconds(), Some(0));
489 assert_eq!(Duration::nanoseconds(1).num_nanoseconds(), Some(1));
490 assert_eq!(Duration::nanoseconds(-1).num_nanoseconds(), Some(-1));
491 assert_eq!(Duration::nanoseconds(i64::MAX).num_nanoseconds(), Some(i64::MAX));
492 assert_eq!(Duration::nanoseconds(i64::MIN).num_nanoseconds(), Some(i64::MIN));
493 assert_eq!(MAX.num_nanoseconds(), None);
494 assert_eq!(MIN.num_nanoseconds(), None);
497 const NANOS_PER_DAY: i64 = 86400_000_000_000;
498 assert_eq!(Duration::days(i64::MAX / NANOS_PER_DAY).num_nanoseconds(),
499 Some(i64::MAX / NANOS_PER_DAY * NANOS_PER_DAY));
500 assert_eq!(Duration::days(i64::MIN / NANOS_PER_DAY).num_nanoseconds(),
501 Some(i64::MIN / NANOS_PER_DAY * NANOS_PER_DAY));
502 assert_eq!(Duration::days(i64::MAX / NANOS_PER_DAY + 1).num_nanoseconds(), None);
503 assert_eq!(Duration::days(i64::MIN / NANOS_PER_DAY - 1).num_nanoseconds(), None);
507 fn test_duration_checked_ops() {
508 assert_eq!(Duration::milliseconds(i64::MAX - 1).checked_add(&Duration::microseconds(999)),
509 Some(Duration::milliseconds(i64::MAX - 2) + Duration::microseconds(1999)));
510 assert!(Duration::milliseconds(i64::MAX).checked_add(&Duration::microseconds(1000))
513 assert_eq!(Duration::milliseconds(i64::MIN).checked_sub(&Duration::milliseconds(0)),
514 Some(Duration::milliseconds(i64::MIN)));
515 assert!(Duration::milliseconds(i64::MIN).checked_sub(&Duration::milliseconds(1))
520 fn test_duration_mul() {
521 assert_eq!(Duration::zero() * i32::MAX, Duration::zero());
522 assert_eq!(Duration::zero() * i32::MIN, Duration::zero());
523 assert_eq!(Duration::nanoseconds(1) * 0, Duration::zero());
524 assert_eq!(Duration::nanoseconds(1) * 1, Duration::nanoseconds(1));
525 assert_eq!(Duration::nanoseconds(1) * 1_000_000_000, Duration::seconds(1));
526 assert_eq!(Duration::nanoseconds(1) * -1_000_000_000, -Duration::seconds(1));
527 assert_eq!(-Duration::nanoseconds(1) * 1_000_000_000, -Duration::seconds(1));
528 assert_eq!(Duration::nanoseconds(30) * 333_333_333,
529 Duration::seconds(10) - Duration::nanoseconds(10));
530 assert_eq!((Duration::nanoseconds(1) + Duration::seconds(1) + Duration::days(1)) * 3,
531 Duration::nanoseconds(3) + Duration::seconds(3) + Duration::days(3));
532 assert_eq!(Duration::milliseconds(1500) * -2, Duration::seconds(-3));
533 assert_eq!(Duration::milliseconds(-1500) * 2, Duration::seconds(-3));
537 fn test_duration_div() {
538 assert_eq!(Duration::zero() / i32::MAX, Duration::zero());
539 assert_eq!(Duration::zero() / i32::MIN, Duration::zero());
540 assert_eq!(Duration::nanoseconds(123_456_789) / 1, Duration::nanoseconds(123_456_789));
541 assert_eq!(Duration::nanoseconds(123_456_789) / -1, -Duration::nanoseconds(123_456_789));
542 assert_eq!(-Duration::nanoseconds(123_456_789) / -1, Duration::nanoseconds(123_456_789));
543 assert_eq!(-Duration::nanoseconds(123_456_789) / 1, -Duration::nanoseconds(123_456_789));
544 assert_eq!(Duration::seconds(1) / 3, Duration::nanoseconds(333_333_333));
545 assert_eq!(Duration::seconds(4) / 3, Duration::nanoseconds(1_333_333_333));
546 assert_eq!(Duration::seconds(-1) / 2, Duration::milliseconds(-500));
547 assert_eq!(Duration::seconds(1) / -2, Duration::milliseconds(-500));
548 assert_eq!(Duration::seconds(-1) / -2, Duration::milliseconds(500));
549 assert_eq!(Duration::seconds(-4) / 3, Duration::nanoseconds(-1_333_333_333));
550 assert_eq!(Duration::seconds(-4) / -3, Duration::nanoseconds(1_333_333_333));
554 fn test_duration_fmt() {
555 assert_eq!(Duration::zero().to_string(), "PT0S");
556 assert_eq!(Duration::days(42).to_string(), "P42D");
557 assert_eq!(Duration::days(-42).to_string(), "-P42D");
558 assert_eq!(Duration::seconds(42).to_string(), "PT42S");
559 assert_eq!(Duration::milliseconds(42).to_string(), "PT0.042S");
560 assert_eq!(Duration::microseconds(42).to_string(), "PT0.000042S");
561 assert_eq!(Duration::nanoseconds(42).to_string(), "PT0.000000042S");
562 assert_eq!((Duration::days(7) + Duration::milliseconds(6543)).to_string(),
564 assert_eq!(Duration::seconds(-86401).to_string(), "-P1DT1S");
565 assert_eq!(Duration::nanoseconds(-1).to_string(), "-PT0.000000001S");
567 // the format specifier should have no effect on `Duration`
568 assert_eq!(format!("{:30}", Duration::days(1) + Duration::milliseconds(2345)),