1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
//! Floating-point duration type `FloatDuration` and helpers.
use std::time;
use std::fmt;
use std::ops;
use std::f64;
use std::u64;
use std::iter::Sum;

#[cfg(feature = "chrono")]
use chrono;
#[cfg(feature = "approx")]
use approx::ApproxEq;

#[cfg(feature = "serde")]
use serde::{Serialize, Deserialize, Serializer, Deserializer};
#[cfg(feature = "serde")]
use serde::de::{self, Visitor};

use super::error;

/// Number of nanoseconds in a second.
pub const NANOS_PER_SEC: f64 = 1.0e9;
/// Number of microseconds in a second.
pub const MICROS_PER_SEC: f64 = 1.0e6;
/// Number of milliseconds in a second.
pub const MILLIS_PER_SEC: f64 = 1.0e3;
/// Number of seconds in a minute.
pub const SECS_PER_MINUTE: f64 = 60.0;
/// Number of seconds in an hour.
pub const SECS_PER_HOUR: f64 = SECS_PER_MINUTE * 60.0;
/// Number of seconds in a day.
pub const SECS_PER_DAY: f64 = SECS_PER_HOUR * 24.0;
/// Number of seconds in a year.
pub const SECS_PER_YEAR: f64 = SECS_PER_DAY * 365.0;

/// A fallible conversion from one duration representation to another.
///
/// This is very similar to the `std::convert::TryFrom` trait which is currently
/// unstable.
pub trait FromDuration<T>: Sized {
    type Error;
    /// Convert `from` into a `Self` object.
    fn from_duration(from: T) -> Result<Self, Self::Error>;
}

/// A fallible conversion that consumes `self`.
///
/// This is very similar to the `std::convert::TryInto` trait which is currently
/// unstable.
///
/// Similar to `std::convert::Into`, this trait is reflexively implemented for
/// all implementations of `FromDuration` and should not be manually implemented.
pub trait IntoDuration<T>: Sized {
    type Error;
    /// Convert `self` into a `T` object.
    fn into_duration(self) -> Result<T, Self::Error>;
}

impl<T, U> IntoDuration<U> for T
    where U: FromDuration<T>
{
    type Error = U::Error;
    fn into_duration(self) -> Result<U, U::Error> {
        U::from_duration(self)
    }
}

/// A specific point in time.
///
/// Types implementing `TimePoint` can have a `FloatDuration` computed between them
/// via `float_duration_since` in either direction.
///
/// #Examples
/// ```rust
/// # fn perform_expensive_task() {}
/// // Don't forget to use TimePoint.
/// use float_duration::{TimePoint, FloatDuration};
/// use std::time::Instant;
///
/// let start = Instant::now();
/// perform_expensive_task();
///
/// // Computing a duration using `Instant` cannot fail, so it is safe to unwrap.
/// let elapsed = Instant::now().float_duration_since(start).unwrap();
///
/// println!("Took {}.", elapsed);
/// ```
pub trait TimePoint<Rhs = Self> {
    /// The type returned if there is an error computing the duration.
    type Error;
    /// The amount of time between two `TimePoint`s.
    fn float_duration_since(self, rhs: Rhs) -> Result<FloatDuration, Self::Error>;
}

/// A time duration stored as a floating point quantity.
///
/// Unlike `std::time::Duration` or `chrono::Duration`, `FloatDuration`
/// aims to be convenient and fast to use in simulation and mathematical expressions
/// rather than to behave like a calendar or perfectly
/// accurately represent precise time scales.
///
/// Internally, a `FloatDuration` stores a single `f64` number of floating-point seconds,
/// thus it is only as precise as the `f64` type.
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
pub struct FloatDuration {
    secs: f64,
}


