Coverage Report

Created: 2021-11-03 07:11

/rust/registry/src/github.com-1ecc6299db9ec823/time-0.1.44/src/duration.rs
Line
Count
Source (jump to first uncovered line)
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
13
use std::{fmt, i64};
14
use std::error::Error;
15
use std::ops::{Add, Sub, Mul, Div, Neg, FnOnce};
16
use std::time::Duration as StdDuration;
17
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;
36
37
macro_rules! try_opt {
38
    ($e:expr) => (match $e { Some(v) => v, None => return None })
39
}
40
41
42
/// ISO 8601 time duration with nanosecond precision.
43
/// This also allows for the negative duration; see individual methods for details.
44
0
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
Unexecuted instantiation: <time::duration::Duration as core::cmp::PartialEq>::eq
Unexecuted instantiation: <time::duration::Duration as core::cmp::PartialEq>::ne
45
pub struct Duration {
46
    secs: i64,
47
    nanos: i32, // Always 0 <= nanos < NANOS_PER_SEC
48
}
49
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
54
};
55
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
60
};
61
62
impl Duration {
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.
66
    #[inline]
67
0
    pub fn weeks(weeks: i64) -> Duration {
68
0
        let secs = weeks.checked_mul(SECS_PER_WEEK).expect("Duration::weeks out of bounds");
69
0
        Duration::seconds(secs)
70
0
    }
71
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.
75
    #[inline]
76
0
    pub fn days(days: i64) -> Duration {
77
0
        let secs = days.checked_mul(SECS_PER_DAY).expect("Duration::days out of bounds");
78
0
        Duration::seconds(secs)
79
0
    }
80
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.
84
    #[inline]
85
0
    pub fn hours(hours: i64) -> Duration {
86
0
        let secs = hours.checked_mul(SECS_PER_HOUR).expect("Duration::hours out of bounds");
87
0
        Duration::seconds(secs)
88
0
    }
89
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.
93
    #[inline]
94
0
    pub fn minutes(minutes: i64) -> Duration {
95
0
        let secs = minutes.checked_mul(SECS_PER_MINUTE).expect("Duration::minutes out of bounds");
96
0
        Duration::seconds(secs)
97
0
    }
98
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.
102
    #[inline]
103
0
    pub fn seconds(seconds: i64) -> Duration {
104
0
        let d = Duration { secs: seconds, nanos: 0 };
105
0
        if d < MIN || d > MAX {
106
0
            panic!("Duration::seconds out of bounds");
107
0
        }
108
0
        d
109
0
    }
110
111
    /// Makes a new `Duration` with given number of milliseconds.
112
    #[inline]
113
0
    pub fn milliseconds(milliseconds: i64) -> Duration {
114
0
        let (secs, millis) = div_mod_floor_64(milliseconds, MILLIS_PER_SEC);
115
0
        let nanos = millis as i32 * NANOS_PER_MILLI;
116
0
        Duration { secs: secs, nanos: nanos }
117
0
    }
118
119
    /// Makes a new `Duration` with given number of microseconds.
120
    #[inline]
121
0
    pub fn microseconds(microseconds: i64) -> Duration {
122
0
        let (secs, micros) = div_mod_floor_64(microseconds, MICROS_PER_SEC);
123
0
        let nanos = micros as i32 * NANOS_PER_MICRO;
124
0
        Duration { secs: secs, nanos: nanos }
125
0
    }
126
127
    /// Makes a new `Duration` with given number of nanoseconds.
128
    #[inline]
129
0
    pub fn nanoseconds(nanos: i64) -> Duration {
130
0
        let (secs, nanos) = div_mod_floor_64(nanos, NANOS_PER_SEC as i64);
131
0
        Duration { secs: secs, nanos: nanos as i32 }
132
0
    }
133
134
    /// Runs a closure, returning the duration of time it took to run the
135
    /// closure.
136
0
    pub fn span<F>(f: F) -> Duration where F: FnOnce() {
137
0
        let before = super::precise_time_ns();
138
0
        f();
139
0
        Duration::nanoseconds((super::precise_time_ns() - before) as i64)
140
0
    }
141
142
    /// Returns the total number of whole weeks in the duration.
143
    #[inline]
144
0
    pub fn num_weeks(&self) -> i64 {
145
0
        self.num_days() / 7
146
0
    }
147
148
    /// Returns the total number of whole days in the duration.
149
0
    pub fn num_days(&self) -> i64 {
150
0
        self.num_seconds() / SECS_PER_DAY
151
0
    }
152
153
    /// Returns the total number of whole hours in the duration.
154
    #[inline]
155
0
    pub fn num_hours(&self) -> i64 {
156
0
        self.num_seconds() / SECS_PER_HOUR
157
0
    }
158
159
    /// Returns the total number of whole minutes in the duration.
160
    #[inline]
161
0
    pub fn num_minutes(&self) -> i64 {
162
0
        self.num_seconds() / SECS_PER_MINUTE
163
0
    }
164
165
    /// Returns the total number of whole seconds in the duration.
166
0
    pub fn num_seconds(&self) -> i64 {
167
0
        // If secs is negative, nanos should be subtracted from the duration.
168
0
        if self.secs < 0 && self.nanos > 0 {
169
0
            self.secs + 1
170
        } else {
171
0
            self.secs
172
        }
173
0
    }
174
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
0
    fn nanos_mod_sec(&self) -> i32 {
179
0
        if self.secs < 0 && self.nanos > 0 {
180
0
            self.nanos - NANOS_PER_SEC
181
        } else {
182
0
            self.nanos
183
        }
184
0
    }
185
186
    /// Returns the total number of whole milliseconds in the duration,
187
0
    pub fn num_milliseconds(&self) -> i64 {
188
0
        // A proper Duration will not overflow, because MIN and MAX are defined
189
0
        // such that the range is exactly i64 milliseconds.
190
0
        let secs_part = self.num_seconds() * MILLIS_PER_SEC;
191
0
        let nanos_part = self.nanos_mod_sec() / NANOS_PER_MILLI;
192
0
        secs_part + nanos_part as i64
193
0
    }
194
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
0
    pub fn num_microseconds(&self) -> Option<i64> {
198
0
        let secs_part = try_opt!(self.num_seconds().checked_mul(MICROS_PER_SEC));
199
0
        let nanos_part = self.nanos_mod_sec() / NANOS_PER_MICRO;
200
0
        secs_part.checked_add(nanos_part as i64)
201
0
    }
202
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
0
    pub fn num_nanoseconds(&self) -> Option<i64> {
206
0
        let secs_part = try_opt!(self.num_seconds().checked_mul(NANOS_PER_SEC as i64));
207
0
        let nanos_part = self.nanos_mod_sec();
208
0
        secs_part.checked_add(nanos_part as i64)
209
0
    }
210
211
    /// Add two durations, returning `None` if overflow occurred.
212
0
    pub fn checked_add(&self, rhs: &Duration) -> Option<Duration> {
213
0
        let mut secs = try_opt!(self.secs.checked_add(rhs.secs));
214
0
        let mut nanos = self.nanos + rhs.nanos;
215
0
        if nanos >= NANOS_PER_SEC {
216
0
            nanos -= NANOS_PER_SEC;
217
0
            secs = try_opt!(secs.checked_add(1));
218
0
        }
219
0
        let d = Duration { secs: secs, nanos: nanos };
220
0
        // Even if d is within the bounds of i64 seconds,
221
0
        // it might still overflow i64 milliseconds.
222
0
        if d < MIN || d > MAX { None } else { Some(d) }
223
0
    }
224
225
    /// Subtract two durations, returning `None` if overflow occurred.
226
0
    pub fn checked_sub(&self, rhs: &Duration) -> Option<Duration> {
227
0
        let mut secs = try_opt!(self.secs.checked_sub(rhs.secs));
228
0
        let mut nanos = self.nanos - rhs.nanos;
229
0
        if nanos < 0 {
230
0
            nanos += NANOS_PER_SEC;
231
0
            secs = try_opt!(secs.checked_sub(1));
232
0
        }
233
0
        let d = Duration { secs: secs, nanos: nanos };
234
0
        // Even if d is within the bounds of i64 seconds,
235
0
        // it might still overflow i64 milliseconds.
236
0
        if d < MIN || d > MAX { None } else { Some(d) }
237
0
    }
238
239
    /// The minimum possible `Duration`: `i64::MIN` milliseconds.
240
    #[inline]
241
0
    pub fn min_value() -> Duration { MIN }
242
243
    /// The maximum possible `Duration`: `i64::MAX` milliseconds.
244
    #[inline]
245
0
    pub fn max_value() -> Duration { MAX }
246
247
    /// A duration where the stored seconds and nanoseconds are equal to zero.
248
    #[inline]
249
0
    pub fn zero() -> Duration {
250
0
        Duration { secs: 0, nanos: 0 }
251
0
    }
252
253
    /// Returns `true` if the duration equals `Duration::zero()`.
254
    #[inline]
255
0
    pub fn is_zero(&self) -> bool {
256
0
        self.secs == 0 && self.nanos == 0
257
0
    }
258
259
    /// Creates a `time::Duration` object from `std::time::Duration`
260
    ///
261
    /// This function errors when original duration is larger than the maximum
262
    /// value supported for this type.
263
0
    pub fn from_std(duration: StdDuration) -> Result<Duration, OutOfRangeError> {
264
0
        // We need to check secs as u64 before coercing to i64
265
0
        if duration.as_secs() > MAX.secs as u64 {
266
0
            return Err(OutOfRangeError(()));
267
0
        }
268
0
        let d = Duration {
269
0
            secs: duration.as_secs() as i64,
270
0
            nanos: duration.subsec_nanos() as i32,
271
0
        };
272
0
        if d > MAX {
273
0
            return Err(OutOfRangeError(()));
274
0
        }
275
0
        Ok(d)
276
0
    }
277
278
    /// Creates a `std::time::Duration` object from `time::Duration`
279
    ///
280
    /// This function errors when duration is less than zero. As standard
281
    /// library implementation is limited to non-negative values.
282
0
    pub fn to_std(&self) -> Result<StdDuration, OutOfRangeError> {
283
0
        if self.secs < 0 {
284
0
            return Err(OutOfRangeError(()));
285
0
        }
286
0
        Ok(StdDuration::new(self.secs as u64, self.nanos as u32))
287
0
    }
288
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)
293
    }
