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};
21 use result::Result::{Ok, Err};
23 /// The number of nanoseconds in a microsecond.
24 const NANOS_PER_MICRO: i32 = 1000;
25 /// The number of nanoseconds in a millisecond.
26 const NANOS_PER_MILLI: i32 = 1000_000;
27 /// The number of nanoseconds in seconds.
28 const NANOS_PER_SEC: i32 = 1_000_000_000;
29 /// The number of microseconds per second.
30 const MICROS_PER_SEC: i64 = 1000_000;
31 /// The number of milliseconds per second.
32 const MILLIS_PER_SEC: i64 = 1000;
33 /// The number of seconds in a minute.
34 const SECS_PER_MINUTE: i64 = 60;
35 /// The number of seconds in an hour.
36 const SECS_PER_HOUR: i64 = 3600;
37 /// The number of (non-leap) seconds in days.
38 const SECS_PER_DAY: i64 = 86400;
39 /// The number of (non-leap) seconds in a week.
40 const SECS_PER_WEEK: i64 = 604800;
42 macro_rules! try_opt {
43 ($e:expr) => (match $e { Some(v) => v, None => return None })
47 /// ISO 8601 time duration with nanosecond precision.
48 /// This also allows for the negative duration; see individual methods for details.
49 #[deriving(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
52 nanos: i32, // Always 0 <= nanos < NANOS_PER_SEC
55 /// The minimum possible `Duration`: `i64::MIN` milliseconds.
56 pub const MIN: Duration = Duration {
57 secs: i64::MIN / MILLIS_PER_SEC - 1,
58 nanos: NANOS_PER_SEC + (i64::MIN % MILLIS_PER_SEC) as i32 * NANOS_PER_MILLI
61 /// The maximum possible `Duration`: `i64::MAX` milliseconds.
62 pub const MAX: Duration = Duration {
63 secs: i64::MAX / MILLIS_PER_SEC,
64 nanos: (i64::MAX % MILLIS_PER_SEC) as i32 * NANOS_PER_MILLI
68 /// Makes a new `Duration` with given number of weeks.
69 /// Equivalent to `Duration::seconds(weeks * 7 * 24 * 60 * 60), with overflow checks.
70 /// Panics when the duration is out of bounds.
72 pub fn weeks(weeks: i64) -> Duration {
73 let secs = weeks.checked_mul(SECS_PER_WEEK).expect("Duration::weeks out of bounds");
74 Duration::seconds(secs)
77 /// Makes a new `Duration` with given number of days.
78 /// Equivalent to `Duration::seconds(days * 24 * 60 * 60)` with overflow checks.
79 /// Panics when the duration is out of bounds.
81 pub fn days(days: i64) -> Duration {
82 let secs = days.checked_mul(SECS_PER_DAY).expect("Duration::days out of bounds");
83 Duration::seconds(secs)
86 /// Makes a new `Duration` with given number of hours.
87 /// Equivalent to `Duration::seconds(hours * 60 * 60)` with overflow checks.
88 /// Panics when the duration is out of bounds.
90 pub fn hours(hours: i64) -> Duration {
91 let secs = hours.checked_mul(SECS_PER_HOUR).expect("Duration::hours ouf of bounds");
92 Duration::seconds(secs)
95 /// Makes a new `Duration` with given number of minutes.
96 /// Equivalent to `Duration::seconds(minutes * 60)` with overflow checks.
97 /// Panics when the duration is out of bounds.
99 pub fn minutes(minutes: i64) -> Duration {
100 let secs = minutes.checked_mul(SECS_PER_MINUTE).expect("Duration::minutes out of bounds");
101 Duration::seconds(secs)
104 /// Makes a new `Duration` with given number of seconds.
105 /// Panics when the duration is more than `i64::MAX` milliseconds
106 /// or less than `i64::MIN` milliseconds.
108 pub fn seconds(seconds: i64) -> Duration {
109 let d = Duration { secs: seconds, nanos: 0 };
110 if d < MIN || d > MAX {
111 panic!("Duration::seconds out of bounds");
116 /// Makes a new `Duration` with given number of milliseconds.
118 pub fn milliseconds(milliseconds: i64) -> Duration {
119 let (secs, millis) = div_mod_floor_64(milliseconds, MILLIS_PER_SEC);
120 let nanos = millis as i32 * NANOS_PER_MILLI;
121 Duration { secs: secs, nanos: nanos }
124 /// Makes a new `Duration` with given number of microseconds.
126 pub fn microseconds(microseconds: i64) -> Duration {
127 let (secs, micros) = div_mod_floor_64(microseconds, MICROS_PER_SEC);
128 let nanos = micros as i32 * NANOS_PER_MICRO;
129 Duration { secs: secs, nanos: nanos }
132 /// Makes a new `Duration` with given number of nanoseconds.
134 pub fn nanoseconds(nanos: i64) -> Duration {
135 let (secs, nanos) = div_mod_floor_64(nanos, NANOS_PER_SEC as i64);
136 Duration { secs: secs, nanos: nanos as i32 }
139 /// Runs a closure, returning the duration of time it took to run the
141 pub fn span<F>(f: F) -> Duration where F: FnOnce() {
142 let before = super::precise_time_ns();
144 Duration::nanoseconds((super::precise_time_ns() - before) as i64)
147 /// Returns the total number of whole weeks in the duration.
149 pub fn num_weeks(&self) -> i64 {
153 /// Returns the total number of whole days in the duration.
154 pub fn num_days(&self) -> i64 {
155 self.num_seconds() / SECS_PER_DAY
158 /// Returns the total number of whole hours in the duration.
160 pub fn num_hours(&self) -> i64 {
161 self.num_seconds() / SECS_PER_HOUR
164 /// Returns the total number of whole minutes in the duration.
166 pub fn num_minutes(&self) -> i64 {
167 self.num_seconds() / SECS_PER_MINUTE
170 /// Returns the total number of whole seconds in the duration.
171 pub fn num_seconds(&self) -> i64 {
172 // If secs is negative, nanos should be subtracted from the duration.
173 if self.secs < 0 && self.nanos > 0 {
180 /// Returns the number of nanoseconds such that
181 /// `nanos_mod_sec() + num_seconds() * NANOS_PER_SEC` is the total number of
182 /// nanoseconds in the duration.
183 fn nanos_mod_sec(&self) -> i32 {
184 if self.secs < 0 && self.nanos > 0 {
185 self.nanos - NANOS_PER_SEC
191 /// Returns the total number of whole milliseconds in the duration,
192 pub fn num_milliseconds(&self) -> i64 {
193 // A proper Duration will not overflow, because MIN and MAX are defined
194 // such that the range is exactly i64 milliseconds.
195 let secs_part = self.num_seconds() * MILLIS_PER_SEC;
196 let nanos_part = self.nanos_mod_sec() / NANOS_PER_MILLI;
197 secs_part + nanos_part as i64
200 /// Returns the total number of whole microseconds in the duration,
201 /// or `None` on overflow (exceeding 2^63 microseconds in either direction).
202 pub fn num_microseconds(&self) -> Option<i64> {
203 let secs_part = try_opt!(self.num_seconds().checked_mul(MICROS_PER_SEC));
204 let nanos_part = self.nanos_mod_sec() / NANOS_PER_MICRO;
205 secs_part.checked_add(nanos_part as i64)
208 /// Returns the total number of whole nanoseconds in the duration,
209 /// or `None` on overflow (exceeding 2^63 nanoseconds in either direction).
210 pub fn num_nanoseconds(&self) -> Option<i64> {
211 let secs_part = try_opt!(self.num_seconds().checked_mul(NANOS_PER_SEC as i64));
212 let nanos_part = self.nanos_mod_sec();
213 secs_part.checked_add(nanos_part as i64)
216 /// Add two durations, returning `None` if overflow occured.
217 pub fn checked_add(&self, rhs: &Duration) -> Option<Duration> {
218 let mut secs = try_opt!(self.secs.checked_add(rhs.secs));
219 let mut nanos = self.nanos + rhs.nanos;
220 if nanos >= NANOS_PER_SEC {
221 nanos -= NANOS_PER_SEC;
222 secs = try_opt!(secs.checked_add(1));
224 let d = Duration { secs: secs, nanos: nanos };
225 // Even if d is within the bounds of i64 seconds,
226 // it might still overflow i64 milliseconds.
227 if d < MIN || d > MAX { None } else { Some(d) }
230 /// Subtract two durations, returning `None` if overflow occured.
231 pub fn checked_sub(&self, rhs: &Duration) -> Option<Duration> {
232 let mut secs = try_opt!(self.secs.checked_sub(rhs.secs));
233 let mut nanos = self.nanos - rhs.nanos;
235 nanos += NANOS_PER_SEC;
236 secs = try_opt!(secs.checked_sub(1));
238 let d = Duration { secs: secs, nanos: nanos };
239 // Even if d is within the bounds of i64 seconds,
240 // it might still overflow i64 milliseconds.
241 if d < MIN || d > MAX { None } else { Some(d) }
244 /// The minimum possible `Duration`: `i64::MIN` milliseconds.
246 pub fn min_value() -> Duration { MIN }
248 /// The maximum possible `Duration`: `i64::MAX` milliseconds.
250 pub fn max_value() -> Duration { MAX }
252 /// A duration where the stored seconds and nanoseconds are equal to zero.
254 pub fn zero() -> Duration {
255 Duration { secs: 0, nanos: 0 }
258 /// Returns `true` if the duration equals `Duration::zero()`.
260 pub fn is_zero(&self) -> bool {
261 self.secs == 0 && self.nanos == 0
265 // NOTE(stage0): Remove impl after a snapshot
267 impl Neg<Duration> for Duration {
269 fn neg(&self) -> Duration {
271 Duration { secs: -self.secs, nanos: 0 }
273 Duration { secs: -self.secs - 1, nanos: NANOS_PER_SEC - self.nanos }
278 #[cfg(not(stage0))] // NOTE(stage0): Remove cfg after a snapshot
279 impl Neg<Duration> for Duration {
281 fn neg(self) -> Duration {
283 Duration { secs: -self.secs, nanos: 0 }
285 Duration { secs: -self.secs - 1, nanos: NANOS_PER_SEC - self.nanos }
290 impl Add<Duration, Duration> for Duration {
291 fn add(self, rhs: Duration) -> Duration {
292 let mut secs = self.secs + rhs.secs;
293 let mut nanos = self.nanos + rhs.nanos;
294 if nanos >= NANOS_PER_SEC {
295 nanos -= NANOS_PER_SEC;
298 Duration { secs: secs, nanos: nanos }
302 impl Sub<Duration, Duration> for Duration {
303 fn sub(self, rhs: Duration) -> Duration {
304 let mut secs = self.secs - rhs.secs;
305 let mut nanos = self.nanos - rhs.nanos;
307 nanos += NANOS_PER_SEC;
310 Duration { secs: secs, nanos: nanos }
314 impl Mul<i32, Duration> for Duration {
315 fn mul(self, rhs: i32) -> Duration {
316 // Multiply nanoseconds as i64, because it cannot overflow that way.
317 let total_nanos = self.nanos as i64 * rhs as i64;
318 let (extra_secs, nanos) = div_mod_floor_64(total_nanos, NANOS_PER_SEC as i64);
319 let secs = self.secs * rhs as i64 + extra_secs;
320 Duration { secs: secs, nanos: nanos as i32 }
324 impl Div<i32, Duration> for Duration {
325 fn div(self, rhs: i32) -> Duration {
326 let mut secs = self.secs / rhs as i64;
327 let carry = self.secs - secs * rhs as i64;
328 let extra_nanos = carry * NANOS_PER_SEC as i64 / rhs as i64;
329 let mut nanos = self.nanos / rhs + extra_nanos as i32;
330 if nanos >= NANOS_PER_SEC {
331 nanos -= NANOS_PER_SEC;
335 nanos += NANOS_PER_SEC;
338 Duration { secs: secs, nanos: nanos }
342 impl fmt::Show for Duration {
343 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
344 // technically speaking, negative duration is not valid ISO 8601,
345 // but we need to print it anyway.
346 let (abs, sign) = if self.secs < 0 { (-*self, "-") } else { (*self, "") };
348 let days = abs.secs / SECS_PER_DAY;
349 let secs = abs.secs - days * SECS_PER_DAY;
350 let hasdate = days != 0;
351 let hastime = (secs != 0 || abs.nanos != 0) || !hasdate;
353 try!(write!(f, "{}P", sign));
356 try!(write!(f, "{}D", days));
360 try!(write!(f, "T{}S", secs));
361 } else if abs.nanos % NANOS_PER_MILLI == 0 {
362 try!(write!(f, "T{}.{:03}S", secs, abs.nanos / NANOS_PER_MILLI));
363 } else if abs.nanos % NANOS_PER_MICRO == 0 {
364 try!(write!(f, "T{}.{:06}S", secs, abs.nanos / NANOS_PER_MICRO));
366 try!(write!(f, "T{}.{:09}S", secs, abs.nanos));
373 // Copied from libnum
375 fn div_mod_floor_64(this: i64, other: i64) -> (i64, i64) {
376 (div_floor_64(this, other), mod_floor_64(this, other))
380 fn div_floor_64(this: i64, other: i64) -> i64 {
381 match div_rem_64(this, other) {
382 (d, r) if (r > 0 && other < 0)
383 || (r < 0 && other > 0) => d - 1,
389 fn mod_floor_64(this: i64, other: i64) -> i64 {
391 r if (r > 0 && other < 0)
392 || (r < 0 && other > 0) => r + other,
398 fn div_rem_64(this: i64, other: i64) -> (i64, i64) {
399 (this / other, this % other)
404 use super::{Duration, MIN, MAX};
406 use option::Option::{Some, None};
407 use string::ToString;
411 assert!(Duration::seconds(1) != Duration::zero());
412 assert_eq!(Duration::seconds(1) + Duration::seconds(2), Duration::seconds(3));
413 assert_eq!(Duration::seconds(86399) + Duration::seconds(4),
414 Duration::days(1) + Duration::seconds(3));
415 assert_eq!(Duration::days(10) - Duration::seconds(1000), Duration::seconds(863000));
416 assert_eq!(Duration::days(10) - Duration::seconds(1000000), Duration::seconds(-136000));
417 assert_eq!(Duration::days(2) + Duration::seconds(86399) +
418 Duration::nanoseconds(1234567890),
419 Duration::days(3) + Duration::nanoseconds(234567890));
420 assert_eq!(-Duration::days(3), Duration::days(-3));
421 assert_eq!(-(Duration::days(3) + Duration::seconds(70)),
422 Duration::days(-4) + Duration::seconds(86400-70));
426 fn test_duration_num_days() {
427 assert_eq!(Duration::zero().num_days(), 0);
428 assert_eq!(Duration::days(1).num_days(), 1);
429 assert_eq!(Duration::days(-1).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::seconds(-86399).num_days(), 0);
433 assert_eq!(Duration::seconds(-86401).num_days(), -1);
434 assert_eq!(Duration::days(i32::MAX as i64).num_days(), i32::MAX as i64);
435 assert_eq!(Duration::days(i32::MIN as i64).num_days(), i32::MIN as i64);
439 fn test_duration_num_seconds() {
440 assert_eq!(Duration::zero().num_seconds(), 0);
441 assert_eq!(Duration::seconds(1).num_seconds(), 1);
442 assert_eq!(Duration::seconds(-1).num_seconds(), -1);
443 assert_eq!(Duration::milliseconds(999).num_seconds(), 0);
444 assert_eq!(Duration::milliseconds(1001).num_seconds(), 1);
445 assert_eq!(Duration::milliseconds(-999).num_seconds(), 0);
446 assert_eq!(Duration::milliseconds(-1001).num_seconds(), -1);
450 fn test_duration_num_milliseconds() {
451 assert_eq!(Duration::zero().num_milliseconds(), 0);
452 assert_eq!(Duration::milliseconds(1).num_milliseconds(), 1);
453 assert_eq!(Duration::milliseconds(-1).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::microseconds(-999).num_milliseconds(), 0);
457 assert_eq!(Duration::microseconds(-1001).num_milliseconds(), -1);
458 assert_eq!(Duration::milliseconds(i64::MAX).num_milliseconds(), i64::MAX);
459 assert_eq!(Duration::milliseconds(i64::MIN).num_milliseconds(), i64::MIN);
460 assert_eq!(MAX.num_milliseconds(), i64::MAX);
461 assert_eq!(MIN.num_milliseconds(), i64::MIN);
465 fn test_duration_num_microseconds() {
466 assert_eq!(Duration::zero().num_microseconds(), Some(0));
467 assert_eq!(Duration::microseconds(1).num_microseconds(), Some(1));
468 assert_eq!(Duration::microseconds(-1).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::nanoseconds(-999).num_microseconds(), Some(0));
472 assert_eq!(Duration::nanoseconds(-1001).num_microseconds(), Some(-1));
473 assert_eq!(Duration::microseconds(i64::MAX).num_microseconds(), Some(i64::MAX));
474 assert_eq!(Duration::microseconds(i64::MIN).num_microseconds(), Some(i64::MIN));
475 assert_eq!(MAX.num_microseconds(), None);
476 assert_eq!(MIN.num_microseconds(), None);
479 const MICROS_PER_DAY: i64 = 86400_000_000;
480 assert_eq!(Duration::days(i64::MAX / MICROS_PER_DAY).num_microseconds(),
481 Some(i64::MAX / MICROS_PER_DAY * MICROS_PER_DAY));
482 assert_eq!(Duration::days(i64::MIN / MICROS_PER_DAY).num_microseconds(),
483 Some(i64::MIN / MICROS_PER_DAY * MICROS_PER_DAY));
484 assert_eq!(Duration::days(i64::MAX / MICROS_PER_DAY + 1).num_microseconds(), None);
485 assert_eq!(Duration::days(i64::MIN / MICROS_PER_DAY - 1).num_microseconds(), None);
489 fn test_duration_num_nanoseconds() {
490 assert_eq!(Duration::zero().num_nanoseconds(), Some(0));
491 assert_eq!(Duration::nanoseconds(1).num_nanoseconds(), Some(1));
492 assert_eq!(Duration::nanoseconds(-1).num_nanoseconds(), Some(-1));
493 assert_eq!(Duration::nanoseconds(i64::MAX).num_nanoseconds(), Some(i64::MAX));
494 assert_eq!(Duration::nanoseconds(i64::MIN).num_nanoseconds(), Some(i64::MIN));
495 assert_eq!(MAX.num_nanoseconds(), None);
496 assert_eq!(MIN.num_nanoseconds(), None);
499 const NANOS_PER_DAY: i64 = 86400_000_000_000;
500 assert_eq!(Duration::days(i64::MAX / NANOS_PER_DAY).num_nanoseconds(),
501 Some(i64::MAX / NANOS_PER_DAY * NANOS_PER_DAY));
502 assert_eq!(Duration::days(i64::MIN / NANOS_PER_DAY).num_nanoseconds(),
503 Some(i64::MIN / NANOS_PER_DAY * NANOS_PER_DAY));
504 assert_eq!(Duration::days(i64::MAX / NANOS_PER_DAY + 1).num_nanoseconds(), None);
505 assert_eq!(Duration::days(i64::MIN / NANOS_PER_DAY - 1).num_nanoseconds(), None);
509 fn test_duration_checked_ops() {
510 assert_eq!(Duration::milliseconds(i64::MAX - 1).checked_add(&Duration::microseconds(999)),
511 Some(Duration::milliseconds(i64::MAX - 2) + Duration::microseconds(1999)));
512 assert!(Duration::milliseconds(i64::MAX).checked_add(&Duration::microseconds(1000))
515 assert_eq!(Duration::milliseconds(i64::MIN).checked_sub(&Duration::milliseconds(0)),
516 Some(Duration::milliseconds(i64::MIN)));
517 assert!(Duration::milliseconds(i64::MIN).checked_sub(&Duration::milliseconds(1))
522 fn test_duration_mul() {
523 assert_eq!(Duration::zero() * i32::MAX, Duration::zero());
524 assert_eq!(Duration::zero() * i32::MIN, Duration::zero());
525 assert_eq!(Duration::nanoseconds(1) * 0, Duration::zero());
526 assert_eq!(Duration::nanoseconds(1) * 1, Duration::nanoseconds(1));
527 assert_eq!(Duration::nanoseconds(1) * 1_000_000_000, Duration::seconds(1));
528 assert_eq!(Duration::nanoseconds(1) * -1_000_000_000, -Duration::seconds(1));
529 assert_eq!(-Duration::nanoseconds(1) * 1_000_000_000, -Duration::seconds(1));
530 assert_eq!(Duration::nanoseconds(30) * 333_333_333,
531 Duration::seconds(10) - Duration::nanoseconds(10));
532 assert_eq!((Duration::nanoseconds(1) + Duration::seconds(1) + Duration::days(1)) * 3,
533 Duration::nanoseconds(3) + Duration::seconds(3) + Duration::days(3));
534 assert_eq!(Duration::milliseconds(1500) * -2, Duration::seconds(-3));
535 assert_eq!(Duration::milliseconds(-1500) * 2, Duration::seconds(-3));
539 fn test_duration_div() {
540 assert_eq!(Duration::zero() / i32::MAX, Duration::zero());
541 assert_eq!(Duration::zero() / i32::MIN, Duration::zero());
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::nanoseconds(123_456_789) / -1, Duration::nanoseconds(123_456_789));
545 assert_eq!(-Duration::nanoseconds(123_456_789) / 1, -Duration::nanoseconds(123_456_789));
546 assert_eq!(Duration::seconds(1) / 3, Duration::nanoseconds(333_333_333));
547 assert_eq!(Duration::seconds(4) / 3, Duration::nanoseconds(1_333_333_333));
548 assert_eq!(Duration::seconds(-1) / 2, Duration::milliseconds(-500));
549 assert_eq!(Duration::seconds(1) / -2, Duration::milliseconds(-500));
550 assert_eq!(Duration::seconds(-1) / -2, Duration::milliseconds(500));
551 assert_eq!(Duration::seconds(-4) / 3, Duration::nanoseconds(-1_333_333_333));
552 assert_eq!(Duration::seconds(-4) / -3, Duration::nanoseconds(1_333_333_333));
556 fn test_duration_fmt() {
557 assert_eq!(Duration::zero().to_string(), "PT0S");
558 assert_eq!(Duration::days(42).to_string(), "P42D");
559 assert_eq!(Duration::days(-42).to_string(), "-P42D");
560 assert_eq!(Duration::seconds(42).to_string(), "PT42S");
561 assert_eq!(Duration::milliseconds(42).to_string(), "PT0.042S");
562 assert_eq!(Duration::microseconds(42).to_string(), "PT0.000042S");
563 assert_eq!(Duration::nanoseconds(42).to_string(), "PT0.000000042S");
564 assert_eq!((Duration::days(7) + Duration::milliseconds(6543)).to_string(),
566 assert_eq!(Duration::seconds(-86401).to_string(), "-P1DT1S");
567 assert_eq!(Duration::nanoseconds(-1).to_string(), "-PT0.000000001S");
569 // the format specifier should have no effect on `Duration`
570 assert_eq!(format!("{:30}", Duration::days(1) + Duration::milliseconds(2345)),