impl FloatDuration {
    /// Create a new `FloatDuration` representing a number of years.
    ///
    /// `float_duration` considers one year to be exactly 365 days, with
    /// no consideration of leap years.
    #[inline]
    pub fn years(years: f64) -> FloatDuration {
        FloatDuration { secs: years * SECS_PER_YEAR }
    }
    /// Create a new `FloatDuration` representing a number of days.
    #[inline]
    pub fn days(days: f64) -> FloatDuration {
        FloatDuration { secs: days * SECS_PER_DAY }
    }
    /// Create a new `FloatDuration` representing a number of hours.
    #[inline]
    pub fn hours(hours: f64) -> FloatDuration {
        FloatDuration { secs: hours * SECS_PER_HOUR }
    }
    /// Create a new `FloatDuration` representing a number of minutes.
    #[inline]
    pub fn minutes(mins: f64) -> FloatDuration {
        FloatDuration { secs: mins * SECS_PER_MINUTE }
    }
    /// Create a new `FloatDuration` representing a number of seconds.
    #[inline]
    pub fn seconds(secs: f64) -> FloatDuration {
        FloatDuration { secs: secs }
    }
    /// Create a new `FloatDuration` representing a number of milliseconds.
    #[inline]
    pub fn milliseconds(millis: f64) -> FloatDuration {
        FloatDuration { secs: millis / MILLIS_PER_SEC }
    }
    /// Create a new `FloatDuration` representing a number of microseconds.
    #[inline]
    pub fn microseconds(micros: f64) -> FloatDuration {
        FloatDuration { secs: micros / MICROS_PER_SEC }
    }
    /// Create a new `FloatDuration` representing a number of nanoseconds.
    #[inline]
    pub fn nanoseconds(nanos: f64) -> FloatDuration {
        FloatDuration { secs: nanos / NANOS_PER_SEC }
    }

    /// Return the total number of fractional years represented by the `FloatDuration`.
    ///
    /// `float_duration` considers one year to be exactly 365 days, with
    /// no consideration of leap years.
    #[inline]
    pub fn as_years(&self) -> f64 {
        self.secs / SECS_PER_YEAR
    }
    /// Return the total number of fractional days represented by the `FloatDuration`.
    #[inline]
    pub fn as_days(&self) -> f64 {
        self.secs / SECS_PER_DAY
    }
    /// Return the total number of fractional hours represented by the `FloatDuration`.
    #[inline]
    pub fn as_hours(&self) -> f64 {
        self.secs / SECS_PER_HOUR
    }
    /// Return the total number of fractional minutes represented by the `FloatDuration`.
    #[inline]
    pub fn as_minutes(&self) -> f64 {
        self.secs / SECS_PER_MINUTE
    }
    /// Return the total number of fractional seconds represented by the `FloatDuration`.
    #[inline]
    pub fn as_seconds(&self) -> f64 {
        self.secs
    }
    /// Return the total number of fractional milliseconds represented by the `FloatDuration`.
    #[inline]
    pub fn as_milliseconds(&self) -> f64 {
        self.secs * MILLIS_PER_SEC
    }
    /// Return the total number of fractional microseconds represented by the `FloatDuration`.
    #[inline]
    pub fn as_microseconds(&self) -> f64 {
        self.secs * MICROS_PER_SEC
    }
    /// Return the total number of fractional nanoseconds represented by the `FloatDuration`.
    #[inline]
    pub fn as_nanoseconds(&self) -> f64 {
        self.secs * NANOS_PER_SEC
    }

    /// Compute the absolute value of this duration.
    #[inline]
    pub fn abs(self) -> FloatDuration {
        FloatDuration { secs: self.secs.abs() }
    }
    /// Return a new `FloatDuration` that represents zero elapsed time.
    #[inline]
    pub fn zero() -> FloatDuration {
        FloatDuration { secs: 0.0 }
    }
    /// Returns true is this duration represents zero elapsed time (equals `FloatDuration::zero()`).
    #[inline]
    pub fn is_zero(&self) -> bool {
        self.secs == 0.0
    }
    /// Returns true if the FloatDuration holds a positive amount of time.
    #[inline]
    pub fn is_positive(&self) -> bool {
        self.secs.is_sign_positive()
    }
    /// Returns true if the FloatDuration holds a negative amount of time.
    #[inline]
    pub fn is_negative(&self) -> bool {
        self.secs.is_sign_negative()
    }

    /// Return a number that represents the sign of `self`.
    ///
    /// - 1.0 if the value is positive, `+0.0` or `INFINITY`
    /// - -1.0 if the value is negative, `-0.0` or `NEG_INFINITY`
    /// - `NAN` if the value is `NAN`
    #[inline]
    pub fn signum(&self) -> f64 {
        self.secs.signum()
    }

    /// Return the maximum of two durations.
    #[inline]
    pub fn max(self, other: FloatDuration) -> FloatDuration {
        FloatDuration { secs: self.secs.max(other.secs) }
    }
    /// Return the minimum of two durations.
    #[inline]
    pub fn min(self, other: FloatDuration) -> FloatDuration {
        FloatDuration { secs: self.secs.min(other.secs) }
    }