294
}
295
296
impl Neg for Duration {
297
    type Output = Duration;
298
299
    #[inline]
300
0
    fn neg(self) -> Duration {
301
0
        if self.nanos == 0 {
302
0
            Duration { secs: -self.secs, nanos: 0 }
303
        } else {
304
0
            Duration { secs: -self.secs - 1, nanos: NANOS_PER_SEC - self.nanos }
305
        }
306
0
    }
307
}
308
309
impl Add for Duration {
310
    type Output = Duration;
311
312
0
    fn add(self, rhs: Duration) -> Duration {
313
0
        let mut secs = self.secs + rhs.secs;
314
0
        let mut nanos = self.nanos + rhs.nanos;
315
0
        if nanos >= NANOS_PER_SEC {
316
0
            nanos -= NANOS_PER_SEC;
317
0
            secs += 1;
318
0
        }
319
0
        Duration { secs: secs, nanos: nanos }
320
0
    }
321
}
322
323
impl Sub for Duration {
324
    type Output = Duration;
325
326
0
    fn sub(self, rhs: Duration) -> Duration {
327
0
        let mut secs = self.secs - rhs.secs;
328
0
        let mut nanos = self.nanos - rhs.nanos;
329
0
        if nanos < 0 {
330
0
            nanos += NANOS_PER_SEC;
331
0
            secs -= 1;
332
0
        }
333
0
        Duration { secs: secs, nanos: nanos }
334
0
    }
335
}
336
337
impl Mul<i32> for Duration {
338
    type Output = Duration;
339
340
0
    fn mul(self, rhs: i32) -> Duration {
341
0
        // Multiply nanoseconds as i64, because it cannot overflow that way.
342
0
        let total_nanos = self.nanos as i64 * rhs as i64;
343
0
        let (extra_secs, nanos) = div_mod_floor_64(total_nanos, NANOS_PER_SEC as i64);
344
0
        let secs = self.secs * rhs as i64 + extra_secs;
345
0
        Duration { secs: secs, nanos: nanos as i32 }
346
0
    }
347
}
348
349
impl Div<i32> for Duration {
350
    type Output = Duration;
351
352
0
    fn div(self, rhs: i32) -> Duration {
353
0
        let mut secs = self.secs / rhs as i64;
354
0
        let carry = self.secs - secs * rhs as i64;
355
0
        let extra_nanos = carry * NANOS_PER_SEC as i64 / rhs as i64;
356
0
        let mut nanos = self.nanos / rhs + extra_nanos as i32;
357
0
        if nanos >= NANOS_PER_SEC {
358
0
            nanos -= NANOS_PER_SEC;
359
0
            secs += 1;
360
0
        }
361
0
        if nanos < 0 {
362
0
            nanos += NANOS_PER_SEC;
363
0
            secs -= 1;
364
0
        }
365
0
        Duration { secs: secs, nanos: nanos }
366
0
    }
367
}
368
369
impl fmt::Display for Duration {
370
0
    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
0
        let (abs, sign) = if self.secs < 0 { (-*self, "-") } else { (*self, "") };
374
375
0
        let days = abs.secs / SECS_PER_DAY;
376
0
        let secs = abs.secs - days * SECS_PER_DAY;
377
0
        let hasdate = days != 0;
378
0
        let hastime = (secs != 0 || abs.nanos != 0) || !hasdate;
379
380
0
        write!(f, "{}P", sign)?;
381
382
0
        if hasdate {
383
0
            write!(f, "{}D", days)?;
384
0
        }
385
0
        if hastime {
386
0
            if abs.nanos == 0 {
387
0
                write!(f, "T{}S", secs)?;
388
0
            } else if abs.nanos % NANOS_PER_MILLI == 0 {
389
0
                write!(f, "T{}.{:03}S", secs, abs.nanos / NANOS_PER_MILLI)?;
390
0
            } else if abs.nanos % NANOS_PER_MICRO == 0 {
391
0
                write!(f, "T{}.{:06}S", secs, abs.nanos / NANOS_PER_MICRO)?;
392
            } else {
393
0
                write!(f, "T{}.{:09}S", secs, abs.nanos)?;
394
            }
395
0
        }
396
0
        Ok(())
397
0
    }
