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
14 use std
::error
::Error
;
15 use std
::ops
::{Add, Sub, Mul, Div, Neg, FnOnce}
;
16 use std
::time
::Duration
as StdDuration
;
18 /// The number of nanoseconds in a microsecond.
19 const NANOS_PER_MICRO
: i32 = 1000;
20 /// The number of nanoseconds in a millisecond.
21 const NANOS_PER_MILLI
: i32 = 1000_000;
22 /// The number of nanoseconds in seconds.
23 const NANOS_PER_SEC
: i32 = 1_000_000_000;
24 /// The number of microseconds per second.
25 const MICROS_PER_SEC
: i64 = 1000_000;
26 /// The number of milliseconds per second.
27 const MILLIS_PER_SEC
: i64 = 1000;
28 /// The number of seconds in a minute.
29 const SECS_PER_MINUTE
: i64 = 60;
30 /// The number of seconds in an hour.
31 const SECS_PER_HOUR
: i64 = 3600;
32 /// The number of (non-leap) seconds in days.
33 const SECS_PER_DAY
: i64 = 86400;
34 /// The number of (non-leap) seconds in a week.
35 const SECS_PER_WEEK
: i64 = 604800;
37 macro_rules
! try_opt
{
38 ($e
:expr
) => (match $e { Some(v) => v, None => return None }
)
42 /// ISO 8601 time duration with nanosecond precision.
43 /// This also allows for the negative duration; see individual methods for details.
44 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
47 nanos
: i32, // Always 0 <= nanos < NANOS_PER_SEC
50 /// The minimum possible `Duration`: `i64::MIN` milliseconds.
51 pub const MIN
: Duration
= Duration
{
52 secs
: i64::MIN
/ MILLIS_PER_SEC
- 1,
53 nanos
: NANOS_PER_SEC
+ (i64::MIN
% MILLIS_PER_SEC
) as i32 * NANOS_PER_MILLI
56 /// The maximum possible `Duration`: `i64::MAX` milliseconds.
57 pub const MAX
: Duration
= Duration
{
58 secs
: i64::MAX
/ MILLIS_PER_SEC
,
59 nanos
: (i64::MAX
% MILLIS_PER_SEC
) as i32 * NANOS_PER_MILLI
63 /// Makes a new `Duration` with given number of weeks.
64 /// Equivalent to `Duration::seconds(weeks * 7 * 24 * 60 * 60)` with overflow checks.
65 /// Panics when the duration is out of bounds.
67 pub fn weeks(weeks
: i64) -> Duration
{
68 let secs
= weeks
.checked_mul(SECS_PER_WEEK
).expect("Duration::weeks out of bounds");
69 Duration
::seconds(secs
)
72 /// Makes a new `Duration` with given number of days.
73 /// Equivalent to `Duration::seconds(days * 24 * 60 * 60)` with overflow checks.
74 /// Panics when the duration is out of bounds.
76 pub fn days(days
: i64) -> Duration
{
77 let secs
= days
.checked_mul(SECS_PER_DAY
).expect("Duration::days out of bounds");
78 Duration
::seconds(secs
)
81 /// Makes a new `Duration` with given number of hours.
82 /// Equivalent to `Duration::seconds(hours * 60 * 60)` with overflow checks.
83 /// Panics when the duration is out of bounds.
85 pub fn hours(hours
: i64) -> Duration
{
86 let secs
= hours
.checked_mul(SECS_PER_HOUR
).expect("Duration::hours out of bounds");
87 Duration
::seconds(secs
)
90 /// Makes a new `Duration` with given number of minutes.
91 /// Equivalent to `Duration::seconds(minutes * 60)` with overflow checks.
92 /// Panics when the duration is out of bounds.
94 pub fn minutes(minutes
: i64) -> Duration
{
95 let secs
= minutes
.checked_mul(SECS_PER_MINUTE
).expect("Duration::minutes out of bounds");
96 Duration
::seconds(secs
)
99 /// Makes a new `Duration` with given number of seconds.
100 /// Panics when the duration is more than `i64::MAX` milliseconds
101 /// or less than `i64::MIN` milliseconds.
103 pub fn seconds(seconds
: i64) -> Duration
{
104 let d
= Duration { secs: seconds, nanos: 0 }
;
105 if d
< MIN
|| d
> MAX
{
106 panic
!("Duration::seconds out of bounds");
111 /// Makes a new `Duration` with given number of milliseconds.
113 pub fn milliseconds(milliseconds
: i64) -> Duration
{
114 let (secs
, millis
) = div_mod_floor_64(milliseconds
, MILLIS_PER_SEC
);
115 let nanos
= millis
as i32 * NANOS_PER_MILLI
;
116 Duration { secs: secs, nanos: nanos }
119 /// Makes a new `Duration` with given number of microseconds.
121 pub fn microseconds(microseconds
: i64) -> Duration
{
122 let (secs
, micros
) = div_mod_floor_64(microseconds
, MICROS_PER_SEC
);
123 let nanos
= micros
as i32 * NANOS_PER_MICRO
;
124 Duration { secs: secs, nanos: nanos }
127 /// Makes a new `Duration` with given number of nanoseconds.
129 pub fn nanoseconds(nanos
: i64) -> Duration
{
130 let (secs
, nanos
) = div_mod_floor_64(nanos
, NANOS_PER_SEC
as i64);
131 Duration { secs: secs, nanos: nanos as i32 }
134 /// Runs a closure, returning the duration of time it took to run the
136 pub fn span
<F
>(f
: F
) -> Duration
where F
: FnOnce() {
137 let before
= super::precise_time_ns();
139 Duration
::nanoseconds((super::precise_time_ns() - before
) as i64)
142 /// Returns the total number of whole weeks in the duration.
144 pub fn num_weeks(&self) -> i64 {
148 /// Returns the total number of whole days in the duration.
149 pub fn num_days(&self) -> i64 {
150 self.num_seconds() / SECS_PER_DAY
153 /// Returns the total number of whole hours in the duration.
155 pub fn num_hours(&self) -> i64 {
156 self.num_seconds() / SECS_PER_HOUR
159 /// Returns the total number of whole minutes in the duration.
161 pub fn num_minutes(&self) -> i64 {
162 self.num_seconds() / SECS_PER_MINUTE
165 /// Returns the total number of whole seconds in the duration.
166 pub fn num_seconds(&self) -> i64 {
167 // If secs is negative, nanos should be subtracted from the duration.
168 if self.secs
< 0 && self.nanos
> 0 {
175 /// Returns the number of nanoseconds such that
176 /// `nanos_mod_sec() + num_seconds() * NANOS_PER_SEC` is the total number of
177 /// nanoseconds in the duration.
178 fn nanos_mod_sec(&self) -> i32 {
179 if self.secs
< 0 && self.nanos
> 0 {
180 self.nanos
- NANOS_PER_SEC
186 /// Returns the total number of whole milliseconds in the duration,
187 pub fn num_milliseconds(&self) -> i64 {
188 // A proper Duration will not overflow, because MIN and MAX are defined
189 // such that the range is exactly i64 milliseconds.
190 let secs_part
= self.num_seconds() * MILLIS_PER_SEC
;
191 let nanos_part
= self.nanos_mod_sec() / NANOS_PER_MILLI
;
192 secs_part
+ nanos_part
as i64
195 /// Returns the total number of whole microseconds in the duration,
196 /// or `None` on overflow (exceeding 2<sup>63</sup> microseconds in either direction).
197 pub fn num_microseconds(&self) -> Option
<i64> {
198 let secs_part
= try_opt
!(self.num_seconds().checked_mul(MICROS_PER_SEC
));
199 let nanos_part
= self.nanos_mod_sec() / NANOS_PER_MICRO
;
200 secs_part
.checked_add(nanos_part
as i64)
203 /// Returns the total number of whole nanoseconds in the duration,
204 /// or `None` on overflow (exceeding 2<sup>63</sup> nanoseconds in either direction).
205 pub fn num_nanoseconds(&self) -> Option
<i64> {
206 let secs_part
= try_opt
!(self.num_seconds().checked_mul(NANOS_PER_SEC
as i64));
207 let nanos_part
= self.nanos_mod_sec();
208 secs_part
.checked_add(nanos_part
as i64)
211 /// Add two durations, returning `None` if overflow occurred.
212 pub fn checked_add(&self, rhs
: &Duration
) -> Option
<Duration
> {
213 let mut secs
= try_opt
!(self.secs
.checked_add(rhs
.secs
));
214 let mut nanos
= self.nanos
+ rhs
.nanos
;
215 if nanos
>= NANOS_PER_SEC
{
216 nanos
-= NANOS_PER_SEC
;
217 secs
= try_opt
!(secs
.checked_add(1));
219 let d
= Duration { secs: secs, nanos: nanos }
;
220 // Even if d is within the bounds of i64 seconds,
221 // it might still overflow i64 milliseconds.
222 if d
< MIN
|| d
> MAX { None }
else { Some(d) }
225 /// Subtract two durations, returning `None` if overflow occurred.
226 pub fn checked_sub(&self, rhs
: &Duration
) -> Option
<Duration
> {
227 let mut secs
= try_opt
!(self.secs
.checked_sub(rhs
.secs
));
228 let mut nanos
= self.nanos
- rhs
.nanos
;
230 nanos
+= NANOS_PER_SEC
;
231 secs
= try_opt
!(secs
.checked_sub(1));
233 let d
= Duration { secs: secs, nanos: nanos }
;
234 // Even if d is within the bounds of i64 seconds,
235 // it might still overflow i64 milliseconds.
236 if d
< MIN
|| d
> MAX { None }
else { Some(d) }
239 /// The minimum possible `Duration`: `i64::MIN` milliseconds.
241 pub fn min_value() -> Duration { MIN }
243 /// The maximum possible `Duration`: `i64::MAX` milliseconds.
245 pub fn max_value() -> Duration { MAX }
247 /// A duration where the stored seconds and nanoseconds are equal to zero.
249 pub fn zero() -> Duration
{
250 Duration { secs: 0, nanos: 0 }
253 /// Returns `true` if the duration equals `Duration::zero()`.
255 pub fn is_zero(&self) -> bool
{
256 self.secs
== 0 && self.nanos
== 0
259 /// Creates a `time::Duration` object from `std::time::Duration`
261 /// This function errors when original duration is larger than the maximum
262 /// value supported for this type.
263 pub fn from_std(duration
: StdDuration
) -> Result
<Duration
, OutOfRangeError
> {
264 // We need to check secs as u64 before coercing to i64
265 if duration
.as_secs() > MAX
.secs
as u64 {
266 return Err(OutOfRangeError(()));
269 secs
: duration
.as_secs() as i64,
270 nanos
: duration
.subsec_nanos() as i32,
273 return Err(OutOfRangeError(()));
278 /// Creates a `std::time::Duration` object from `time::Duration`
280 /// This function errors when duration is less than zero. As standard
281 /// library implementation is limited to non-negative values.
282 pub fn to_std(&self) -> Result
<StdDuration
, OutOfRangeError
> {
284 return Err(OutOfRangeError(()));
286 Ok(StdDuration
::new(self.secs
as u64, self.nanos
as u32))
289 /// Returns the raw value of duration.
290 #[cfg(target_env = "sgx")]
291 pub(crate) fn raw(&self) -> (i64, i32) {
292 (self.secs
, self.nanos
)
296 impl Neg
for Duration
{
297 type Output
= Duration
;
300 fn neg(self) -> Duration
{
302 Duration { secs: -self.secs, nanos: 0 }
304 Duration { secs: -self.secs - 1, nanos: NANOS_PER_SEC - self.nanos }
309 impl Add
for Duration
{
310 type Output
= Duration
;
312 fn add(self, rhs
: Duration
) -> Duration
{
313 let mut secs
= self.secs
+ rhs
.secs
;
314 let mut nanos
= self.nanos
+ rhs
.nanos
;
315 if nanos
>= NANOS_PER_SEC
{
316 nanos
-= NANOS_PER_SEC
;
319 Duration { secs: secs, nanos: nanos }
323 impl Sub
for Duration
{
324 type Output
= Duration
;
326 fn sub(self, rhs
: Duration
) -> Duration
{
327 let mut secs
= self.secs
- rhs
.secs
;
328 let mut nanos
= self.nanos
- rhs
.nanos
;
330 nanos
+= NANOS_PER_SEC
;
333 Duration { secs: secs, nanos: nanos }
337 impl Mul
<i32> for Duration
{
338 type Output
= Duration
;
340 fn mul(self, rhs
: i32) -> Duration
{
341 // Multiply nanoseconds as i64, because it cannot overflow that way.
342 let total_nanos
= self.nanos
as i64 * rhs
as i64;
343 let (extra_secs
, nanos
) = div_mod_floor_64(total_nanos
, NANOS_PER_SEC
as i64);
344 let secs
= self.secs
* rhs
as i64 + extra_secs
;
345 Duration { secs: secs, nanos: nanos as i32 }
349 impl Div
<i32> for Duration
{
350 type Output
= Duration
;
352 fn div(self, rhs
: i32) -> Duration
{
353 let mut secs
= self.secs
/ rhs
as i64;
354 let carry
= self.secs
- secs
* rhs
as i64;
355 let extra_nanos
= carry
* NANOS_PER_SEC
as i64 / rhs
as i64;
356 let mut nanos
= self.nanos
/ rhs
+ extra_nanos
as i32;
357 if nanos
>= NANOS_PER_SEC
{
358 nanos
-= NANOS_PER_SEC
;
362 nanos
+= NANOS_PER_SEC
;
365 Duration { secs: secs, nanos: nanos }
369 impl fmt
::Display
for Duration
{
370 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
371 // technically speaking, negative duration is not valid ISO 8601,
372 // but we need to print it anyway.
373 let (abs
, sign
) = if self.secs
< 0 { (-*self, "-") }
else { (*self, "") }
;
375 let days
= abs
.secs
/ SECS_PER_DAY
;
376 let secs
= abs
.secs
- days
* SECS_PER_DAY
;
377 let hasdate
= days
!= 0;
378 let hastime
= (secs
!= 0 || abs
.nanos
!= 0) || !hasdate
;
380 write
!(f
, "{}P", sign
)?
;
383 write
!(f
, "{}D", days
)?
;
387 write
!(f
, "T{}S", secs
)?
;
388 } else if abs
.nanos
% NANOS_PER_MILLI
== 0 {
389 write
!(f
, "T{}.{:03}S", secs
, abs
.nanos
/ NANOS_PER_MILLI
)?
;
390 } else if abs
.nanos
% NANOS_PER_MICRO
== 0 {
391 write
!(f
, "T{}.{:06}S", secs
, abs
.nanos
/ NANOS_PER_MICRO
)?
;
393 write
!(f
, "T{}.{:09}S", secs
, abs
.nanos
)?
;
400 /// Represents error when converting `Duration` to/from a standard library
403 /// The `std::time::Duration` supports a range from zero to `u64::MAX`
404 /// *seconds*, while this module supports signed range of up to
405 /// `i64::MAX` of *milliseconds*.
406 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
407 pub struct OutOfRangeError(());
409 impl fmt
::Display
for OutOfRangeError
{
411 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
412 write
!(f
, "{}", self.description())
416 impl Error
for OutOfRangeError
{
417 fn description(&self) -> &str {
418 "Source duration value is out of range for the target type"
422 // Copied from libnum
424 fn div_mod_floor_64(this
: i64, other
: i64) -> (i64, i64) {
425 (div_floor_64(this
, other
), mod_floor_64(this
, other
))
429 fn div_floor_64(this
: i64, other
: i64) -> i64 {
430 match div_rem_64(this
, other
) {
431 (d
, r
) if (r
> 0 && other
< 0)
432 || (r
< 0 && other
> 0) => d
- 1,
438 fn mod_floor_64(this
: i64, other
: i64) -> i64 {
440 r
if (r
> 0 && other
< 0)
441 || (r
< 0 && other
> 0) => r
+ other
,
447 fn div_rem_64(this
: i64, other
: i64) -> (i64, i64) {
448 (this
/ other
, this
% other
)
453 use super::{Duration, MIN, MAX, OutOfRangeError}
;
455 use std
::time
::Duration
as StdDuration
;
459 assert
!(Duration
::seconds(1) != Duration
::zero());
460 assert_eq
!(Duration
::seconds(1) + Duration
::seconds(2), Duration
::seconds(3));
461 assert_eq
!(Duration
::seconds(86399) + Duration
::seconds(4),
462 Duration
::days(1) + Duration
::seconds(3));
463 assert_eq
!(Duration
::days(10) - Duration
::seconds(1000), Duration
::seconds(863000));
464 assert_eq
!(Duration
::days(10) - Duration
::seconds(1000000), Duration
::seconds(-136000));
465 assert_eq
!(Duration
::days(2) + Duration
::seconds(86399) +
466 Duration
::nanoseconds(1234567890),
467 Duration
::days(3) + Duration
::nanoseconds(234567890));
468 assert_eq
!(-Duration
::days(3), Duration
::days(-3));
469 assert_eq
!(-(Duration
::days(3) + Duration
::seconds(70)),
470 Duration
::days(-4) + Duration
::seconds(86400-70));
474 fn test_duration_num_days() {
475 assert_eq
!(Duration
::zero().num_days(), 0);
476 assert_eq
!(Duration
::days(1).num_days(), 1);
477 assert_eq
!(Duration
::days(-1).num_days(), -1);
478 assert_eq
!(Duration
::seconds(86399).num_days(), 0);
479 assert_eq
!(Duration
::seconds(86401).num_days(), 1);
480 assert_eq
!(Duration
::seconds(-86399).num_days(), 0);
481 assert_eq
!(Duration
::seconds(-86401).num_days(), -1);
482 assert_eq
!(Duration
::days(i32::MAX
as i64).num_days(), i32::MAX
as i64);
483 assert_eq
!(Duration
::days(i32::MIN
as i64).num_days(), i32::MIN
as i64);
487 fn test_duration_num_seconds() {
488 assert_eq
!(Duration
::zero().num_seconds(), 0);
489 assert_eq
!(Duration
::seconds(1).num_seconds(), 1);
490 assert_eq
!(Duration
::seconds(-1).num_seconds(), -1);
491 assert_eq
!(Duration
::milliseconds(999).num_seconds(), 0);
492 assert_eq
!(Duration
::milliseconds(1001).num_seconds(), 1);
493 assert_eq
!(Duration
::milliseconds(-999).num_seconds(), 0);
494 assert_eq
!(Duration
::milliseconds(-1001).num_seconds(), -1);
498 fn test_duration_num_milliseconds() {
499 assert_eq
!(Duration
::zero().num_milliseconds(), 0);
500 assert_eq
!(Duration
::milliseconds(1).num_milliseconds(), 1);
501 assert_eq
!(Duration
::milliseconds(-1).num_milliseconds(), -1);
502 assert_eq
!(Duration
::microseconds(999).num_milliseconds(), 0);
503 assert_eq
!(Duration
::microseconds(1001).num_milliseconds(), 1);
504 assert_eq
!(Duration
::microseconds(-999).num_milliseconds(), 0);
505 assert_eq
!(Duration
::microseconds(-1001).num_milliseconds(), -1);
506 assert_eq
!(Duration
::milliseconds(i64::MAX
).num_milliseconds(), i64::MAX
);
507 assert_eq
!(Duration
::milliseconds(i64::MIN
).num_milliseconds(), i64::MIN
);
508 assert_eq
!(MAX
.num_milliseconds(), i64::MAX
);
509 assert_eq
!(MIN
.num_milliseconds(), i64::MIN
);
513 fn test_duration_num_microseconds() {
514 assert_eq
!(Duration
::zero().num_microseconds(), Some(0));
515 assert_eq
!(Duration
::microseconds(1).num_microseconds(), Some(1));
516 assert_eq
!(Duration
::microseconds(-1).num_microseconds(), Some(-1));
517 assert_eq
!(Duration
::nanoseconds(999).num_microseconds(), Some(0));
518 assert_eq
!(Duration
::nanoseconds(1001).num_microseconds(), Some(1));
519 assert_eq
!(Duration
::nanoseconds(-999).num_microseconds(), Some(0));
520 assert_eq
!(Duration
::nanoseconds(-1001).num_microseconds(), Some(-1));
521 assert_eq
!(Duration
::microseconds(i64::MAX
).num_microseconds(), Some(i64::MAX
));
522 assert_eq
!(Duration
::microseconds(i64::MIN
).num_microseconds(), Some(i64::MIN
));
523 assert_eq
!(MAX
.num_microseconds(), None
);
524 assert_eq
!(MIN
.num_microseconds(), None
);
527 const MICROS_PER_DAY
: i64 = 86400_000_000;
528 assert_eq
!(Duration
::days(i64::MAX
/ MICROS_PER_DAY
).num_microseconds(),
529 Some(i64::MAX
/ MICROS_PER_DAY
* MICROS_PER_DAY
));
530 assert_eq
!(Duration
::days(i64::MIN
/ MICROS_PER_DAY
).num_microseconds(),
531 Some(i64::MIN
/ MICROS_PER_DAY
* MICROS_PER_DAY
));
532 assert_eq
!(Duration
::days(i64::MAX
/ MICROS_PER_DAY
+ 1).num_microseconds(), None
);
533 assert_eq
!(Duration
::days(i64::MIN
/ MICROS_PER_DAY
- 1).num_microseconds(), None
);
537 fn test_duration_num_nanoseconds() {
538 assert_eq
!(Duration
::zero().num_nanoseconds(), Some(0));
539 assert_eq
!(Duration
::nanoseconds(1).num_nanoseconds(), Some(1));
540 assert_eq
!(Duration
::nanoseconds(-1).num_nanoseconds(), Some(-1));
541 assert_eq
!(Duration
::nanoseconds(i64::MAX
).num_nanoseconds(), Some(i64::MAX
));
542 assert_eq
!(Duration
::nanoseconds(i64::MIN
).num_nanoseconds(), Some(i64::MIN
));
543 assert_eq
!(MAX
.num_nanoseconds(), None
);
544 assert_eq
!(MIN
.num_nanoseconds(), None
);
547 const NANOS_PER_DAY
: i64 = 86400_000_000_000;
548 assert_eq
!(Duration
::days(i64::MAX
/ NANOS_PER_DAY
).num_nanoseconds(),
549 Some(i64::MAX
/ NANOS_PER_DAY
* NANOS_PER_DAY
));
550 assert_eq
!(Duration
::days(i64::MIN
/ NANOS_PER_DAY
).num_nanoseconds(),
551 Some(i64::MIN
/ NANOS_PER_DAY
* NANOS_PER_DAY
));
552 assert_eq
!(Duration
::days(i64::MAX
/ NANOS_PER_DAY
+ 1).num_nanoseconds(), None
);
553 assert_eq
!(Duration
::days(i64::MIN
/ NANOS_PER_DAY
- 1).num_nanoseconds(), None
);
557 fn test_duration_checked_ops() {
558 assert_eq
!(Duration
::milliseconds(i64::MAX
- 1).checked_add(&Duration
::microseconds(999)),
559 Some(Duration
::milliseconds(i64::MAX
- 2) + Duration
::microseconds(1999)));
560 assert
!(Duration
::milliseconds(i64::MAX
).checked_add(&Duration
::microseconds(1000))
563 assert_eq
!(Duration
::milliseconds(i64::MIN
).checked_sub(&Duration
::milliseconds(0)),
564 Some(Duration
::milliseconds(i64::MIN
)));
565 assert
!(Duration
::milliseconds(i64::MIN
).checked_sub(&Duration
::milliseconds(1))
570 fn test_duration_mul() {
571 assert_eq
!(Duration
::zero() * i32::MAX
, Duration
::zero());
572 assert_eq
!(Duration
::zero() * i32::MIN
, Duration
::zero());
573 assert_eq
!(Duration
::nanoseconds(1) * 0, Duration
::zero());
574 assert_eq
!(Duration
::nanoseconds(1) * 1, Duration
::nanoseconds(1));
575 assert_eq
!(Duration
::nanoseconds(1) * 1_000_000_000, Duration
::seconds(1));
576 assert_eq
!(Duration
::nanoseconds(1) * -1_000_000_000, -Duration
::seconds(1));
577 assert_eq
!(-Duration
::nanoseconds(1) * 1_000_000_000, -Duration
::seconds(1));
578 assert_eq
!(Duration
::nanoseconds(30) * 333_333_333,
579 Duration
::seconds(10) - Duration
::nanoseconds(10));
580 assert_eq
!((Duration
::nanoseconds(1) + Duration
::seconds(1) + Duration
::days(1)) * 3,
581 Duration
::nanoseconds(3) + Duration
::seconds(3) + Duration
::days(3));
582 assert_eq
!(Duration
::milliseconds(1500) * -2, Duration
::seconds(-3));
583 assert_eq
!(Duration
::milliseconds(-1500) * 2, Duration
::seconds(-3));
587 fn test_duration_div() {
588 assert_eq
!(Duration
::zero() / i32::MAX
, Duration
::zero());
589 assert_eq
!(Duration
::zero() / i32::MIN
, Duration
::zero());
590 assert_eq
!(Duration
::nanoseconds(123_456_789) / 1, Duration
::nanoseconds(123_456_789));
591 assert_eq
!(Duration
::nanoseconds(123_456_789) / -1, -Duration
::nanoseconds(123_456_789));
592 assert_eq
!(-Duration
::nanoseconds(123_456_789) / -1, Duration
::nanoseconds(123_456_789));
593 assert_eq
!(-Duration
::nanoseconds(123_456_789) / 1, -Duration
::nanoseconds(123_456_789));
594 assert_eq
!(Duration
::seconds(1) / 3, Duration
::nanoseconds(333_333_333));
595 assert_eq
!(Duration
::seconds(4) / 3, Duration
::nanoseconds(1_333_333_333));
596 assert_eq
!(Duration
::seconds(-1) / 2, Duration
::milliseconds(-500));
597 assert_eq
!(Duration
::seconds(1) / -2, Duration
::milliseconds(-500));
598 assert_eq
!(Duration
::seconds(-1) / -2, Duration
::milliseconds(500));
599 assert_eq
!(Duration
::seconds(-4) / 3, Duration
::nanoseconds(-1_333_333_333));
600 assert_eq
!(Duration
::seconds(-4) / -3, Duration
::nanoseconds(1_333_333_333));
604 fn test_duration_fmt() {
605 assert_eq
!(Duration
::zero().to_string(), "PT0S");
606 assert_eq
!(Duration
::days(42).to_string(), "P42D");
607 assert_eq
!(Duration
::days(-42).to_string(), "-P42D");
608 assert_eq
!(Duration
::seconds(42).to_string(), "PT42S");
609 assert_eq
!(Duration
::milliseconds(42).to_string(), "PT0.042S");
610 assert_eq
!(Duration
::microseconds(42).to_string(), "PT0.000042S");
611 assert_eq
!(Duration
::nanoseconds(42).to_string(), "PT0.000000042S");
612 assert_eq
!((Duration
::days(7) + Duration
::milliseconds(6543)).to_string(),
614 assert_eq
!(Duration
::seconds(-86401).to_string(), "-P1DT1S");
615 assert_eq
!(Duration
::nanoseconds(-1).to_string(), "-PT0.000000001S");
617 // the format specifier should have no effect on `Duration`
618 assert_eq
!(format
!("{:30}", Duration
::days(1) + Duration
::milliseconds(2345)),
624 assert_eq
!(Duration
::seconds(1).to_std(), Ok(StdDuration
::new(1, 0)));
625 assert_eq
!(Duration
::seconds(86401).to_std(), Ok(StdDuration
::new(86401, 0)));
626 assert_eq
!(Duration
::milliseconds(123).to_std(), Ok(StdDuration
::new(0, 123000000)));
627 assert_eq
!(Duration
::milliseconds(123765).to_std(), Ok(StdDuration
::new(123, 765000000)));
628 assert_eq
!(Duration
::nanoseconds(777).to_std(), Ok(StdDuration
::new(0, 777)));
629 assert_eq
!(MAX
.to_std(), Ok(StdDuration
::new(9223372036854775, 807000000)));
630 assert_eq
!(Duration
::seconds(-1).to_std(),
631 Err(OutOfRangeError(())));
632 assert_eq
!(Duration
::milliseconds(-1).to_std(),
633 Err(OutOfRangeError(())));
638 assert_eq
!(Ok(Duration
::seconds(1)),
639 Duration
::from_std(StdDuration
::new(1, 0)));
640 assert_eq
!(Ok(Duration
::seconds(86401)),
641 Duration
::from_std(StdDuration
::new(86401, 0)));
642 assert_eq
!(Ok(Duration
::milliseconds(123)),
643 Duration
::from_std(StdDuration
::new(0, 123000000)));
644 assert_eq
!(Ok(Duration
::milliseconds(123765)),
645 Duration
::from_std(StdDuration
::new(123, 765000000)));
646 assert_eq
!(Ok(Duration
::nanoseconds(777)),
647 Duration
::from_std(StdDuration
::new(0, 777)));
649 Duration
::from_std(StdDuration
::new(9223372036854775, 807000000)));
650 assert_eq
!(Duration
::from_std(StdDuration
::new(9223372036854776, 0)),
651 Err(OutOfRangeError(())));
652 assert_eq
!(Duration
::from_std(StdDuration
::new(9223372036854775, 807000001)),
653 Err(OutOfRangeError(())));