Skip to content

Commit 3e29ffe

Browse files
committed
Add PartialDuration record functionality
1 parent 979ee69 commit 3e29ffe

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

src/components/duration.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,31 @@ pub use date::DateDuration;
2626
#[doc(inline)]
2727
pub use time::TimeDuration;
2828

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+
2954
/// The native Rust implementation of `Temporal.Duration`.
3055
///
3156
/// `Duration` is made up of a `DateDuration` and `TimeDuration` as primarily
@@ -173,6 +198,26 @@ impl Duration {
173198
}
174199
}
175200

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+
176221
/// Return if the Durations values are within their valid ranges.
177222
#[inline]
178223
#[must_use]

src/components/duration/tests.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,3 +658,17 @@ fn basic_subtract_duration() {
658658
assert_eq!(result.days(), 6.0);
659659
assert_eq!(result.minutes(), 30.0);
660660
}
661+
662+
#[test]
663+
fn partial_duration_empty() {
664+
let err = Duration::from_partial_duration(PartialDuration::default());
665+
assert!(err.is_err())
666+
}
667+
668+
#[test]
669+
fn partial_duration_values() {
670+
let mut partial = PartialDuration::default();
671+
let _ = partial.years.insert(FiniteF64(20.0));
672+
let result = Duration::from_partial_duration(partial).unwrap();
673+
assert_eq!(result.years(), 20.0);
674+
}

0 commit comments

Comments
 (0)