    /// Return a new `FloatDuration` with the minimum possible value.
    #[inline]
    pub fn min_value() -> FloatDuration {
        FloatDuration { secs: f64::MIN }
    }
    /// Return a new `FloatDuration` with the maximum possible value.
    #[inline]
    pub fn max_value() -> FloatDuration {
        FloatDuration { secs: f64::MAX }
    }

    /// Create a `std::time::Duration` object from a `FloatDuration`.
    ///
    /// # Errors
    /// `std::time::Duration` does not support negative values or seconds
    /// greater than `std::u64::MAX`. This function will return a
    /// `DurationError::StdOutOfRange` if the `FloatDuration` value is outside
    /// of either of those bounds.
    pub fn to_std(&self) -> Result<time::Duration, error::OutOfRangeError> {
        if self.secs.is_sign_negative() {
            Err(error::OutOfRangeError::new())
        } else {
            let seconds = self.secs.trunc();
            let nanos = self.secs.fract() * NANOS_PER_SEC;

            if seconds > u64::MAX as f64 {
                Err(error::OutOfRangeError::new())
            } else {
                Ok(time::Duration::new(seconds as u64, nanos as u32))
            }
        }
    }

    /// Create a `FloatDuration` object from a `std::time::Duration`.
    ///
    /// Equivalent to using `FloatDuration::from(duration)`
    #[inline]
    pub fn from_std(duration: time::Duration) -> FloatDuration {
        FloatDuration::seconds((duration.as_secs() as f64) +
                               (duration.subsec_nanos() as f64) / NANOS_PER_SEC)
    }
}

#[cfg(feature = "chrono")]
impl FloatDuration {
    /// Create a `chrono::Duration` object from a `FloatDuration`.
    ///
    /// # Errors
    /// Presently, the conversion to `chrono::Duration` first goes through
    /// `std::time::Duration` and return an error if `to_std` returns an error.
    pub fn to_chrono(&self) -> Result<chrono::Duration, error::OutOfRangeError> {
        let is_negative = self.is_negative();
        let std_duration = self.abs().to_std()?;
        let chrono_duration = chrono::Duration::from_std(std_duration)?;
        if is_negative {
            Ok(-chrono_duration)
        } else {
            Ok(chrono_duration)
        }
    }

    /// Create a `FloatDuration` object from a `chrono::Duration`.
    ///
    /// `chrono::Duration` does not provide a way to access sub-millisecond
    /// precision if the duration is too large to be entirely represented as a single
    /// value. Thus, if the absolute value of the total number of nanoseconds is
    /// greater than `i64::MAX`, only millisecond precision will be captured.
    ///
    /// Equivalent to using `FloatDuration::from(duration)`
    #[inline]
    pub fn from_chrono(duration: chrono::Duration) -> FloatDuration {
        if let Some(nanos) = duration.num_nanoseconds() {
            FloatDuration::nanoseconds(nanos as f64)
        } else {
            FloatDuration::milliseconds(duration.num_milliseconds() as f64)
        }
    }
}

#[cfg(feature = "serde")]
struct FloatDurationVisitor;

// We want to serialize a `FloatDuration` as a single f64 instead of a struct.
#[cfg(feature = "serde")]
impl<'de> Visitor<'de> for FloatDurationVisitor {
    type Value = FloatDuration;

    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        formatter.write_str("a floating-point value")
    }
    fn visit_f32<E>(self, value: f32) -> Result<FloatDuration, E>
        where E: de::Error
    {
        Ok(FloatDuration::seconds(value as f64))
    }
    fn visit_f64<E>(self, value: f64) -> Result<FloatDuration, E>
        where E: de::Error
    {
        Ok(FloatDuration::seconds(value))
    }
}

#[cfg(feature = "serde")]
impl Serialize for FloatDuration {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
        where S: Serializer
    {
        serializer.serialize_f64(self.secs)
    }
}

#[cfg(feature = "serde")]
impl<'de> Deserialize<'de> for FloatDuration {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
        where D: Deserializer<'de>
    {
        deserializer.deserialize_f64(FloatDurationVisitor)
    }
}

