@@ -26,6 +26,31 @@ pub use date::DateDuration;
26
26
#[ doc( inline) ]
27
27
pub use time:: TimeDuration ;
28
28
29
+ /// A `PartialDuration` is a Duration that may have fields not set.
30
+ #[ derive( Debug , Default , Clone , Copy , PartialEq , PartialOrd ) ]
31
+ pub struct PartialDuration {
32
+ /// A potentially existent `years` field.
33
+ pub years : Option < FiniteF64 > ,
34
+ /// A potentially existent `months` field.
35
+ pub months : Option < FiniteF64 > ,
36
+ /// A potentially existent `weeks` field.
37
+ pub weeks : Option < FiniteF64 > ,
38
+ /// A potentially existent `days` field.
39
+ pub days : Option < FiniteF64 > ,
40
+ /// A potentially existent `hours` field.
41
+ pub hours : Option < FiniteF64 > ,
42
+ /// A potentially existent `minutes` field.
43
+ pub minutes : Option < FiniteF64 > ,
44
+ /// A potentially existent `seconds` field.
45
+ pub seconds : Option < FiniteF64 > ,
46
+ /// A potentially existent `milliseconds` field.
47
+ pub milliseconds : Option < FiniteF64 > ,
48
+ /// A potentially existent `microseconds` field.
49
+ pub microseconds : Option < FiniteF64 > ,
50
+ /// A potentially existent `nanoseconds` field.
51
+ pub nanoseconds : Option < FiniteF64 > ,
52
+ }
53
+
29
54
/// The native Rust implementation of `Temporal.Duration`.
30
55
///
31
56
/// `Duration` is made up of a `DateDuration` and `TimeDuration` as primarily
@@ -173,6 +198,26 @@ impl Duration {
173
198
}
174
199
}
175
200
201
+ /// Creates a `Duration` from a provided `PartialDuration`.
202
+ pub fn from_partial_duration ( partial : PartialDuration ) -> TemporalResult < Self > {
203
+ if partial == PartialDuration :: default ( ) {
204
+ return Err ( TemporalError :: r#type ( )
205
+ . with_message ( "PartialDuration cannot have all empty fields." ) ) ;
206
+ }
207
+ Self :: new (
208
+ partial. years . unwrap_or_default ( ) ,
209
+ partial. months . unwrap_or_default ( ) ,
210
+ partial. weeks . unwrap_or_default ( ) ,
211
+ partial. days . unwrap_or_default ( ) ,
212
+ partial. hours . unwrap_or_default ( ) ,
213
+ partial. minutes . unwrap_or_default ( ) ,
214
+ partial. seconds . unwrap_or_default ( ) ,
215
+ partial. milliseconds . unwrap_or_default ( ) ,
216
+ partial. microseconds . unwrap_or_default ( ) ,
217
+ partial. nanoseconds . unwrap_or_default ( ) ,
218
+ )
219
+ }
220
+
176
221
/// Return if the Durations values are within their valid ranges.
177
222
#[ inline]
178
223
#[ must_use]
0 commit comments