Skip to content
Merged
Show file tree
Hide file tree
Changes from 12 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
16 changes: 16 additions & 0 deletions src/builtins/compiled/date.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use crate::{builtins::TZ_PROVIDER, TemporalError, TemporalResult, PlainDate, PlainTime};

impl PlainDate {

/// Converts a `Date` to a `ZonedDateTime` in the UTC time zone.
pub fn to_zoned_date_time(
&self,
time_zone: TimeZone,
plain_time: Option<PlainTime>
) -> TemporalResult<crate::ZonedDateTime> {
let provider = TZ_PROVIDER
.lock()
.map_err(|_| TemporalError::general("Unable to acquire lock"))?;
self.to_zoned_date_time_with_provider(time_zone, plain_time, &*provider)
}
}
75 changes: 70 additions & 5 deletions src/builtins/core/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
use crate::{
builtins::core::{
calendar::Calendar, duration::DateDuration, Duration, PlainDateTime, PlainTime,
ZonedDateTime,
},
iso::{IsoDate, IsoDateTime, IsoTime},
options::{
ArithmeticOverflow, DifferenceOperation, DifferenceSettings, DisplayCalendar,
ResolvedRoundingOptions, TemporalUnit, UnitGroup,
ArithmeticOverflow, DifferenceOperation, DifferenceSettings, Disambiguation,
DisplayCalendar, ResolvedRoundingOptions, TemporalUnit, UnitGroup,
},
parsers::{parse_date_time, IxdtfStringBuilder},
primitive::FiniteF64,
provider::NeverProvider,
provider::{NeverProvider, TimeZoneProvider},
MonthCode, TemporalError, TemporalResult, TemporalUnwrap, TimeZone,
};
use alloc::{format, string::String};
Expand Down Expand Up @@ -651,8 +652,50 @@ impl PlainDate {
.with_calendar(self.calendar.identifier(), display_calendar)
.build()
}
}

#[inline]
pub fn to_zoned_date_time_with_provider(
&self,
tz: TimeZone,
plain_time: Option<PlainTime>,
provider: &impl TimeZoneProvider,
) -> TemporalResult<ZonedDateTime> {
// 1. Let temporalDate be the this value.
// 2. Perform ? RequireInternalSlot(temporalDate, [[InitializedTemporalDate]]).
// 3. If item is an Object, then
// a. Let timeZoneLike be ? Get(item, "timeZone").
// b. If timeZoneLike is undefined, then
// i. Let timeZone be ? ToTemporalTimeZoneIdentifier(item).
// ii. Let temporalTime be undefined.
// c. Else,
// i. Let timeZone be ? ToTemporalTimeZoneIdentifier(timeZoneLike).
// ii. Let temporalTime be ? Get(item, "plainTime").
// 4. Else,
// a. Let timeZone be ? ToTemporalTimeZoneIdentifier(item).
// b. Let temporalTime be undefined.

// 5. If temporalTime is undefined, then
// a. Let epochNs be ? GetStartOfDay(timeZone, temporalDate.[[ISODate]]).
// 6. Else,
// a. Set temporalTime to ? ToTemporalTime(temporalTime).
// b. Let isoDateTime be CombineISODateAndTimeRecord(temporalDate.[[ISODate]], temporalTime.[[Time]]).
// c. If ISODateTimeWithinLimits(isoDateTime) is false, throw a RangeError exception.
// d. Let epochNs be ? GetEpochNanosecondsFor(timeZone, isoDateTime, compatible).
let epoch_ns = if let Some(time) = plain_time {
let result_iso = IsoDateTime::new(self.iso, time.iso);

tz.get_epoch_nanoseconds_for(
result_iso.unwrap_or_default(),
Disambiguation::Compatible,
provider,
)?
} else {
tz.get_start_of_day(&self.iso, provider)?
};
// 7. Return ! CreateTemporalZonedDateTime(epochNs, timeZone, temporalDate.[[Calendar]]).
ZonedDateTime::try_new(epoch_ns.0, self.calendar.clone(), tz)
}
}
// ==== Trait impls ====

impl From<PlainDateTime> for PlainDate {
Expand Down Expand Up @@ -682,9 +725,11 @@ impl FromStr for PlainDate {
}
}

#[cfg(test)]
#[cfg(all(test, feature = "tzdb"))]
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.

nit: the feature configuration shouldn't be here I think. You should be able to configure specific tests. With #[cfg(feature = "tzdb"] above the specific tests. I think references can be found in other test modules.

mod tests {
use tinystr::tinystr;
use crate::tzdb::FsTzdbProvider;


use super::*;

Expand Down Expand Up @@ -947,6 +992,26 @@ mod tests {
assert_eq!(with_day.day().unwrap(), 17);
}

// test toZonedDateTime
#[test]
fn to_zoned_date_time() {
let date = PlainDate::from_str("2020-01-01").unwrap();
let tz = TimeZone::try_from_str("UTC").unwrap();
let provider = &FsTzdbProvider::default();
let zdt = date
.to_zoned_date_time_with_provider(tz, None, provider)
.unwrap();
assert_eq!(zdt.year_with_provider(provider).unwrap(), 2020);
assert_eq!(zdt.month_with_provider(provider).unwrap(), 1);
assert_eq!(zdt.day_with_provider(provider).unwrap(), 1);
assert_eq!(zdt.hour_with_provider(provider).unwrap(), 0);
assert_eq!(zdt.minute_with_provider(provider).unwrap(), 0);
assert_eq!(zdt.second_with_provider(provider).unwrap(), 0);
assert_eq!(zdt.millisecond_with_provider(provider).unwrap(), 0);
assert_eq!(zdt.microsecond_with_provider(provider).unwrap(), 0);
assert_eq!(zdt.nanosecond_with_provider(provider).unwrap(), 0);
}

// test262/test/built-ins/Temporal/Calendar/prototype/month/argument-string-invalid.js
#[test]
fn invalid_strings() {
Expand Down