Skip to content

Commit 7484b25

Browse files
authored
Build out stubs for remaining unimplemented methods (#202)
Unimplemented methods not being stubbed came up today in the ICU4X call. This implements stubs for the remaining unimplemented methods that I'm currently aware of nobody working on (in order to minimize any conflicts with ongoing work). This PR also takes a stab at adding the `temporal_capi` methods. CC: @Manishearth
1 parent d45999b commit 7484b25

File tree

12 files changed

+201
-26
lines changed

12 files changed

+201
-26
lines changed

src/builtins/compiled/duration.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{
22
builtins::TZ_PROVIDER,
3-
options::{RelativeTo, RoundingOptions},
3+
options::{RelativeTo, RoundingOptions, TemporalUnit},
44
Duration, TemporalError, TemporalResult,
55
};
66

@@ -39,4 +39,15 @@ impl Duration {
3939
.map_err(|_| TemporalError::general("Unable to acquire lock"))?;
4040
self.compare_with_provider(two, relative_to, &*provider)
4141
}
42+
43+
pub fn total(
44+
&self,
45+
unit: TemporalUnit,
46+
relative_to: Option<RelativeTo>,
47+
) -> TemporalResult<i64> {
48+
let provider = TZ_PROVIDER
49+
.lock()
50+
.map_err(|_| TemporalError::general("Unable to acquire lock"))?;
51+
self.total_with_provider(unit, relative_to, &*provider)
52+
}
4253
}

src/builtins/compiled/zoneddatetime.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,14 @@ impl ZonedDateTime {
268268
self.in_leap_year_with_provider(&*provider)
269269
}
270270

271+
// TODO: Update direction to correct option
272+
pub fn get_time_zone_transition(&self, direction: bool) -> TemporalResult<Self> {
273+
let provider = TZ_PROVIDER
274+
.lock()
275+
.map_err(|_| TemporalError::general("Unable to acquire lock"))?;
276+
self.get_time_zone_transition_with_provider(direction, &*provider)
277+
}
278+
271279
/// Returns the hours in the day.
272280
///
273281
/// Enable with the `compiled_data` feature flag.

src/builtins/core/datetime.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,10 @@ impl PlainDateTime {
626626
Ok(Self::new_unchecked(result, self.calendar.clone()))
627627
}
628628

629+
pub fn to_plain_time(&self) -> TemporalResult<PlainTime> {
630+
Err(TemporalError::general("Not yet implemented."))
631+
}
632+
629633
pub fn to_ixdtf_string(
630634
&self,
631635
options: ToStringRoundingOptions,

src/builtins/core/duration.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,17 @@ impl Duration {
692692
}
693693
}
694694

