@@ -29,7 +29,10 @@ macro_rules! earlyexit(
29
29
( $e: expr) => ( match $e { Some ( v) => v, None => return None } )
30
30
)
31
31
32
- /// ISO 8601 duration
32
+ /// The representation of a span of time.
33
+ ///
34
+ /// This type has nanosecond precision, and conforms to the ISO 8601
35
+ /// standard for Date interchange.
33
36
#[ deriving( PartialEq , Eq , PartialOrd , Ord ) ]
34
37
pub struct Duration {
35
38
days : i32 ,
@@ -38,6 +41,7 @@ pub struct Duration {
38
41
}
39
42
40
43
impl Duration {
44
+ /// Create a new `Duration`.
41
45
pub fn new ( days : i32 , secs : i32 , nanos : i32 ) -> Option < Duration > {
42
46
let ( secs_, nanos) = div_mod_floor ( nanos, NANOS_PER_SEC ) ;
43
47
let secs = earlyexit ! ( secs. checked_add( & secs_) ) ;
@@ -46,67 +50,78 @@ impl Duration {
46
50
Some ( Duration { days : days, secs : secs as u32 , nanos : nanos as u32 } )
47
51
}
48
52
53
+ /// Create a new `Duration` from an integer number of weeks.
49
54
#[ inline]
50
55
pub fn weeks ( weeks : i32 ) -> Duration {
51
56
Duration :: days ( weeks * 7 )
52
57
}
53
58
59
+ /// Create a new `Duration` from an integer number of days.
54
60
#[ inline]
55
61
pub fn days ( days : i32 ) -> Duration {
56
62
let days = days. to_i32 ( ) . expect ( "Duration::days out of bounds" ) ;
57
63
Duration { days : days, secs : 0 , nanos : 0 }
58
64
}
59
65
66
+ /// Create a new `Duration` from an integer number of hours.
60
67
#[ inline]
61
68
pub fn hours ( hours : i32 ) -> Duration {
62
69
let ( days, hours) = div_mod_floor ( hours, ( SECS_PER_DAY / 3600 ) ) ;
63
70
let secs = hours * 3600 ;
64
71
Duration { secs : secs as u32 , ..Duration :: days ( days) }
65
72
}
66
73
74
+ /// Create a new `Duration` from an integer number of minutes.
67
75
#[ inline]
68
76
pub fn minutes ( mins : i32 ) -> Duration {
69
77
let ( days, mins) = div_mod_floor ( mins, ( SECS_PER_DAY / 60 ) ) ;
70
78
let secs = mins * 60 ;
71
79
Duration { secs : secs as u32 , ..Duration :: days ( days) }
72
80
}
73
81
82
+ /// Create a new `Duration` from an integer number of seconds.
74
83
#[ inline]
75
84
pub fn seconds ( secs : i32 ) -> Duration {
76
85
let ( days, secs) = div_mod_floor ( secs, SECS_PER_DAY ) ;
77
86
Duration { secs : secs as u32 , ..Duration :: days ( days) }
78
87
}
79
88
89
+ /// Create a new `Duration` from an integer number of milliseconds.
80
90
#[ inline]
81
91
pub fn milliseconds ( millis : i32 ) -> Duration {
82
92
let ( secs, millis) = div_mod_floor ( millis, ( NANOS_PER_SEC / 1_000_000 ) ) ;
83
93
let nanos = millis * 1_000_000 ;
84
94
Duration { nanos : nanos as u32 , ..Duration :: seconds ( secs) }
85
95
}
86
96
97
+ /// Create a new `Duration` from an integer number of microseconds.
87
98
#[ inline]
88
99
pub fn microseconds ( micros : i32 ) -> Duration {
89
100
let ( secs, micros) = div_mod_floor ( micros, ( NANOS_PER_SEC / 1_000 ) ) ;
90
101
let nanos = micros * 1_000 ;
91
102
Duration { nanos : nanos as u32 , ..Duration :: seconds ( secs) }
92
103
}
93
104
105
+ /// Create a new `Duration` from an integer number of nanoseconds.
94
106
#[ inline]
95
107
pub fn nanoseconds ( nanos : i32 ) -> Duration {
96
108
let ( secs, nanos) = div_mod_floor ( nanos, NANOS_PER_SEC ) ;
97
109
Duration { nanos : nanos as u32 , ..Duration :: seconds ( secs) }
98
110
}
99
111
112
+ /// Return the number of whole days in the `Duration`.
100
113
#[ inline]
101
114
pub fn ndays ( & self ) -> i32 {
102
115
self . days as i32
103
116
}
104
117
118
+ /// Return the fractional number of days in the `Duration` as seconds.
105
119
#[ inline]
106
120
pub fn nseconds ( & self ) -> u32 {
107
121
self . secs as u32
108
122
}
109
123
124
+ /// Return the fractional number of seconds in the `Duration` as nanoseconds.
110
125
#[ inline]
111
126
pub fn nnanoseconds ( & self ) -> u32 {
112
127
self . nanos as u32
0 commit comments