#[cfg(feature = "chrono")]
impl<Tz: chrono::TimeZone> TimePoint for chrono::DateTime<Tz> {
    type Error = ();
    #[inline]
    fn float_duration_since(self, since: chrono::DateTime<Tz>) -> Result<FloatDuration, ()> {
        let chrono_duration = self.signed_duration_since(since);
        Ok(FloatDuration::from_chrono(chrono_duration))
    }
}
#[cfg(feature = "chrono")]
impl<Tz: chrono::TimeZone> TimePoint for chrono::Date<Tz> {
    type Error = ();
    #[inline]
    fn float_duration_since(self, since: chrono::Date<Tz>) -> Result<FloatDuration, ()> {
        let chrono_duration = self.signed_duration_since(since);
        Ok(FloatDuration::from_chrono(chrono_duration))
    }
}
#[cfg(feature = "chrono")]
impl TimePoint for chrono::NaiveDate {
    type Error = ();
    #[inline]
    fn float_duration_since(self, since: chrono::NaiveDate) -> Result<FloatDuration, ()> {
        let chrono_duration = self.signed_duration_since(since);
        Ok(FloatDuration::from_chrono(chrono_duration))
    }
}
#[cfg(feature = "chrono")]
impl TimePoint for chrono::NaiveTime {
    type Error = ();
    #[inline]
    fn float_duration_since(self, since: chrono::NaiveTime) -> Result<FloatDuration, ()> {
        let chrono_duration = self.signed_duration_since(since);
        Ok(FloatDuration::from_chrono(chrono_duration))
    }
}
#[cfg(feature = "chrono")]
impl TimePoint for chrono::NaiveDateTime {
    type Error = ();
    #[inline]
    fn float_duration_since(self, since: chrono::NaiveDateTime) -> Result<FloatDuration, ()> {
        let chrono_duration = self.signed_duration_since(since);
        Ok(FloatDuration::from_chrono(chrono_duration))
    }
}

impl TimePoint for time::Instant {
    type Error = ();
    #[inline]
    fn float_duration_since(self, since: time::Instant) -> Result<FloatDuration, ()> {
        let std_duration = self.duration_since(since);
        Ok(FloatDuration::from_std(std_duration))
    }
}
impl TimePoint for time::SystemTime {
    type Error = time::SystemTimeError;
    #[inline]
    fn float_duration_since(self,
                            since: time::SystemTime)
                            -> Result<FloatDuration, time::SystemTimeError> {
        let std_duration = self.duration_since(since)?;
        Ok(FloatDuration::from_std(std_duration))
    }
}

impl FromDuration<time::Duration> for FloatDuration {
    type Error = ();
    #[inline]
    fn from_duration(from: time::Duration) -> Result<FloatDuration, ()> {
        Ok(FloatDuration::from_std(from))
    }
}
#[cfg(feature = "chrono")]
impl FromDuration<chrono::Duration> for FloatDuration {
    type Error = ();
    #[inline]
    fn from_duration(from: chrono::Duration) -> Result<FloatDuration, ()> {
        Ok(FloatDuration::from_chrono(from))
    }
}
impl FromDuration<FloatDuration> for time::Duration {
    type Error = error::OutOfRangeError;
    #[inline]
    fn from_duration(from: FloatDuration) -> Result<time::Duration, error::OutOfRangeError> {
        from.to_std()
    }
}
#[cfg(feature = "chrono")]
impl FromDuration<FloatDuration> for chrono::Duration {
    type Error = error::OutOfRangeError;
    #[inline]
    fn from_duration(from: FloatDuration) -> Result<chrono::Duration, error::OutOfRangeError> {
        from.to_chrono()
    }
}

impl From<time::Duration> for FloatDuration {
    #[inline]
    fn from(from: time::Duration) -> FloatDuration {
        FloatDuration::from_std(from)
    }
}
#[cfg(feature = "chrono")]
impl From<chrono::Duration> for FloatDuration {
    #[inline]
    fn from(from: chrono::Duration) -> FloatDuration {
        FloatDuration::from_chrono(from)
    }
}