398
}
399
400
/// Represents error when converting `Duration` to/from a standard library
401
/// implementation
402
///
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
0
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
Unexecuted instantiation: <time::duration::OutOfRangeError as core::cmp::PartialEq>::ne
Unexecuted instantiation: <time::duration::OutOfRangeError as core::cmp::PartialEq>::eq
407
pub struct OutOfRangeError(());
408
409
impl fmt::Display for OutOfRangeError {
410
    #[allow(deprecated)]
411
0
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
412
0
        write!(f, "{}", self.description())
413
0
    }
414
}
415
416
impl Error for OutOfRangeError {
417
0
    fn description(&self) -> &str {
418
0
        "Source duration value is out of range for the target type"
419
0
    }
420
}
421
422
// Copied from libnum
423
#[inline]
424
0
fn div_mod_floor_64(this: i64, other: i64) -> (i64, i64) {
425
0
    (div_floor_64(this, other), mod_floor_64(this, other))
426
0
}
427
428
#[inline]
429
0
fn div_floor_64(this: i64, other: i64) -> i64 {
430
0
    match div_rem_64(this, other) {
431
0
        (d, r) if (r > 0 && other < 0)
432
0
               || (r < 0 && other > 0) => d - 1,
433
0
        (d, _)                         => d,
434
    }
435
0
}
436
437
#[inline]
438
0
fn mod_floor_64(this: i64, other: i64) -> i64 {
439
0
    match this % other {
440
0
        r if (r > 0 && other < 0)
441
0
          || (r < 0 && other > 0) => r + other,
442
0
        r                         => r,
443
    }
444
0
}
445
446
#[inline]
447
0
fn div_rem_64(this: i64, other: i64) -> (i64, i64) {
448
0
    (this / other, this % other)
449
0
}
450
451
#[cfg(test)]
452
mod tests {
453
    use super::{Duration, MIN, MAX, OutOfRangeError};
454
    use std::{i32, i64};
455
    use std::time::Duration as StdDuration;
456
457
    #[test]
458
    fn test_duration() {
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));
471
    }
472
473
    #[test]
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);
484
    }
485
486
    #[test]
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);
495
    }
496
497
    #[test]
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);
510
    }
511
512
    #[test]
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);
525
526
        // overflow checks
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);
534
    }
535
536
    #[test]
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);
545
546
        // overflow checks
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);
554
    }
555
556
    #[test]
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))
561
                                                .is_none());
562
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))
566
                                                .is_none());
567
    }
568
569
    #[test]
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));
584
    }
585
586
    #[test]
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));
601
    }
602
603
    #[test]
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(),
613
                   "P7DT6.543S");
614
        assert_eq!(Duration::seconds(-86401).to_string(), "-P1DT1S");
615
        assert_eq!(Duration::nanoseconds(-1).to_string(), "-PT0.000000001S");
616
617
        // the format specifier should have no effect on `Duration`
618
        assert_eq!(format!("{:30}", Duration::days(1) + Duration::milliseconds(2345)),
619
                   "P1DT2.345S");
620
    }
621
622
    #[test]
623
    fn test_to_std() {
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(())));
634
    }
635
636
    #[test]
637
    fn test_from_std() {
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)));
648
        assert_eq!(Ok(MAX),
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(())));
654
    }
655
}