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
4 changes: 2 additions & 2 deletions src/builtins/compiled/duration/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,8 +488,8 @@ fn round_relative_to_zoned_datetime() {
let duration = Duration::from_hours(25);
let zdt = ZonedDateTime::try_new(
1_000_000_000_000_000_000,
Calendar::default(),
TimeZone::try_from_str("+04:30").unwrap(),
Calendar::default(),
)
.unwrap();
let options = RoundingOptions {
Expand Down Expand Up @@ -803,8 +803,8 @@ fn nudge_past_end() {
let duration = Duration::default();
let relative_to = ZonedDateTime::try_new(
86_40000_00000_00000_00000,
Default::default(),
TimeZone::try_from_str("UTC").unwrap(),
Default::default(),
)
.unwrap();
let options = RoundingOptions {
Expand Down
29 changes: 26 additions & 3 deletions src/builtins/compiled/zoned_date_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ use crate::builtins::zoned_date_time::ZonedDateTimeFields;
use crate::builtins::TZ_PROVIDER;
use crate::partial::PartialZonedDateTime;
use crate::provider::TransitionDirection;
use crate::ZonedDateTime;
use crate::{
options::{
DifferenceSettings, Disambiguation, DisplayCalendar, DisplayOffset, DisplayTimeZone,
OffsetDisambiguation, Overflow, RoundingOptions, ToStringRoundingOptions,
},
Calendar, Duration, PlainTime, TemporalResult, TimeZone,
};
use crate::{Instant, ZonedDateTime};
use alloc::string::String;

impl core::fmt::Display for ZonedDateTime {
Expand Down Expand Up @@ -63,9 +63,32 @@ impl ZonedDateTime {
impl ZonedDateTime {
/// Creates a new valid `ZonedDateTime`.
#[inline]
pub fn try_new(nanos: i128, calendar: Calendar, time_zone: TimeZone) -> TemporalResult<Self> {
Self::try_new_with_provider(nanos, calendar, time_zone, &*TZ_PROVIDER)
pub fn try_new(nanos: i128, time_zone: TimeZone, calendar: Calendar) -> TemporalResult<Self> {
Self::try_new_with_provider(nanos, time_zone, calendar, &*TZ_PROVIDER)
}

/// Creates a new valid `ZonedDateTime` with an ISO 8601 calendar.
#[inline]
pub fn try_new_iso(nanos: i128, time_zone: TimeZone) -> TemporalResult<Self> {
Self::try_new_iso_with_provider(nanos, time_zone, &*TZ_PROVIDER)
}

/// Creates a new valid `ZonedDateTime` from an [`Instant`].
#[inline]
pub fn try_new_from_instant(
instant: Instant,
time_zone: TimeZone,
calendar: Calendar,
) -> TemporalResult<Self> {
Self::try_new_from_instant_with_provider(instant, time_zone, calendar, &*TZ_PROVIDER)
}

/// Creates a new valid `ZonedDateTime` from an [`Instant`] with an ISO 8601 calendar.
#[inline]
pub fn try_new_iso_from_instant(instant: Instant, time_zone: TimeZone) -> TemporalResult<Self> {
Self::try_new_iso_from_instant_with_provider(instant, time_zone, &*TZ_PROVIDER)
}

#[inline]
pub fn from_partial(
partial: PartialZonedDateTime,
Expand Down
2 changes: 1 addition & 1 deletion src/builtins/core/instant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ impl Instant {
time_zone: TimeZone,
provider: &impl TimeZoneProvider,
) -> TemporalResult<ZonedDateTime> {
ZonedDateTime::new_unchecked_with_provider(*self, Calendar::default(), time_zone, provider)
ZonedDateTime::new_unchecked_with_provider(*self, time_zone, Calendar::ISO, provider)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/builtins/core/now.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl<H: HostHooks> Now<H> {
let system_nanoseconds = self.host_hooks.get_system_epoch_nanoseconds()?;
let time_zone = time_zone.unwrap_or(self.host_hooks.get_system_time_zone(provider)?);
let instant = Instant::from(system_nanoseconds);
ZonedDateTime::new_unchecked_with_provider(instant, Calendar::ISO, time_zone, provider)
ZonedDateTime::new_unchecked_with_provider(instant, time_zone, Calendar::ISO, provider)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/builtins/core/plain_date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -676,8 +676,8 @@ impl PlainDate {
// 7. Return ! CreateTemporalZonedDateTime(epochNs, timeZone, temporalDate.[[Calendar]]).
ZonedDateTime::try_new_with_cached_offset(
epoch_ns.ns.0,
self.calendar.clone(),
tz,
self.calendar.clone(),
epoch_ns.offset,
)
}
Expand Down
2 changes: 1 addition & 1 deletion src/builtins/core/plain_date_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -899,8 +899,8 @@ impl PlainDateTime {
// 7. Return ! CreateTemporalZonedDateTime(epochNs, timeZone, dateTime.[[Calendar]]).
Ok(ZonedDateTime::new_unchecked(
Instant::from(epoch_ns.ns),
self.calendar.clone(),
time_zone,
self.calendar.clone(),
epoch_ns.offset,
))
}
Expand Down
65 changes: 43 additions & 22 deletions src/builtins/core/zoned_date_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ impl ZonedDateTimeFields {
/// // Create from epoch nanoseconds
/// let zdt = ZonedDateTime::try_new(
/// 0, // epoch nanoseconds (Unix epoch)
/// Calendar::default(), // ISO 8601 calendar
/// TimeZone::utc(), // UTC timezone
/// Calendar::default(), // ISO 8601 calendar
/// ).unwrap();
///
/// assert_eq!(zdt.epoch_milliseconds(), 0);
Expand All @@ -162,8 +162,8 @@ impl ZonedDateTimeFields {
/// let time_zone = TimeZone::try_from_str("America/New_York").unwrap();
/// let zoned_date_time = ZonedDateTime::try_new(
/// 1609459200000000000, // 2021-01-01T00:00:00Z
/// Calendar::default(),
/// time_zone,
/// Calendar::default(),
/// ).unwrap();
///
/// // Note: This would be December 31, 2020 19:00 in New York (EST)
Expand All @@ -184,8 +184,8 @@ impl ZonedDateTimeFields {
/// let time_zone = TimeZone::try_from_str("Europe/London").unwrap();
/// let zdt = ZonedDateTime::try_new(
/// 1609459200000000000, // 2021-01-01T00:00:00Z
/// Calendar::default(),
/// time_zone,
/// Calendar::default(),
/// ).unwrap();
///
/// // Add 6 months
Expand Down Expand Up @@ -227,8 +227,8 @@ impl ZonedDateTimeFields {
///
/// let zdt = ZonedDateTime::try_new(
/// 1609459200000000000,
/// Calendar::default(),
/// TimeZone::try_from_str("Asia/Tokyo").unwrap(),
/// Calendar::default(),
/// ).unwrap();
///
/// let iso_string = zdt.to_ixdtf_string(
Expand Down Expand Up @@ -267,8 +267,8 @@ impl ZonedDateTime {
#[must_use]
pub(crate) fn new_unchecked(
instant: Instant,
calendar: Calendar,
time_zone: TimeZone,
calendar: Calendar,
cached_offset: UtcOffsetSeconds,
) -> Self {
Self {
Expand All @@ -281,8 +281,8 @@ impl ZonedDateTime {

pub(crate) fn new_unchecked_with_provider(
instant: Instant,
calendar: Calendar,
time_zone: TimeZone,
calendar: Calendar,
provider: &impl TimeZoneProvider,
) -> TemporalResult<Self> {
let offset = time_zone
Expand Down Expand Up @@ -364,8 +364,8 @@ impl ZonedDateTime {
// 9. Return ! CreateTemporalZonedDateTime(epochNanoseconds, timeZone, calendar).
Self::new_unchecked_with_provider(
epoch_ns,
self.calendar().clone(),
*self.time_zone(),
self.calendar().clone(),
provider,
)
}
Expand Down Expand Up @@ -588,27 +588,27 @@ impl ZonedDateTime {
#[inline]
pub fn try_new_with_provider(
nanos: i128,
calendar: Calendar,
time_zone: TimeZone,
calendar: Calendar,
provider: &impl TimeZoneProvider,
) -> TemporalResult<Self> {
let instant = Instant::try_new(nanos)?;
Self::new_unchecked_with_provider(instant, calendar, time_zone, provider)
Self::new_unchecked_with_provider(instant, time_zone, calendar, provider)
}

/// Creates a new valid `ZonedDateTime`.
#[inline]
pub(crate) fn try_new_with_cached_offset(
nanos: i128,
calendar: Calendar,
time_zone: TimeZone,
calendar: Calendar,
cached_offset: UtcOffsetSeconds,
) -> TemporalResult<Self> {
let instant = Instant::try_new(nanos)?;
Ok(Self::new_unchecked(
instant,
calendar,
time_zone,
calendar,
cached_offset,
))
}
Expand All @@ -621,7 +621,28 @@ impl ZonedDateTime {
provider: &impl TimeZoneProvider,
) -> TemporalResult<Self> {
let instant = Instant::try_new(nanos)?;
Self::new_unchecked_with_provider(instant, Calendar::default(), time_zone, provider)
Self::new_unchecked_with_provider(instant, time_zone, Calendar::ISO, provider)
}

/// Creates a new valid `ZonedDateTime` from an [`Instant`].
#[inline]
pub fn try_new_from_instant_with_provider(
instant: Instant,
time_zone: TimeZone,
calendar: Calendar,
provider: &impl TimeZoneProvider,
) -> TemporalResult<Self> {
Self::new_unchecked_with_provider(instant, time_zone, calendar, provider)
}

/// Creates a new valid `ZonedDateTime` from an [`Instant`] with an ISO 8601 calendar.
#[inline]
pub fn try_new_iso_from_instant_with_provider(
instant: Instant,
time_zone: TimeZone,
provider: &impl TimeZoneProvider,
) -> TemporalResult<Self> {
Self::new_unchecked_with_provider(instant, time_zone, Calendar::ISO, provider)
}

/// Returns `ZonedDateTime`'s Calendar.
Expand Down Expand Up @@ -678,8 +699,8 @@ impl ZonedDateTime {

Ok(Self::new_unchecked(
Instant::from(epoch_nanos.ns),
partial.calendar,
timezone,
partial.calendar,
epoch_nanos.offset,
))
}
Expand Down Expand Up @@ -755,8 +776,8 @@ impl ZonedDateTime {
// 26. Return ! CreateTemporalZonedDateTime(epochNanoseconds, timeZone, calendar).
Ok(Self::new_unchecked(
Instant::from(epoch_nanos.ns),
self.calendar.clone(),
self.time_zone,
self.calendar.clone(),
epoch_nanos.offset,
))
}
Expand All @@ -770,8 +791,8 @@ impl ZonedDateTime {
) -> TemporalResult<Self> {
Self::try_new_with_provider(
self.epoch_nanoseconds().as_i128(),
self.calendar.clone(),
time_zone,
self.calendar.clone(),
provider,
)
}
Expand All @@ -783,8 +804,8 @@ impl ZonedDateTime {
pub fn with_calendar(&self, calendar: Calendar) -> Self {
Self::new_unchecked(
self.instant,
calendar,
self.time_zone,
calendar,
self.cached_offset.into(),
)
}
Expand Down Expand Up @@ -842,8 +863,8 @@ impl ZonedDateTime {
Ok(Some(
ZonedDateTime::try_new_with_provider(
transition.0,
self.calendar().clone(),
self.time_zone,
self.calendar().clone(),
provider,
)
.ok()
Expand Down Expand Up @@ -1091,8 +1112,8 @@ impl ZonedDateTime {
};
Self::try_new_with_cached_offset(
epoch_ns.ns.0,
self.calendar.clone(),
self.time_zone,
self.calendar.clone(),
epoch_ns.offset,
)
}
Expand Down Expand Up @@ -1173,8 +1194,8 @@ impl ZonedDateTime {
let epoch_nanos = self.time_zone.get_start_of_day(&iso.date, provider)?;
Self::try_new_with_cached_offset(
epoch_nanos.ns.0,
self.calendar.clone(),
self.time_zone,
self.calendar.clone(),
epoch_nanos.offset,
)
}
Expand Down Expand Up @@ -1297,8 +1318,8 @@ impl ZonedDateTime {
// 20. Return ! CreateTemporalZonedDateTime(epochNanoseconds, timeZone, calendar).
ZonedDateTime::try_new_with_cached_offset(
candidate,
self.calendar.clone(),
self.time_zone,
self.calendar.clone(),
offset,
)
} else {
Expand Down Expand Up @@ -1328,8 +1349,8 @@ impl ZonedDateTime {

ZonedDateTime::try_new_with_cached_offset(
epoch_ns.ns.0,
self.calendar.clone(),
self.time_zone,
self.calendar.clone(),
epoch_ns.offset,
)
}
Expand Down Expand Up @@ -1409,8 +1430,8 @@ impl ZonedDateTime {
)?;
Ok(Self::new_unchecked(
Instant::from(epoch_nanos.ns),
Calendar::new(parsed.date.calendar),
parsed.timezone,
Calendar::new(parsed.date.calendar),
epoch_nanos.offset,
))
}
Expand Down
Loading
Loading