impl fmt::Display for FloatDuration {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        if self.secs > SECS_PER_YEAR {
            write!(fmt, "{} years", self.as_years())
        } else if self.secs > SECS_PER_DAY {
            write!(fmt, "{} days", self.as_days())
        } else if self.secs > SECS_PER_HOUR {
            write!(fmt, "{} hours", self.as_hours())
        } else if self.secs > SECS_PER_MINUTE {
            write!(fmt, "{} minutes", self.as_minutes())
        } else if self.secs > 1.0 {
            write!(fmt, "{} seconds", self.as_seconds())
        } else if self.secs > 1.0e-3 {
            write!(fmt, "{} milliseconds", self.as_milliseconds())
        } else if self.secs > 1.0e-6 {
            write!(fmt, "{} microseconds", self.as_microseconds())
        } else if self.secs > 1.0e-9 {
            write!(fmt, "{} nanoseconds", self.as_nanoseconds())
        } else if self.is_zero() {
            write!(fmt, "0 seconds")
        } else {
            // Here we simply print seconds in scientific notation.
            write!(fmt, "{:e} seconds", self.as_seconds())
        }
    }
}

impl ops::Neg for FloatDuration {
    type Output = FloatDuration;

    #[inline]
    fn neg(self) -> FloatDuration {
        FloatDuration { secs: -self.secs }
    }
}

impl ops::Add<FloatDuration> for FloatDuration {
    type Output = FloatDuration;

    #[inline]
    fn add(self, rhs: FloatDuration) -> FloatDuration {
        FloatDuration { secs: self.secs + rhs.secs }
    }
}
impl ops::Sub<FloatDuration> for FloatDuration {
    type Output = FloatDuration;

    #[inline]
    fn sub(self, rhs: FloatDuration) -> FloatDuration {
        FloatDuration { secs: self.secs - rhs.secs }
    }
}

impl ops::Mul<f64> for FloatDuration {
    type Output = FloatDuration;

    #[inline]
    fn mul(self, rhs: f64) -> FloatDuration {
        FloatDuration { secs: self.secs * rhs }
    }
}
impl ops::Mul<FloatDuration> for f64 {
    type Output = FloatDuration;

    #[inline]
    fn mul(self, rhs: FloatDuration) -> FloatDuration {
        FloatDuration { secs: self * rhs.secs }
    }
}
impl ops::Div<f64> for FloatDuration {
    type Output = FloatDuration;

    #[inline]
    fn div(self, rhs: f64) -> FloatDuration {
        FloatDuration { secs: self.secs / rhs }
    }
}
impl ops::Div<FloatDuration> for FloatDuration {
    type Output = f64;

    #[inline]
    fn div(self, rhs: FloatDuration) -> f64 {
        self.secs / rhs.secs
    }
}

impl ops::AddAssign<FloatDuration> for FloatDuration {
    #[inline]
    fn add_assign(&mut self, rhs: FloatDuration) {
        self.secs += rhs.secs;
    }
}
impl ops::SubAssign<FloatDuration> for FloatDuration {
    #[inline]
    fn sub_assign(&mut self, rhs: FloatDuration) {
        self.secs -= rhs.secs;
    }
}

impl ops::MulAssign<f64> for FloatDuration {
    #[inline]
    fn mul_assign(&mut self, rhs: f64) {
        self.secs *= rhs;
    }
}
impl ops::DivAssign<f64> for FloatDuration {
    #[inline]
    fn div_assign(&mut self, rhs: f64) {
        self.secs /= rhs;
    }
}
impl Default for FloatDuration {
    #[inline]
    fn default() -> FloatDuration {
        FloatDuration::zero()
    }
}
impl Sum for FloatDuration {
    fn sum<I>(iter: I) -> FloatDuration
        where I: Iterator<Item = FloatDuration>
    {
        iter.fold(FloatDuration::zero(), |a, b| a + b)
    }
}
impl<'a> Sum<&'a FloatDuration> for FloatDuration {
    fn sum<I>(iter: I) -> FloatDuration
        where I: Iterator<Item = &'a FloatDuration>
    {
        iter.fold(FloatDuration::zero(), |a, &b| a + b)
    }
}

#[cfg(feature = "approx")]
impl ApproxEq for FloatDuration {
    type Epsilon = f64;

