Skip to content

Commit 5d21776

Browse files
authored
initial implementation of ToZonedDateTime, ToPlainDate, ToPlainTime (#244)
Fixes #149
1 parent 5e864d8 commit 5d21776

File tree

6 files changed

+76
-6
lines changed

6 files changed

+76
-6
lines changed

src/builtins/compiled/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
mod duration;
44
mod instant;
55
mod now;
6+
mod plain_date_time;
67
mod zoneddatetime;
78

89
mod options {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use crate::{
2+
builtins::core::{PlainDateTime, ZonedDateTime},
3+
builtins::TZ_PROVIDER,
4+
options::Disambiguation,
5+
TemporalError, TemporalResult, TimeZone,
6+
};
7+
8+
impl PlainDateTime {
9+
/// Returns a `ZonedDateTime` with the provided `PlainDateTime`, TimeZone` and
10+
/// `Disambiguation`
11+
/// Enable with the `compiled_data` feature flag.
12+
pub fn to_zoned_date_time(
13+
&self,
14+
time_zone: &TimeZone,
15+
disambiguation: Disambiguation,
16+
) -> TemporalResult<ZonedDateTime> {
17+
let provider = TZ_PROVIDER
18+
.lock()
19+
.map_err(|_| TemporalError::general("Unable to acquire lock"))?;
20+
21+
self.to_zoned_date_time_with_provider(time_zone, disambiguation, &*provider)
22+
}
23+
}

src/builtins/core/datetime.rs

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,19 @@
22
33
use super::{
44
duration::normalized::{NormalizedDurationRecord, NormalizedTimeDuration},
5-
Duration, PartialDate, PartialTime, PlainDate, PlainTime,
5+
Duration, PartialDate, PartialTime, PlainDate, PlainTime, ZonedDateTime,
66
};
77
use crate::{
88
builtins::core::{calendar::Calendar, Instant},
99
iso::{IsoDate, IsoDateTime, IsoTime},
1010
options::{
11-
ArithmeticOverflow, DifferenceOperation, DifferenceSettings, DisplayCalendar,
12-
ResolvedRoundingOptions, RoundingOptions, TemporalUnit, ToStringRoundingOptions, UnitGroup,
11+
ArithmeticOverflow, DifferenceOperation, DifferenceSettings, Disambiguation,
12+
DisplayCalendar, ResolvedRoundingOptions, RoundingOptions, TemporalUnit,
13+
ToStringRoundingOptions, UnitGroup,
1314
},
1415
parsers::{parse_date_time, IxdtfStringBuilder},
1516
primitive::FiniteF64,
16-
provider::NeverProvider,
17+
provider::{NeverProvider, TimeZoneProvider},
1718
temporal_assert, MonthCode, TemporalError, TemporalResult, TemporalUnwrap, TimeZone,
1819
};
1920
use alloc::string::String;
@@ -682,8 +683,33 @@ impl PlainDateTime {
682683
Ok(Self::new_unchecked(result, self.calendar.clone()))
683684
}
684685

686+
pub fn to_zoned_date_time_with_provider(
687+
&self,
688+
time_zone: &TimeZone,
689+
disambiguation: Disambiguation,
690+
provider: &impl TimeZoneProvider,
691+
) -> TemporalResult<ZonedDateTime> {
692+
// 6. Let epochNs be ? GetEpochNanosecondsFor(timeZone, dateTime.[[ISODateTime]], disambiguation).
693+
let epoch_ns = time_zone.get_epoch_nanoseconds_for(self.iso, disambiguation, provider)?;
694+
// 7. Return ! CreateTemporalZonedDateTime(epochNs, timeZone, dateTime.[[Calendar]]).
695+
Ok(ZonedDateTime::new_unchecked(
696+
Instant::from(epoch_ns),
697+
self.calendar.clone(),
698+
time_zone.clone(),
699+
))
700+
}
701+
702+
pub fn to_plain_date(&self) -> TemporalResult<PlainDate> {
703+
// 3. Return ! CreateTemporalDate(dateTime.[[ISODateTime]].[[ISODate]], dateTime.[[Calendar]]).
704+
Ok(PlainDate::new_unchecked(
705+
self.iso.date,
706+
self.calendar.clone(),
707+
))
708+
}
709+
685710
pub fn to_plain_time(&self) -> TemporalResult<PlainTime> {
686-
Err(TemporalError::general("Not yet implemented."))
711+
// 3. Return ! CreateTemporalTime(dateTime.[[ISODateTime]].[[Time]]).
712+
Ok(PlainTime::new_unchecked(self.iso.time))
687713
}
688714

689715
pub fn to_ixdtf_string(

temporal_capi/bindings/cpp/temporal_rs/PlainDateTime.d.hpp

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

temporal_capi/bindings/cpp/temporal_rs/PlainDateTime.hpp

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

temporal_capi/src/plain_date_time.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub mod ffi {
1212
ArithmeticOverflow, DifferenceSettings, DisplayCalendar, RoundingOptions,
1313
ToStringRoundingOptions,
1414
};
15-
use crate::plain_date::ffi::PartialDate;
15+
use crate::plain_date::ffi::{PartialDate, PlainDate};
1616
use crate::plain_time::ffi::{PartialTime, PlainTime};
1717
use diplomat_runtime::DiplomatWrite;
1818
use std::fmt::Write;
@@ -249,6 +249,13 @@ pub mod ffi {
249249
.map_err(Into::into)
250250
}
251251

252+
pub fn to_plain_date(&self) -> Result<Box<PlainDate>, TemporalError> {
253+
self.0
254+
.to_plain_date()
255+
.map(|x| Box::new(PlainDate(x)))
256+
.map_err(Into::into)
257+
}
258+
252259
pub fn to_plain_time(&self) -> Result<Box<PlainTime>, TemporalError> {
253260
self.0
254261
.to_plain_time()

0 commit comments

Comments
 (0)