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.
4//
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.
10
11//! Temporal quantification
12
13use core::ops::{Add, Div, Mul, Neg, Sub};
14use core::time::Duration as StdDuration;
15use core::{fmt, i64};
16#[cfg(any(feature = "std", test))]
17use std::error::Error;
18
19#[cfg(feature = "rkyv")]
20use rkyv::{Archive, Deserialize, Serialize};
21
22/// The number of nanoseconds in a microsecond.
23const NANOS_PER_MICRO: i32 = 1000;
24/// The number of nanoseconds in a millisecond.
25const NANOS_PER_MILLI: i32 = 1000_000;
26/// The number of nanoseconds in seconds.
27const NANOS_PER_SEC: i32 = 1_000_000_000;
28/// The number of microseconds per second.
29const MICROS_PER_SEC: i64 = 1000_000;
30/// The number of milliseconds per second.
31const MILLIS_PER_SEC: i64 = 1000;
32/// The number of seconds in a minute.
33const SECS_PER_MINUTE: i64 = 60;
34/// The number of seconds in an hour.
35const SECS_PER_HOUR: i64 = 3600;
36/// The number of (non-leap) seconds in days.
37const SECS_PER_DAY: i64 = 86400;
38/// The number of (non-leap) seconds in a week.
39const SECS_PER_WEEK: i64 = 604800;
40
41macro_rules! try_opt {
42 ($e:expr) => {
43 match $e {
44 Some(v) => v,
45 None => return None,
46 }
47 };
48}
49
50/// ISO 8601 time duration with nanosecond precision.
51///
52/// This also allows for the negative duration; see individual methods for details.
53#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
54#[cfg_attr(feature = "rkyv", derive(Archive, Deserialize, Serialize))]
55pub struct Duration {
56 secs: i64,
57 nanos: i32, // Always 0 <= nanos < NANOS_PER_SEC
58}
59
60/// The minimum possible `Duration`: `i64::MIN` milliseconds.
61pub(crate) const MIN: Duration = Duration {
62 secs: i64::MIN / MILLIS_PER_SEC - 1,
63 nanos: NANOS_PER_SEC + (i64::MIN % MILLIS_PER_SEC) as i32 * NANOS_PER_MILLI,
64};
65
66/// The maximum possible `Duration`: `i64::MAX` milliseconds.
67pub(crate) const MAX: Duration = Duration {
68 secs: i64::MAX / MILLIS_PER_SEC,
69 nanos: (i64::MAX % MILLIS_PER_SEC) as i32 * NANOS_PER_MILLI,
70};
71
72impl Duration {
73 /// Makes a new `Duration` with given number of weeks.
74 /// Equivalent to `Duration::seconds(weeks * 7 * 24 * 60 * 60)` with overflow checks.
75 /// Panics when the duration is out of bounds.
76 #[inline]
77 #[must_use]
78 pub fn weeks(weeks: i64) -> Duration {
79 let secs = weeks.checked_mul(SECS_PER_WEEK).expect("Duration::weeks out of bounds");
80 Duration::seconds(secs)
81 }
82
83 /// Makes a new `Duration` with given number of days.
84 /// Equivalent to `Duration::seconds(days * 24 * 60 * 60)` with overflow checks.
85 /// Panics when the duration is out of bounds.
86 #[inline]
87 #[must_use]
88 pub fn days(days: i64) -> Duration {
89 let secs = days.checked_mul(SECS_PER_DAY).expect("Duration::days out of bounds");
90 Duration::seconds(secs)
91 }
92
93 /// Makes a new `Duration` with given number of hours.
94 /// Equivalent to `Duration::seconds(hours * 60 * 60)` with overflow checks.
95 /// Panics when the duration is out of bounds.
96 #[inline]
97 #[must_use]
98 pub fn hours(hours: i64) -> Duration {
99 let secs = hours.checked_mul(SECS_PER_HOUR).expect("Duration::hours ouf of bounds");
100 Duration::seconds(secs)
101 }
102
103 /// Makes a new `Duration` with given number of minutes.
104 /// Equivalent to `Duration::seconds(minutes * 60)` with overflow checks.
105 /// Panics when the duration is out of bounds.
106 #[inline]
107 #[must_use]
108 pub fn minutes(minutes: i64) -> Duration {
109 let secs = minutes.checked_mul(SECS_PER_MINUTE).expect("Duration::minutes out of bounds");
110 Duration::seconds(secs)
111 }
112
113 /// Makes a new `Duration` with given number of seconds.
114 /// Panics when the duration is more than `i64::MAX` seconds
115 /// or less than `i64::MIN` seconds.
116 #[inline]
117 #[must_use]
118 pub fn seconds(seconds: i64) -> Duration {
119 let d = Duration { secs: seconds, nanos: 0 };
120 if d < MIN || d > MAX {
121 panic!("Duration::seconds out of bounds");
122 }
123 d
124 }
125
126 /// Makes a new `Duration` with given number of milliseconds.
127 #[inline]
128 pub const fn milliseconds(milliseconds: i64) -> Duration {
129 let (secs, millis) = div_mod_floor_64(milliseconds, MILLIS_PER_SEC);
130 let nanos = millis as i32 * NANOS_PER_MILLI;
131 Duration { secs: secs, nanos: nanos }
132 }
133
134 /// Makes a new `Duration` with given number of microseconds.
135 #[inline]
136 pub const fn microseconds(microseconds: i64) -> Duration {
137 let (secs, micros) = div_mod_floor_64(microseconds, MICROS_PER_SEC);
138 let nanos = micros as i32 * NANOS_PER_MICRO;
139 Duration { secs: secs, nanos: nanos }
140 }
141
142 /// Makes a new `Duration` with given number of nanoseconds.
143 #[inline]
144 pub const fn nanoseconds(nanos: i64) -> Duration {
145 let (secs, nanos) = div_mod_floor_64(nanos, NANOS_PER_SEC as i64);
146 Duration { secs: secs, nanos: nanos as i32 }
147 }
148
149 /// Returns the total number of whole weeks in the duration.
150 #[inline]
151 pub const fn num_weeks(&self) -> i64 {
152 self.num_days() / 7
153 }
154
155 /// Returns the total number of whole days in the duration.
156 pub const fn num_days(&self) -> i64 {
157 self.num_seconds() / SECS_PER_DAY
158 }
159
160 /// Returns the total number of whole hours in the duration.
161 #[inline]
162 pub const fn num_hours(&self) -> i64 {
163 self.num_seconds() / SECS_PER_HOUR
164 }
165
166 /// Returns the total number of whole minutes in the duration.
167 #[inline]
168 pub const fn num_minutes(&self) -> i64 {
169 self.num_seconds() / SECS_PER_MINUTE
170 }
171
172 /// Returns the total number of whole seconds in the duration.
173 pub const 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 {
176 self.secs + 1
177 } else {
178 self.secs
179 }
180 }
181
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 const fn nanos_mod_sec(&self) -> i32 {
186 if self.secs < 0 && self.nanos > 0 {
187 self.nanos - NANOS_PER_SEC
188 } else {
189 self.nanos
190 }
191 }
192
193 /// Returns the total number of whole milliseconds in the duration,
194 pub const 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
200 }
201
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 const 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)
208 }
209
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 const 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)
216 }
217
218 /// Add two durations, returning `None` if overflow occurred.
219 #[must_use]
220 pub fn checked_add(&self, rhs: &Duration) -> Option<Duration> {
221 let mut secs = try_opt!(self.secs.checked_add(rhs.secs));
222 let mut nanos = self.nanos + rhs.nanos;
223 if nanos >= NANOS_PER_SEC {
224 nanos -= NANOS_PER_SEC;
225 secs = try_opt!(secs.checked_add(1));
226 }
227 let d = Duration { secs: secs, nanos: nanos };
228 // Even if d is within the bounds of i64 seconds,
229 // it might still overflow i64 milliseconds.
230 if d < MIN || d > MAX {
231 None
232 } else {
233 Some(d)
234 }
235 }
236
237 /// Subtract two durations, returning `None` if overflow occurred.
238 #[must_use]
239 pub fn checked_sub(&self, rhs: &Duration) -> Option<Duration> {
240 let mut secs = try_opt!(self.secs.checked_sub(rhs.secs));
241 let mut nanos = self.nanos - rhs.nanos;
242 if nanos < 0 {
243 nanos += NANOS_PER_SEC;
244 secs = try_opt!(secs.checked_sub(1));
245 }
246 let d = Duration { secs: secs, nanos: nanos };
247 // Even if d is within the bounds of i64 seconds,
248 // it might still overflow i64 milliseconds.
249 if d < MIN || d > MAX {
250 None
251 } else {
252 Some(d)
253 }
254 }
255
256 /// Returns the duration as an absolute (non-negative) value.
257 #[inline]
258 pub const fn abs(&self) -> Duration {
259 if self.secs < 0 && self.nanos != 0 {
260 Duration { secs: (self.secs + 1).abs(), nanos: NANOS_PER_SEC - self.nanos }
261 } else {
262 Duration { secs: self.secs.abs(), nanos: self.nanos }
263 }
264 }
265
266 /// The minimum possible `Duration`: `i64::MIN` milliseconds.
267 #[inline]
268 pub const fn min_value() -> Duration {
269 MIN
270 }
271
272 /// The maximum possible `Duration`: `i64::MAX` milliseconds.
273 #[inline]
274 pub const fn max_value() -> Duration {
275 MAX
276 }
277
278 /// A duration where the stored seconds and nanoseconds are equal to zero.
279 #[inline]
280 pub const fn zero() -> Duration {
281 Duration { secs: 0, nanos: 0 }
282 }
283
284 /// Returns `true` if the duration equals `Duration::zero()`.
285 #[inline]
286 pub const fn is_zero(&self) -> bool {
287 self.secs == 0 && self.nanos == 0
288 }
289
290 /// Creates a `time::Duration` object from `std::time::Duration`
291 ///
292 /// This function errors when original duration is larger than the maximum
293 /// value supported for this type.
294 pub fn from_std(duration: StdDuration) -> Result<Duration, OutOfRangeError> {
295 // We need to check secs as u64 before coercing to i64
296 if duration.as_secs() > MAX.secs as u64 {
297 return Err(OutOfRangeError(()));
298 }
299 let d = Duration { secs: duration.as_secs() as i64, nanos: duration.subsec_nanos() as i32 };
300 if d > MAX {
301 return Err(OutOfRangeError(()));
302 }
303 Ok(d)
304 }
305
306 /// Creates a `std::time::Duration` object from `time::Duration`
307 ///
308 /// This function errors when duration is less than zero. As standard
309 /// library implementation is limited to non-negative values.
310 pub fn to_std(&self) -> Result<StdDuration, OutOfRangeError> {
311 if self.secs < 0 {
312 return Err(OutOfRangeError(()));
313 }
314 Ok(StdDuration::new(self.secs as u64, self.nanos as u32))
315 }
316}
317
318impl Neg for Duration {
319 type Output = Duration;
320
321 #[inline]
322 fn neg(self) -> Duration {
323 if self.nanos == 0 {
324 Duration { secs: -self.secs, nanos: 0 }
325 } else {
326 Duration { secs: -self.secs - 1, nanos: NANOS_PER_SEC - self.nanos }
327 }
328 }
329}
330
331impl Add for Duration {
332 type Output = Duration;
333
334 fn add(self, rhs: Duration) -> Duration {
335 let mut secs: i64 = self.secs + rhs.secs;
336 let mut nanos: i32 = self.nanos + rhs.nanos;
337 if nanos >= NANOS_PER_SEC {
338 nanos -= NANOS_PER_SEC;
339 secs += 1;
340 }
341 Duration { secs: secs, nanos: nanos }
342 }
343}
344
345impl Sub for Duration {
346 type Output = Duration;
347
348 fn sub(self, rhs: Duration) -> Duration {
349 let mut secs: i64 = self.secs - rhs.secs;
350 let mut nanos: i32 = self.nanos - rhs.nanos;
351 if nanos < 0 {
352 nanos += NANOS_PER_SEC;
353 secs -= 1;
354 }
355 Duration { secs: secs, nanos: nanos }
356 }
357}
358
359impl Mul<i32> for Duration {
360 type Output = Duration;
361
362 fn mul(self, rhs: i32) -> Duration {
363 // Multiply nanoseconds as i64, because it cannot overflow that way.
364 let total_nanos: i64 = self.nanos as i64 * rhs as i64;
365 let (extra_secs: i64, nanos: i64) = div_mod_floor_64(this:total_nanos, NANOS_PER_SEC as i64);
366 let secs: i64 = self.secs * rhs as i64 + extra_secs;
367 Duration { secs: secs, nanos: nanos as i32 }
368 }
369}
370
371impl Div<i32> for Duration {
372 type Output = Duration;
373
374 fn div(self, rhs: i32) -> Duration {
375 let mut secs: i64 = self.secs / rhs as i64;
376 let carry: i64 = self.secs - secs * rhs as i64;
377 let extra_nanos: i64 = carry * NANOS_PER_SEC as i64 / rhs as i64;
378 let mut nanos: i32 = self.nanos / rhs + extra_nanos as i32;
379 if nanos >= NANOS_PER_SEC {
380 nanos -= NANOS_PER_SEC;
381 secs += 1;
382 }
383 if nanos < 0 {
384 nanos += NANOS_PER_SEC;
385 secs -= 1;
386 }
387 Duration { secs: secs, nanos: nanos }
388 }
389}
390
391#[cfg(any(feature = "std", test))]
392#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
393impl<'a> std::iter::Sum<&'a Duration> for Duration {
394 fn sum<I: Iterator<Item = &'a Duration>>(iter: I) -> Duration {
395 iter.fold(init:Duration::zero(), |acc: Duration, x: &Duration| acc + *x)
396 }
397}
398
399#[cfg(any(feature = "std", test))]
400#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
401impl std::iter::Sum<Duration> for Duration {
402 fn sum<I: Iterator<Item = Duration>>(iter: I) -> Duration {
403 iter.fold(init:Duration::zero(), |acc: Duration, x: Duration| acc + x)
404 }
405}
406
407impl fmt::Display for Duration {
408 /// Format a duration using the [ISO 8601] format
409 ///
410 /// [ISO 8601]: https://en.wikipedia.org/wiki/ISO_8601#Durations
411 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
412 // technically speaking, negative duration is not valid ISO 8601,
413 // but we need to print it anyway.
414 let (abs, sign) = if self.secs < 0 { (-*self, "-") } else { (*self, "") };
415
416 let days = abs.secs / SECS_PER_DAY;
417 let secs = abs.secs - days * SECS_PER_DAY;
418 let hasdate = days != 0;
419 let hastime = (secs != 0 || abs.nanos != 0) || !hasdate;
420
421 write!(f, "{}P", sign)?;
422
423 if hasdate {
424 write!(f, "{}D", days)?;
425 }
426 if hastime {
427 if abs.nanos == 0 {
428 write!(f, "T{}S", secs)?;
429 } else if abs.nanos % NANOS_PER_MILLI == 0 {
430 write!(f, "T{}.{:03}S", secs, abs.nanos / NANOS_PER_MILLI)?;
431 } else if abs.nanos % NANOS_PER_MICRO == 0 {
432 write!(f, "T{}.{:06}S", secs, abs.nanos / NANOS_PER_MICRO)?;
433 } else {
434 write!(f, "T{}.{:09}S", secs, abs.nanos)?;
435 }
436 }
437 Ok(())
438 }
439}
440
441/// Represents error when converting `Duration` to/from a standard library
442/// implementation
443///
444/// The `std::time::Duration` supports a range from zero to `u64::MAX`
445/// *seconds*, while this module supports signed range of up to
446/// `i64::MAX` of *milliseconds*.
447#[derive(Debug, Clone, Copy, PartialEq, Eq)]
448pub struct OutOfRangeError(());
449
450impl fmt::Display for OutOfRangeError {
451 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
452 write!(f, "Source duration value is out of range for the target type")
453 }
454}
455
456#[cfg(any(feature = "std", test))]
457#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
458impl Error for OutOfRangeError {
459 #[allow(deprecated)]
460 fn description(&self) -> &str {
461 "out of range error"
462 }
463}
464
465#[inline]
466const fn div_mod_floor_64(this: i64, other: i64) -> (i64, i64) {
467 (this.div_euclid(other), this.rem_euclid(other))
468}
469
470#[cfg(feature = "arbitrary")]
471impl arbitrary::Arbitrary<'_> for Duration {
472 fn arbitrary(u: &mut arbitrary::Unstructured) -> arbitrary::Result<Duration> {
473 const MIN_SECS: i64 = i64::MIN / MILLIS_PER_SEC - 1;
474 const MAX_SECS: i64 = i64::MAX / MILLIS_PER_SEC;
475
476 let secs: i64 = u.int_in_range(MIN_SECS..=MAX_SECS)?;
477 let nanos: i32 = u.int_in_range(0..=(NANOS_PER_SEC - 1))?;
478 let duration = Duration { secs, nanos };
479
480 if duration < MIN || duration > MAX {
481 Err(arbitrary::Error::IncorrectFormat)
482 } else {
483 Ok(duration)
484 }
485 }
486}
487
488#[cfg(test)]
489mod tests {
490 use super::{Duration, OutOfRangeError, MAX, MIN};
491 use std::time::Duration as StdDuration;
492 use std::{i32, i64};
493
494 #[test]
495 fn test_duration() {
496 assert!(Duration::seconds(1) != Duration::zero());
497 assert_eq!(Duration::seconds(1) + Duration::seconds(2), Duration::seconds(3));
498 assert_eq!(
499 Duration::seconds(86399) + Duration::seconds(4),
500 Duration::days(1) + Duration::seconds(3)
501 );
502 assert_eq!(Duration::days(10) - Duration::seconds(1000), Duration::seconds(863000));
503 assert_eq!(Duration::days(10) - Duration::seconds(1000000), Duration::seconds(-136000));
504 assert_eq!(
505 Duration::days(2) + Duration::seconds(86399) + Duration::nanoseconds(1234567890),
506 Duration::days(3) + Duration::nanoseconds(234567890)
507 );
508 assert_eq!(-Duration::days(3), Duration::days(-3));
509 assert_eq!(
510 -(Duration::days(3) + Duration::seconds(70)),
511 Duration::days(-4) + Duration::seconds(86400 - 70)
512 );
513 }
514
515 #[test]
516 fn test_duration_num_days() {
517 assert_eq!(Duration::zero().num_days(), 0);
518 assert_eq!(Duration::days(1).num_days(), 1);
519 assert_eq!(Duration::days(-1).num_days(), -1);
520 assert_eq!(Duration::seconds(86399).num_days(), 0);
521 assert_eq!(Duration::seconds(86401).num_days(), 1);
522 assert_eq!(Duration::seconds(-86399).num_days(), 0);
523 assert_eq!(Duration::seconds(-86401).num_days(), -1);
524 assert_eq!(Duration::days(i32::MAX as i64).num_days(), i32::MAX as i64);
525 assert_eq!(Duration::days(i32::MIN as i64).num_days(), i32::MIN as i64);
526 }
527
528 #[test]
529 fn test_duration_num_seconds() {
530 assert_eq!(Duration::zero().num_seconds(), 0);
531 assert_eq!(Duration::seconds(1).num_seconds(), 1);
532 assert_eq!(Duration::seconds(-1).num_seconds(), -1);
533 assert_eq!(Duration::milliseconds(999).num_seconds(), 0);
534 assert_eq!(Duration::milliseconds(1001).num_seconds(), 1);
535 assert_eq!(Duration::milliseconds(-999).num_seconds(), 0);
536 assert_eq!(Duration::milliseconds(-1001).num_seconds(), -1);
537 }
538
539 #[test]
540 fn test_duration_num_milliseconds() {
541 assert_eq!(Duration::zero().num_milliseconds(), 0);
542 assert_eq!(Duration::milliseconds(1).num_milliseconds(), 1);
543 assert_eq!(Duration::milliseconds(-1).num_milliseconds(), -1);
544 assert_eq!(Duration::microseconds(999).num_milliseconds(), 0);
545 assert_eq!(Duration::microseconds(1001).num_milliseconds(), 1);
546 assert_eq!(Duration::microseconds(-999).num_milliseconds(), 0);
547 assert_eq!(Duration::microseconds(-1001).num_milliseconds(), -1);
548 assert_eq!(Duration::milliseconds(i64::MAX).num_milliseconds(), i64::MAX);
549 assert_eq!(Duration::milliseconds(i64::MIN).num_milliseconds(), i64::MIN);
550 assert_eq!(MAX.num_milliseconds(), i64::MAX);
551 assert_eq!(MIN.num_milliseconds(), i64::MIN);
552 }
553
554 #[test]
555 fn test_duration_num_microseconds() {
556 assert_eq!(Duration::zero().num_microseconds(), Some(0));
557 assert_eq!(Duration::microseconds(1).num_microseconds(), Some(1));
558 assert_eq!(Duration::microseconds(-1).num_microseconds(), Some(-1));
559 assert_eq!(Duration::nanoseconds(999).num_microseconds(), Some(0));
560 assert_eq!(Duration::nanoseconds(1001).num_microseconds(), Some(1));
561 assert_eq!(Duration::nanoseconds(-999).num_microseconds(), Some(0));
562 assert_eq!(Duration::nanoseconds(-1001).num_microseconds(), Some(-1));
563 assert_eq!(Duration::microseconds(i64::MAX).num_microseconds(), Some(i64::MAX));
564 assert_eq!(Duration::microseconds(i64::MIN).num_microseconds(), Some(i64::MIN));
565 assert_eq!(MAX.num_microseconds(), None);
566 assert_eq!(MIN.num_microseconds(), None);
567
568 // overflow checks
569 const MICROS_PER_DAY: i64 = 86400_000_000;
570 assert_eq!(
571 Duration::days(i64::MAX / MICROS_PER_DAY).num_microseconds(),
572 Some(i64::MAX / MICROS_PER_DAY * MICROS_PER_DAY)
573 );
574 assert_eq!(
575 Duration::days(i64::MIN / MICROS_PER_DAY).num_microseconds(),
576 Some(i64::MIN / MICROS_PER_DAY * MICROS_PER_DAY)
577 );
578 assert_eq!(Duration::days(i64::MAX / MICROS_PER_DAY + 1).num_microseconds(), None);
579 assert_eq!(Duration::days(i64::MIN / MICROS_PER_DAY - 1).num_microseconds(), None);
580 }
581
582 #[test]
583 fn test_duration_num_nanoseconds() {
584 assert_eq!(Duration::zero().num_nanoseconds(), Some(0));
585 assert_eq!(Duration::nanoseconds(1).num_nanoseconds(), Some(1));
586 assert_eq!(Duration::nanoseconds(-1).num_nanoseconds(), Some(-1));
587 assert_eq!(Duration::nanoseconds(i64::MAX).num_nanoseconds(), Some(i64::MAX));
588 assert_eq!(Duration::nanoseconds(i64::MIN).num_nanoseconds(), Some(i64::MIN));
589 assert_eq!(MAX.num_nanoseconds(), None);
590 assert_eq!(MIN.num_nanoseconds(), None);
591
592 // overflow checks
593 const NANOS_PER_DAY: i64 = 86400_000_000_000;
594 assert_eq!(
595 Duration::days(i64::MAX / NANOS_PER_DAY).num_nanoseconds(),
596 Some(i64::MAX / NANOS_PER_DAY * NANOS_PER_DAY)
597 );
598 assert_eq!(
599 Duration::days(i64::MIN / NANOS_PER_DAY).num_nanoseconds(),
600 Some(i64::MIN / NANOS_PER_DAY * NANOS_PER_DAY)
601 );
602 assert_eq!(Duration::days(i64::MAX / NANOS_PER_DAY + 1).num_nanoseconds(), None);
603 assert_eq!(Duration::days(i64::MIN / NANOS_PER_DAY - 1).num_nanoseconds(), None);
604 }
605
606 #[test]
607 fn test_duration_checked_ops() {
608 assert_eq!(
609 Duration::milliseconds(i64::MAX - 1).checked_add(&Duration::microseconds(999)),
610 Some(Duration::milliseconds(i64::MAX - 2) + Duration::microseconds(1999))
611 );
612 assert!(Duration::milliseconds(i64::MAX)
613 .checked_add(&Duration::microseconds(1000))
614 .is_none());
615
616 assert_eq!(
617 Duration::milliseconds(i64::MIN).checked_sub(&Duration::milliseconds(0)),
618 Some(Duration::milliseconds(i64::MIN))
619 );
620 assert!(Duration::milliseconds(i64::MIN).checked_sub(&Duration::milliseconds(1)).is_none());
621 }
622
623 #[test]
624 fn test_duration_abs() {
625 assert_eq!(Duration::milliseconds(1300).abs(), Duration::milliseconds(1300));
626 assert_eq!(Duration::milliseconds(1000).abs(), Duration::milliseconds(1000));
627 assert_eq!(Duration::milliseconds(300).abs(), Duration::milliseconds(300));
628 assert_eq!(Duration::milliseconds(0).abs(), Duration::milliseconds(0));
629 assert_eq!(Duration::milliseconds(-300).abs(), Duration::milliseconds(300));
630 assert_eq!(Duration::milliseconds(-700).abs(), Duration::milliseconds(700));
631 assert_eq!(Duration::milliseconds(-1000).abs(), Duration::milliseconds(1000));
632 assert_eq!(Duration::milliseconds(-1300).abs(), Duration::milliseconds(1300));
633 assert_eq!(Duration::milliseconds(-1700).abs(), Duration::milliseconds(1700));
634 }
635
636 #[test]
637 fn test_duration_mul() {
638 assert_eq!(Duration::zero() * i32::MAX, Duration::zero());
639 assert_eq!(Duration::zero() * i32::MIN, Duration::zero());
640 assert_eq!(Duration::nanoseconds(1) * 0, Duration::zero());
641 assert_eq!(Duration::nanoseconds(1) * 1, Duration::nanoseconds(1));
642 assert_eq!(Duration::nanoseconds(1) * 1_000_000_000, Duration::seconds(1));
643 assert_eq!(Duration::nanoseconds(1) * -1_000_000_000, -Duration::seconds(1));
644 assert_eq!(-Duration::nanoseconds(1) * 1_000_000_000, -Duration::seconds(1));
645 assert_eq!(
646 Duration::nanoseconds(30) * 333_333_333,
647 Duration::seconds(10) - Duration::nanoseconds(10)
648 );
649 assert_eq!(
650 (Duration::nanoseconds(1) + Duration::seconds(1) + Duration::days(1)) * 3,
651 Duration::nanoseconds(3) + Duration::seconds(3) + Duration::days(3)
652 );
653 assert_eq!(Duration::milliseconds(1500) * -2, Duration::seconds(-3));
654 assert_eq!(Duration::milliseconds(-1500) * 2, Duration::seconds(-3));
655 }
656
657 #[test]
658 fn test_duration_div() {
659 assert_eq!(Duration::zero() / i32::MAX, Duration::zero());
660 assert_eq!(Duration::zero() / i32::MIN, Duration::zero());
661 assert_eq!(Duration::nanoseconds(123_456_789) / 1, Duration::nanoseconds(123_456_789));
662 assert_eq!(Duration::nanoseconds(123_456_789) / -1, -Duration::nanoseconds(123_456_789));
663 assert_eq!(-Duration::nanoseconds(123_456_789) / -1, Duration::nanoseconds(123_456_789));
664 assert_eq!(-Duration::nanoseconds(123_456_789) / 1, -Duration::nanoseconds(123_456_789));
665 assert_eq!(Duration::seconds(1) / 3, Duration::nanoseconds(333_333_333));
666 assert_eq!(Duration::seconds(4) / 3, Duration::nanoseconds(1_333_333_333));
667 assert_eq!(Duration::seconds(-1) / 2, Duration::milliseconds(-500));
668 assert_eq!(Duration::seconds(1) / -2, Duration::milliseconds(-500));
669 assert_eq!(Duration::seconds(-1) / -2, Duration::milliseconds(500));
670 assert_eq!(Duration::seconds(-4) / 3, Duration::nanoseconds(-1_333_333_333));
671 assert_eq!(Duration::seconds(-4) / -3, Duration::nanoseconds(1_333_333_333));
672 }
673
674 #[test]
675 fn test_duration_sum() {
676 let duration_list_1 = [Duration::zero(), Duration::seconds(1)];
677 let sum_1: Duration = duration_list_1.iter().sum();
678 assert_eq!(sum_1, Duration::seconds(1));
679
680 let duration_list_2 =
681 [Duration::zero(), Duration::seconds(1), Duration::seconds(6), Duration::seconds(10)];
682 let sum_2: Duration = duration_list_2.iter().sum();
683 assert_eq!(sum_2, Duration::seconds(17));
684
685 let duration_vec = vec![
686 Duration::zero(),
687 Duration::seconds(1),
688 Duration::seconds(6),
689 Duration::seconds(10),
690 ];
691 let sum_3: Duration = duration_vec.into_iter().sum();
692 assert_eq!(sum_3, Duration::seconds(17));
693 }
694
695 #[test]
696 fn test_duration_fmt() {
697 assert_eq!(Duration::zero().to_string(), "PT0S");
698 assert_eq!(Duration::days(42).to_string(), "P42D");
699 assert_eq!(Duration::days(-42).to_string(), "-P42D");
700 assert_eq!(Duration::seconds(42).to_string(), "PT42S");
701 assert_eq!(Duration::milliseconds(42).to_string(), "PT0.042S");
702 assert_eq!(Duration::microseconds(42).to_string(), "PT0.000042S");
703 assert_eq!(Duration::nanoseconds(42).to_string(), "PT0.000000042S");
704 assert_eq!((Duration::days(7) + Duration::milliseconds(6543)).to_string(), "P7DT6.543S");
705 assert_eq!(Duration::seconds(-86401).to_string(), "-P1DT1S");
706 assert_eq!(Duration::nanoseconds(-1).to_string(), "-PT0.000000001S");
707
708 // the format specifier should have no effect on `Duration`
709 assert_eq!(
710 format!("{:30}", Duration::days(1) + Duration::milliseconds(2345)),
711 "P1DT2.345S"
712 );
713 }
714
715 #[test]
716 fn test_to_std() {
717 assert_eq!(Duration::seconds(1).to_std(), Ok(StdDuration::new(1, 0)));
718 assert_eq!(Duration::seconds(86401).to_std(), Ok(StdDuration::new(86401, 0)));
719 assert_eq!(Duration::milliseconds(123).to_std(), Ok(StdDuration::new(0, 123000000)));
720 assert_eq!(Duration::milliseconds(123765).to_std(), Ok(StdDuration::new(123, 765000000)));
721 assert_eq!(Duration::nanoseconds(777).to_std(), Ok(StdDuration::new(0, 777)));
722 assert_eq!(MAX.to_std(), Ok(StdDuration::new(9223372036854775, 807000000)));
723 assert_eq!(Duration::seconds(-1).to_std(), Err(OutOfRangeError(())));
724 assert_eq!(Duration::milliseconds(-1).to_std(), Err(OutOfRangeError(())));
725 }
726
727 #[test]
728 fn test_from_std() {
729 assert_eq!(Ok(Duration::seconds(1)), Duration::from_std(StdDuration::new(1, 0)));
730 assert_eq!(Ok(Duration::seconds(86401)), Duration::from_std(StdDuration::new(86401, 0)));
731 assert_eq!(
732 Ok(Duration::milliseconds(123)),
733 Duration::from_std(StdDuration::new(0, 123000000))
734 );
735 assert_eq!(
736 Ok(Duration::milliseconds(123765)),
737 Duration::from_std(StdDuration::new(123, 765000000))
738 );
739 assert_eq!(Ok(Duration::nanoseconds(777)), Duration::from_std(StdDuration::new(0, 777)));
740 assert_eq!(Ok(MAX), Duration::from_std(StdDuration::new(9223372036854775, 807000000)));
741 assert_eq!(
742 Duration::from_std(StdDuration::new(9223372036854776, 0)),
743 Err(OutOfRangeError(()))
744 );
745 assert_eq!(
746 Duration::from_std(StdDuration::new(9223372036854775, 807000001)),
747 Err(OutOfRangeError(()))
748 );
749 }
750}
751