    #[inline]
    fn default_epsilon() -> f64 {
        f64::default_epsilon()
    }
    #[inline]
    fn default_max_relative() -> f64 {
        f64::default_max_relative()
    }
    #[inline]
    fn default_max_ulps() -> u32 {
        f64::default_max_ulps()
    }
    #[inline]
    fn relative_eq(&self, other: &FloatDuration, epsilon: f64, max_relative: f64) -> bool {
        self.secs.relative_eq(&other.secs, epsilon, max_relative)
    }
    #[inline]
    fn ulps_eq(&self, other: &FloatDuration, epsilon: f64, max_ulps: u32) -> bool {
        self.secs.ulps_eq(&other.secs, epsilon, max_ulps)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::time;

    #[test]
    fn test_construct() {
        let duration1 = FloatDuration::hours(3.0);
        assert_eq!(duration1.as_hours(), 3.0);
        assert_eq!(duration1.as_minutes(), 180.0);
        assert_eq!(duration1.as_seconds(), 180.0 * 60.0);
        assert_eq!(duration1.as_days(), 3.0 / 24.0);
        assert_eq!(duration1.as_milliseconds(), 180.0 * 60.0 * 1000.0);
        assert!(duration1.is_positive());
        assert_eq!(duration1.signum(), 1.0);

        let duration2 = FloatDuration::milliseconds(55.0);
        assert_eq!(duration2.as_seconds(), 0.055);
        assert_eq!(duration2.as_milliseconds(), 55.0);
        assert_eq!(duration2.as_microseconds(), 55000.0);
        assert_eq!(duration2.as_nanoseconds(), 55000000.0);
        assert!(!duration2.is_zero());

        let duration3 = FloatDuration::zero();
        assert!(duration3.is_zero());
        assert_eq!(duration3.as_minutes(), 0.0);
        assert_eq!(duration3.as_nanoseconds(), 0.0);

        assert_eq!(FloatDuration::days(1.5), FloatDuration::hours(36.0));
        assert_eq!(FloatDuration::minutes(30.0), FloatDuration::hours(0.5));
        assert_eq!(FloatDuration::seconds(180.0), FloatDuration::minutes(3.0));
        assert_eq!(FloatDuration::seconds(3.5),
                   FloatDuration::milliseconds(3500.0));
        assert_eq!(FloatDuration::microseconds(300.0),
                   FloatDuration::milliseconds(0.30));
        assert_eq!(FloatDuration::nanoseconds(1000.0),
                   FloatDuration::microseconds(1.0));

        let duration4 = FloatDuration::minutes(-3.0);
        assert_eq!(duration4.as_minutes(), -3.0);
        assert_eq!(duration4.as_hours(), -0.05);
        assert!(duration4.is_negative());
        assert_eq!(duration4.signum(), -1.0);

        assert_eq!(FloatDuration::years(2.0), FloatDuration::days(365.0 * 2.0));
    }

    #[test]
    fn test_arithmetic() {
        assert_eq!(FloatDuration::minutes(5.0) + FloatDuration::seconds(30.0),
                   FloatDuration::seconds(330.0));

        assert_eq!(FloatDuration::hours(3.0) * 2.5, FloatDuration::hours(7.5));

        assert_eq!(FloatDuration::days(3.0) / 3.0 - FloatDuration::hours(2.0),
                   FloatDuration::hours(22.0));

        assert_eq!(FloatDuration::zero() + FloatDuration::milliseconds(500.0) +
                   FloatDuration::microseconds(500.0),
                   FloatDuration::microseconds(500500.0));

        assert_eq!(2.0 * FloatDuration::milliseconds(150.0),
                   FloatDuration::milliseconds(300.0));

        assert_eq!(FloatDuration::minutes(10.0) / FloatDuration::seconds(60.0),
                   10.0);
        assert_eq!(FloatDuration::minutes(5.0),
                   (-FloatDuration::minutes(5.0)) * -1.0);

        assert_eq!(FloatDuration::seconds(10.0) - FloatDuration::minutes(1.0),
                   FloatDuration::seconds(-50.0));

        let inf = FloatDuration::seconds(10.0) / 0.0;

        assert!(inf.as_seconds().is_infinite());
        assert!(inf.as_years().is_infinite());
        assert!(inf.as_microseconds().is_infinite());
        assert!(FloatDuration::hours(10.0) / FloatDuration::minutes(0.0) == f64::INFINITY);

        let mut d1 = FloatDuration::seconds(5.0);
        d1 += FloatDuration::minutes(10.0);
        d1 *= 2.0;
        assert_eq!(d1,
                   FloatDuration::minutes(20.0) + FloatDuration::seconds(10.0));
        d1 /= 2.0;
        assert_eq!(d1,
                   FloatDuration::minutes(10.0) + FloatDuration::seconds(5.0));
        d1 -= d1;
        assert_eq!(d1, FloatDuration::zero());
    }

    #[test]
    fn test_min_max() {
        assert_eq!(FloatDuration::minutes(5.0).max(FloatDuration::minutes(10.0)),
                   FloatDuration::minutes(10.0));
        assert_eq!(FloatDuration::minutes(5.0).min(FloatDuration::minutes(10.0)),
                   FloatDuration::minutes(5.0));
        assert_eq!(FloatDuration::zero().max(FloatDuration::seconds(1.0)),
                   FloatDuration::seconds(1.0));
        assert_eq!(FloatDuration::zero().min(FloatDuration::seconds(1.0)),
                   FloatDuration::zero());
    }

    #[test]
    fn test_std_conversion() {
        let duration1 = FloatDuration::minutes(5.0);
        let std_duration1 = duration1.to_std().unwrap();
        assert!(duration1.is_positive());
        assert_eq!(duration1.signum(), 1.0);
        assert_eq!(std_duration1, time::Duration::new(300, 0));
        assert_eq!(FloatDuration::from_std(std_duration1), duration1);

        let duration2 = FloatDuration::hours(-2.0);
        assert!(duration2.is_negative());
        assert_eq!(duration2.signum(), -1.0);
        assert!(!duration2.to_std().is_ok());
        let std_duration2 = (-duration2).to_std().unwrap();
        assert_eq!(std_duration2, time::Duration::new(3600 * 2, 0));
        assert_eq!(FloatDuration::from(std_duration2), -duration2);
        assert_eq!(FloatDuration::from_std(std_duration2), -duration2);

        assert_eq!(FloatDuration::zero().to_std().unwrap(),
                   time::Duration::new(0, 0));
        assert_eq!(FloatDuration::zero().signum(), 1.0);
        assert!(FloatDuration::zero().is_positive());
        assert!(FloatDuration::nanoseconds(-1.0).to_std().is_err());
        assert!(FloatDuration::max_value().to_std().is_err());

        assert_eq!(FloatDuration::from_std(time::Duration::new(0, 1)),
                   FloatDuration::nanoseconds(1.0));
        assert_eq!(FloatDuration::from(time::Duration::new(0, 1)),
                   FloatDuration::nanoseconds(1.0));
        assert_eq!(FloatDuration::from_std(time::Duration::new(1, 1)),
                   FloatDuration::seconds(1.0) + FloatDuration::nanoseconds(1.0));
    }

    #[test]
    fn test_time_point_std() {
        use std::time::{Instant, SystemTime, UNIX_EPOCH};

        let i = Instant::now();
        let s = SystemTime::now();
        assert_eq!(i.float_duration_since(i), Ok(FloatDuration::zero()));
        assert_eq!(s.float_duration_since(s).unwrap(), FloatDuration::zero());
        assert!(Instant::now().float_duration_since(i).unwrap() > FloatDuration::zero());
        assert!(SystemTime::now().float_duration_since(s).unwrap() > FloatDuration::zero());

        assert!(SystemTime::now().float_duration_since(UNIX_EPOCH).unwrap() >
                FloatDuration::years(30.0));
    }

    #[test]
    fn test_display() {
        assert_eq!(format!("{}", FloatDuration::minutes(3.5)), "3.5 minutes");
        assert_eq!(format!("{}", FloatDuration::days(3.0) + FloatDuration::hours(12.0)),
                   "3.5 days");
        assert_eq!(format!("{}", FloatDuration::seconds(12.7)), "12.7 seconds");
        assert_eq!(format!("{}", FloatDuration::default()), "0 seconds");

        assert_eq!(format!("{}", FloatDuration::microseconds(100.0)),
                   "100 microseconds");
        assert_eq!(format!("{}", FloatDuration::milliseconds(12.5)),
                   "12.5 milliseconds");

        assert_eq!(format!("{}", FloatDuration::days(325.0) + FloatDuration::hours(6.0)),
                   "325.25 days");
        assert_eq!(format!("{}",
                           FloatDuration::milliseconds(50.0) + FloatDuration::microseconds(500.0)),
                   "50.5 milliseconds");

        assert_eq!(format!("{}", FloatDuration::nanoseconds(25.25)),
                   "25.25 nanoseconds");
        assert_eq!(format!("{}", FloatDuration::minutes(90.0)), "1.5 hours");
        assert_eq!(format!("{}", FloatDuration::years(2.5)), "2.5 years");
        assert_eq!(format!("{}", FloatDuration::seconds(1.5e-30)),
                   "1.5e-30 seconds");
    }

    #[test]
    fn test_sum() {
        let zero: [FloatDuration; 0] = [];

        assert_eq!(zero.iter().sum::<FloatDuration>(), FloatDuration::zero());

        assert_eq!([FloatDuration::milliseconds(50.0),
                    FloatDuration::milliseconds(30.0),
                    FloatDuration::zero()]
                           .iter()
                           .sum::<FloatDuration>(),
                   FloatDuration::milliseconds(80.0));
        assert_eq!([FloatDuration::days(2.0)].iter().sum::<FloatDuration>(),
                   FloatDuration::days(2.0));
    }

    #[cfg(feature = "chrono")]
    #[test]
    fn test_chrono_conversion() {
        assert_eq!(FloatDuration::from(chrono::Duration::minutes(10)),
                   FloatDuration::minutes(10.0));
        assert_eq!(FloatDuration::from_chrono(chrono::Duration::hours(72)),
                   FloatDuration::days(3.0));
        assert_eq!(FloatDuration::from_chrono(chrono::Duration::nanoseconds(500)),
                   FloatDuration::nanoseconds(500.0));
        assert_eq!(FloatDuration::from_chrono(chrono::Duration::microseconds(-20000)),
                   FloatDuration::milliseconds(-20.0));
        assert_eq!(FloatDuration::from_chrono(chrono::Duration::zero()),
                   FloatDuration::zero());
        assert_eq!(FloatDuration::from(chrono::Duration::hours(10000)),
                   FloatDuration::hours(10000.0));
        assert_eq!(FloatDuration::from_chrono(chrono::Duration::milliseconds(1i64 << 62)),
                   FloatDuration::milliseconds((1i64 << 62) as f64));

        assert_eq!(FloatDuration::minutes(2.5).to_chrono().unwrap(),
                   chrono::Duration::seconds(150));
        assert_eq!(FloatDuration::milliseconds(250.050).to_chrono().unwrap(),
                   chrono::Duration::microseconds(250050));
        assert!(FloatDuration::max_value().to_chrono().is_err());
        assert_eq!(FloatDuration::nanoseconds(-20.0).to_chrono().unwrap(),
                   chrono::Duration::nanoseconds(-20));

    }

    #[cfg(feature = "chrono")]
    #[test]
    fn test_chrono_timepoint() {
        use chrono::{TimeZone, Utc, Local};

        let date1 = Utc.ymd(2017, 5, 25).and_hms(10, 0, 0);
        let date2 = Utc.ymd(2017, 5, 26).and_hms(12, 0, 0);

        assert_eq!(date2.float_duration_since(date1).unwrap(),
                   FloatDuration::days(1.0) + FloatDuration::hours(2.0));

        let date3 = Local::now();
        assert_eq!(date3.float_duration_since(date3).unwrap(),
                   FloatDuration::zero());
        assert!(date3.float_duration_since(Local::now()).unwrap() < FloatDuration::zero());
    }

    #[cfg(feature = "serde")]
    #[test]
    fn test_serde() {
        use serde_test::{Token, assert_tokens, assert_de_tokens};

        let duration = FloatDuration::seconds(1.5);
        assert_tokens(&duration, &[Token::F64(duration.as_seconds())]);

        assert_tokens(&FloatDuration::zero(), &[Token::F64(0.0)]);

        let duration2 = FloatDuration::hours(3.0);
        assert_tokens(&duration2, &[Token::F64(3.0 * SECS_PER_HOUR)]);

        let duration3 = FloatDuration::days(5.0) + FloatDuration::minutes(35.2) +
                        FloatDuration::milliseconds(100.0);
        assert_tokens(&duration3, &[Token::F64(duration3.as_seconds())]);

        assert_tokens(&FloatDuration::seconds(-10.0), &[Token::F64(-10.0)]);

        assert_de_tokens(&FloatDuration::seconds(30.0), &[Token::F32(30.0)]);
    }

    #[cfg(feature = "serde")]
    #[should_panic]
    #[test]
    fn test_serde_integer_panic() {
        use serde_test::{Token, assert_de_tokens};
        assert_de_tokens(&FloatDuration::seconds(10.0), &[Token::I32(10)]);
    }
}