Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions src/builtins/core/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,6 @@ impl PartialDate {
crate::impl_with_fallback_method!(with_fallback_year_month, () PlainYearMonth); // excludes day
crate::impl_with_fallback_method!(with_fallback_date, (with_day: day) PlainDate);
crate::impl_with_fallback_method!(with_fallback_datetime, (with_day:day) PlainDateTime);

// TODO: ZonedDateTime
}

// Use macro to impl fallback methods to avoid having a trait method.
Expand Down
136 changes: 133 additions & 3 deletions src/builtins/core/zoneddatetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,8 +514,55 @@ impl ZonedDateTime {
self.instant
}

pub fn with(&self, _partial: PartialZonedDateTime) -> TemporalResult<Self> {
Err(TemporalError::general("Not yet implemented"))
pub fn with(
&self,
partial: PartialZonedDateTime,
disambiguation: Option<Disambiguation>,
offset_option: Option<OffsetDisambiguation>,
overflow: Option<ArithmeticOverflow>,
provider: &impl TimeZoneProvider,
) -> TemporalResult<Self> {
let overflow = overflow.unwrap_or_default();
let disambiguation = disambiguation.unwrap_or_default();
let offset_option = offset_option.unwrap_or(OffsetDisambiguation::Reject);

let iso_date_time = self.tz.get_iso_datetime_for(&self.instant, provider)?;
let plain_date_time = PlainDateTime::new_unchecked(iso_date_time, self.calendar.clone());

// 23. Let dateTimeResult be ? InterpretTemporalDateTimeFields(calendar, fields, overflow).
let result_date = self.calendar.date_from_partial(
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thought (non-blocking): this strikes me as a bit curious

Now that I'm looking at this in a bit more detail, I'm noticing that we have with_fallback_zoneddatetime, but then we call get_iso_datetime_for immediately after. Did you consider using the return value of get_iso_datetime_for at all to calculate the fallback at all?

&partial.date.with_fallback_datetime(&plain_date_time)?,
overflow,
)?;

let time = iso_date_time.time.with(partial.time, overflow)?;

// 24. Let newOffsetNanoseconds be ! ParseDateTimeUTCOffset(fields.[[OffsetString]]).
let original_offset = self.offset_nanoseconds_with_provider(provider)?;
let new_offset_nanos = partial
.offset
.map(|offset| i64::from(offset.0) * 60_000_000_000)
.or(Some(original_offset));

// 25. Let epochNanoseconds be ? InterpretISODateTimeOffset(dateTimeResult.[[ISODate]], dateTimeResult.[[Time]], option, newOffsetNanoseconds, timeZone, disambiguation, offset, match-exactly).
let epoch_nanos = interpret_isodatetime_offset(
result_date.iso,
Some(time),
false,
new_offset_nanos,
&self.tz,
disambiguation,
offset_option,
true,
provider,
)?;

// 26. Return ! CreateTemporalZonedDateTime(epochNanoseconds, timeZone, calendar).
Ok(Self::new_unchecked(
Instant::from(epoch_nanos),
self.calendar.clone(),
self.tz.clone(),
))
}

/// Creates a new `ZonedDateTime` from the current `ZonedDateTime`
Expand Down Expand Up @@ -1227,7 +1274,9 @@ pub(crate) fn nanoseconds_to_formattable_offset_minutes(
mod tests {
use super::ZonedDateTime;
use crate::{
options::{DifferenceSettings, Disambiguation, OffsetDisambiguation, Unit},
options::{
ArithmeticOverflow, DifferenceSettings, Disambiguation, OffsetDisambiguation, Unit,
},
partial::{PartialDate, PartialTime, PartialZonedDateTime},
time::EpochNanoseconds,
tzdb::FsTzdbProvider,
Expand Down Expand Up @@ -1366,4 +1415,85 @@ mod tests {
assert_eq!(diff.microseconds(), 0);
assert_eq!(diff.nanoseconds(), 0);
}

// overflow-reject-throws.js
#[test]
fn overflow_reject_throws() {
Comment thread
nekevss marked this conversation as resolved.
let provider = &FsTzdbProvider::default();

let zdt =
ZonedDateTime::try_new(217178610123456789, Calendar::default(), TimeZone::default())
.unwrap();

let overflow = ArithmeticOverflow::Reject;

let result_1 = zdt.with(
PartialZonedDateTime {
date: PartialDate {
month: Some(29),
..Default::default()
},
time: PartialTime::default(),
offset: None,
timezone: None,
},
None,
None,
Some(overflow),
provider,
);

let result_2 = zdt.with(
PartialZonedDateTime {
date: PartialDate {
day: Some(31),
..Default::default()
},
time: PartialTime::default(),
offset: None,
timezone: None,
},
None,
None,
Some(overflow),
provider,
);

let result_3 = zdt.with(
PartialZonedDateTime {
date: PartialDate::default(),
time: PartialTime {
hour: Some(29),
..Default::default()
},
offset: None,
timezone: None,
},
None,
None,
Some(overflow),
provider,
);

let result_4 = zdt.with(
PartialZonedDateTime {
date: PartialDate::default(),
time: PartialTime {
nanosecond: Some(9000),
..Default::default()
},
offset: None,
timezone: None,
},
None,
None,
Some(overflow),
provider,
);

assert!(result_1.is_err());
assert!(result_2.is_err());
assert!(result_3.is_err());
assert!(result_4.is_err());
}
}