695+
/// Returns the total of the `Duration`
696+
pub fn total_with_provider(
697+
&self,
698+
_unit: TemporalUnit,
699+
_relative_to: Option<RelativeTo>,
700+
_provider: &impl TimeZoneProvider,
701+
) -> TemporalResult<i64> {
702+
Err(TemporalError::general("Not yet implemented"))
703+
}
704+
705+
/// Returns the `Duration` as a formatted string
695706
pub fn as_temporal_string(&self, options: ToStringRoundingOptions) -> TemporalResult<String> {
696707
if options.smallest_unit == Some(TemporalUnit::Hour)
697708
|| options.smallest_unit == Some(TemporalUnit::Minute)

src/builtins/core/instant.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use num_traits::Euclid;
2525

2626
use super::{
2727
duration::normalized::{NormalizedDurationRecord, NormalizedTimeDuration},
28-
DateDuration,
28+
DateDuration, ZonedDateTime,
2929
};
3030

3131
const NANOSECONDS_PER_SECOND: i64 = 1_000_000_000;
@@ -234,6 +234,11 @@ impl Instant {
234234
pub fn epoch_nanoseconds(&self) -> i128 {
235235
self.as_i128()
236236
}
237+
238+
// TODO: May end up needing a provider API during impl
239+
pub fn to_zoned_date_time_iso(&self, _time_zone: TimeZone) -> TemporalResult<ZonedDateTime> {
240+
Err(TemporalError::general("Not yet implemented"))
241+
}
237242
}
238243

239244
// ==== Instant Provider API ====

src/builtins/core/month_day.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ use crate::{
1212
Calendar, TemporalError, TemporalResult, TemporalUnwrap,
1313
};
1414

15+
use super::{PartialDate, PlainDate};
16+
1517
/// The native Rust implementation of `Temporal.PlainMonthDay`
1618
#[non_exhaustive]
1719
#[derive(Debug, Default, Clone, PartialEq, Eq)]
@@ -49,6 +51,14 @@ impl PlainMonthDay {
4951
Ok(Self::new_unchecked(iso, calendar))
5052
}
5153

54+
pub fn with(
55+
&self,
56+
_partial: PartialDate,
57+
_overflow: ArithmeticOverflow,
58+
) -> TemporalResult<Self> {
59+
Err(TemporalError::general("Not yet implemented."))
60+
}
61+
5262
/// Returns the iso day value of `MonthDay`.
5363
#[inline]
5464
#[must_use]
@@ -90,6 +100,10 @@ impl PlainMonthDay {
90100
self.calendar.month_code(&self.iso)
91101
}
92102

103+
pub fn to_plain_date(&self) -> TemporalResult<PlainDate> {
104+
Err(TemporalError::general("Not yet implemented"))
105+
}
106+
93107
pub fn to_ixdtf_string(&self, display_calendar: DisplayCalendar) -> String {
94108
let ixdtf = FormattableMonthDay {
95109
date: FormattableDate(self.iso_year(), self.iso_month(), self.iso.day),

src/builtins/core/year_month.rs

Lines changed: 65 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ use tinystr::TinyAsciiStr;
77

88
use crate::{
99
iso::IsoDate,
10-
options::{ArithmeticOverflow, DisplayCalendar},
10+
options::{ArithmeticOverflow, DifferenceOperation, DifferenceSettings, DisplayCalendar},
1111
parsers::{FormattableCalendar, FormattableDate, FormattableYearMonth},
1212
utils::pad_iso_year,
1313
Calendar, TemporalError, TemporalResult, TemporalUnwrap,
1414
};
1515

16-
use super::{Duration, PartialDate};
16+
use super::{Duration, PartialDate, PlainDate};
1717

1818
/// The native Rust implementation of `Temporal.YearMonth`.
1919
#[non_exhaustive]
@@ -37,6 +37,40 @@ impl PlainYearMonth {
3737
Self { iso, calendar }
3838
}
3939

40+
/// Internal addition method for adding `Duration` to a `PlainYearMonth`
41+
pub(crate) fn add_or_subtract_duration(
42+
&self,
43+
duration: &Duration,
44+
overflow: ArithmeticOverflow,
45+
) -> TemporalResult<Self> {
46+
// Potential TODO: update to current Temporal specification
47+
let partial = PartialDate::try_from_year_month(self)?;
48+
49+
let mut intermediate_date = self.calendar().date_from_partial(&partial, overflow)?;
50+
51+
intermediate_date = intermediate_date.add_date(duration, Some(overflow))?;
52+
53+
let result_fields = PartialDate::default().with_fallback_date(&intermediate_date)?;
54+
55+
self.calendar()
56+
.year_month_from_partial(&result_fields, overflow)
57+
}
58+
59+
/// The internal difference operation of `PlainYearMonth`.
60+
pub(crate) fn diff(
61+
&self,
62+
_op: DifferenceOperation,
63+
_other: &Self,
64+
_settings: DifferenceSettings,
65+
) -> TemporalResult<Duration> {
66+
// TODO: implement
67+
Err(TemporalError::general("Not yet implemented"))
68+
}
69+
}
70+
71+
// ==== Public method implementations ====
72+
73+
impl PlainYearMonth {
4074
/// Creates a new valid `YearMonth`.
4175
#[inline]
4276
pub fn new_with_overflow(
@@ -137,6 +171,15 @@ impl PlainYearMonth {
137171
self.calendar.identifier()
138172
}
139173

174+
/// Creates a `PlainYearMonth` using the fields provided from a [`PartialDate`]
175+
pub fn with(
176+
&self,
177+
_partial: PartialDate,
178+
_overflow: ArithmeticOverflow,
179+
) -> TemporalResult<Self> {
180+
Err(TemporalError::general("Not yet implemented."))
181+
}
182+
140183
/// Compares one `PlainYearMonth` to another `PlainYearMonth` using their
141184
/// `IsoDate` representation.
142185
///
@@ -151,39 +194,40 @@ impl PlainYearMonth {
151194
self.iso.cmp(&other.iso)
152195
}
153196

154-
pub fn add_duration(
155-
&self,
156-
duration: &Duration,
157-
overflow: ArithmeticOverflow,
158-
) -> TemporalResult<Self> {
197+
/// Adds a [`Duration`] from the current `PlainYearMonth`.
198+
#[inline]
199+
pub fn add(&self, duration: &Duration, overflow: ArithmeticOverflow) -> TemporalResult<Self> {
159200
self.add_or_subtract_duration(duration, overflow)
160201
}
161202

162-
pub fn subtract_duration(
203+
/// Subtracts a [`Duration`] from the current `PlainYearMonth`.
204+
#[inline]
205+
pub fn subtract(
163206
&self,
164207
duration: &Duration,
165208
overflow: ArithmeticOverflow,
166209
) -> TemporalResult<Self> {
167210
self.add_or_subtract_duration(&duration.negated(), overflow)
168211
}
169212

170-
pub(crate) fn add_or_subtract_duration(
171-
&self,
172-
duration: &Duration,
173-
overflow: ArithmeticOverflow,
174-
) -> TemporalResult<Self> {
175-
let partial = PartialDate::try_from_year_month(self)?;
176-
177-
let mut intermediate_date = self.calendar().date_from_partial(&partial, overflow)?;
178-
179-
intermediate_date = intermediate_date.add_date(duration, Some(overflow))?;
213+
/// Returns a `Duration` representing the period of time from this `PlainYearMonth` until the other `PlainYearMonth`.
214+
#[inline]
215+
pub fn until(&self, other: &Self, settings: DifferenceSettings) -> TemporalResult<Duration> {
216+
self.diff(DifferenceOperation::Until, other, settings)
217+
}
180218

181-
let result_fields = PartialDate::default().with_fallback_date(&intermediate_date)?;
219+
/// Returns a `Duration` representing the period of time from this `PlainYearMonth` since the other `PlainYearMonth`.
220+
#[inline]
221+
pub fn since(&self, other: &Self, settings: DifferenceSettings) -> TemporalResult<Duration> {
222+
self.diff(DifferenceOperation::Since, other, settings)
223+
}
182224

183-
self.calendar()
184-
.year_month_from_partial(&result_fields, overflow)
225+
pub fn to_plain_date(&self) -> TemporalResult<PlainDate> {
226+
Err(TemporalError::general("Not yet iimplemented."))
185227
}
186228

229+
/// Returns a RFC9557 IXDTF string for the current `PlainYearMonth`
230+
#[inline]
187231
pub fn to_ixdtf_string(&self, display_calendar: DisplayCalendar) -> String {
188232
let ixdtf = FormattableYearMonth {
189233
date: FormattableDate(self.iso_year(), self.iso_month(), self.iso.day),

src/builtins/core/zoneddatetime.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,10 @@ impl ZonedDateTime {
408408
self.instant
409409
}
410410

411+
pub fn with(&self, _partial: PartialZonedDateTime) -> TemporalResult<Self> {
412+
Err(TemporalError::general("Not yet implemented"))
413+
}
414+
411415
/// Creates a new `ZonedDateTime` from the current `ZonedDateTime`
412416
/// combined with the provided `TimeZone`.
413417
pub fn with_timezone(&self, timezone: TimeZone) -> TemporalResult<Self> {
@@ -438,6 +442,15 @@ impl ZonedDateTime {
438442
// ==== HoursInDay accessor method implementation ====
439443

440444
impl ZonedDateTime {
445+
// TODO: Add direction parameter to either zoneddatetime.rs or option.rs
446+
pub fn get_time_zone_transition_with_provider(
447+
&self,
448+
_direction: bool,
449+
_provider: &impl TimeZoneProvider,
450+
) -> TemporalResult<Self> {
451+
Err(TemporalError::general("Not yet implemented"))
452+
}
453+
441454
pub fn hours_in_day_with_provider(
442455
&self,
443456
provider: &impl TimeZoneProvider,

temporal_capi/src/duration.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ pub mod ffi {
228228
}
229229

230230
// TODO round_with_provider (needs time zone stuff)
231+
// TODO total_with_provider (needs time zone stuff)
231232
}
232233
}
233234

temporal_capi/src/plain_date_time.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,13 @@ pub mod ffi {
251251
.map_err(Into::into)
252252
}
253253

254+
pub fn to_plain_time(&self) -> Result<Box<PlainTime>, TemporalError> {
255+
self.0
256+
.to_plain_time()
257+
.map(|x| Box::new(PlainTime(x)))
258+
.map_err(Into::into)
259+
}
260+
254261
pub fn to_ixdtf_string(
255262
&self,
256263
options: ToStringRoundingOptions,

0 commit comments

Comments
 (0)