Skip to content

Commit 12bb6cb

Browse files
committed
Add epoch_ns_for_utc
1 parent f7ae910 commit 12bb6cb

26 files changed

+191
-3
lines changed

src/builtins/core/plain_date.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use crate::{
1717
},
1818
parsers::IxdtfStringBuilder,
1919
provider::{NeverProvider, TimeZoneProvider},
20+
unix_time::EpochNanoseconds,
2021
MonthCode, TemporalError, TemporalResult, TimeZone,
2122
};
2223
use alloc::string::String;
@@ -685,6 +686,18 @@ impl PlainDate {
685686
epoch_ns.offset,
686687
)
687688
}
689+
690+
/// Gets the EpochNanoseconds represented by this PlainDate
691+
/// (using noon time, and UTC timezone)
692+
///
693+
// Useful for implementing HandleDateTimeTemporalYearMonth
694+
pub fn epoch_ns_for_utc(&self) -> EpochNanoseconds {
695+
// 2. Let isoDateTime be CombineISODateAndTimeRecord(temporalYearMonth.[[ISODate]], NoonTimeRecord()).
696+
let iso = IsoDateTime::new(self.iso, IsoTime::noon());
697+
debug_assert!(iso.is_ok());
698+
// 3. Let epochNs be ? GetUTCEpochNanoseconds(isoDateTime).
699+
iso.unwrap_or_default().as_nanoseconds()
700+
}
688701
}
689702

690703
// ==== Trait impls ====

src/builtins/core/plain_date_time.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use crate::{
1919
parsers::IxdtfStringBuilder,
2020
primitive::FiniteF64,
2121
provider::{NeverProvider, TimeZoneProvider},
22+
unix_time::EpochNanoseconds,
2223
MonthCode, TemporalError, TemporalResult, TimeZone,
2324
};
2425
use alloc::string::String;
@@ -907,6 +908,15 @@ impl PlainDateTime {
907908
))
908909
}
909910

911+
/// Gets the EpochNanoseconds represented by this PlainDateTime
912+
/// (using and UTC timezone)
913+
///
914+
// Useful for implementing HandleDateTimeTemporalDateTime
915+
pub fn epoch_ns_for_utc(&self) -> EpochNanoseconds {
916+
// 3. Let epochNs be ? GetUTCEpochNanoseconds(isoDateTime).
917+
self.iso.as_nanoseconds()
918+
}
919+
910920
/// Create a [`PlainDate`] from the current `PlainDateTime`.
911921
#[inline]
912922
pub fn to_plain_date(&self) -> PlainDate {

src/builtins/core/plain_month_day.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,18 @@ impl PlainMonthDay {
364364
.ns)
365365
}
366366

367+
/// Gets the EpochNanoseconds represented by this MonthDay
368+
/// (using the reference year, noon time, and UTC timezone)
369+
///
370+
// Useful for implementing HandleDateTimeTemporalYearMonth
371+
pub fn epoch_ns_for_utc(&self) -> EpochNanoseconds {
372+
// 2. Let isoDateTime be CombineISODateAndTimeRecord(temporalMonthDay.[[ISODate]], NoonTimeRecord()).
373+
let iso = IsoDateTime::new(self.iso, IsoTime::noon());
374+
debug_assert!(iso.is_ok());
375+
// 3. Let epochNs be ? GetUTCEpochNanoseconds(isoDateTime).
376+
iso.unwrap_or_default().as_nanoseconds()
377+
}
378+
367379
/// Creates a RFC9557 IXDTF string from the current `PlainMonthDay`.
368380
pub fn to_ixdtf_string(&self, display_calendar: DisplayCalendar) -> String {
369381
self.to_ixdtf_writeable(display_calendar)

src/builtins/core/plain_time.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ use crate::{
77
},
88
error::ErrorMessage,
99
iso::IsoTime,
10+
iso::{IsoDate, IsoDateTime},
1011
options::{
1112
DifferenceOperation, DifferenceSettings, Overflow, ResolvedRoundingOptions,
1213
RoundingOptions, ToStringRoundingOptions, Unit, UnitGroup,
1314
},
1415
parsers::{parse_time, IxdtfStringBuilder},
16+
unix_time::EpochNanoseconds,
1517
TemporalError, TemporalResult,
1618
};
1719
use alloc::string::String;
@@ -541,6 +543,19 @@ impl PlainTime {
541543
let builder = IxdtfStringBuilder::default().with_time(result, resolved.precision);
542544
Ok(builder)
543545
}
546+
547+
/// Gets the EpochNanoseconds represented by this PlainTime
548+
/// (using the Unix epoch, and UTC timezone)
549+
///
550+
// Useful for implementing HandleDateTimeTemporalTime
551+
pub fn epoch_ns_for_utc(&self) -> EpochNanoseconds {
552+
// 1. Let isoDate be CreateISODateRecord(1970, 1, 1).
553+
// 2. Let isoDateTime be CombineISODateAndTimeRecord(isoDate, temporalTime.[[Time]]).
554+
let iso = IsoDateTime::new(IsoDate::UNIX_EPOCH, self.iso);
555+
debug_assert!(iso.is_ok());
556+
// 3. Let epochNs be ? GetUTCEpochNanoseconds(isoDateTime).
557+
iso.unwrap_or_default().as_nanoseconds()
558+
}
544559
}
545560

546561
impl From<PlainDateTime> for PlainTime {

src/builtins/core/plain_year_month.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -598,9 +598,7 @@ impl PlainYearMonth {
598598
}
599599

600600
/// Gets the epochMilliseconds represented by this YearMonth in the given timezone
601-
/// (using the reference year, and noon time)
602-
///
603-
// Useful for implementing HandleDateTimeTemporalYearMonth
601+
/// (using the reference day, and noon time)
604602
pub fn epoch_ns_for_with_provider(
605603
&self,
606604
time_zone: TimeZone,
@@ -614,6 +612,18 @@ impl PlainYearMonth {
614612
.ns)
615613
}
616614

615+
/// Gets the EpochNanoseconds represented by this YearMonth
616+
/// (using the reference day, noon time, and UTC timezone)
617+
///
618+
// Useful for implementing HandleDateTimeTemporalYearMonth
619+
pub fn epoch_ns_for_utc(&self) -> EpochNanoseconds {
620+
// 2. Let isoDateTime be CombineISODateAndTimeRecord(temporalYearMonth.[[ISODate]], NoonTimeRecord()).
621+
let iso = IsoDateTime::new(self.iso, IsoTime::noon());
622+
debug_assert!(iso.is_ok());
623+
// 3. Let epochNs be ? GetUTCEpochNanoseconds(isoDateTime).
624+
iso.unwrap_or_default().as_nanoseconds()
625+
}
626+
617627
/// Returns a RFC9557 IXDTF string for the current `PlainYearMonth`
618628
#[inline]
619629
pub fn to_ixdtf_string(&self, display_calendar: DisplayCalendar) -> String {

src/iso.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,8 @@ pub struct IsoDate {
264264
}
265265

266266
impl IsoDate {
267+
pub(crate) const UNIX_EPOCH: Self = Self::new_unchecked(1970, 1, 1);
268+
267269
/// Creates a new `IsoDate` without determining the validity.
268270
pub(crate) const fn new_unchecked(year: i32, month: u8, day: u8) -> Self {
269271
Self { year, month, day }

temporal_capi/bindings/c/PlainDate.h

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

temporal_capi/bindings/c/PlainDateTime.h

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

temporal_capi/bindings/c/PlainMonthDay.h

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

temporal_capi/bindings/c/PlainTime.h

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

0 commit comments

Comments
 (0)