Skip to content

Commit cb8ff7b

Browse files
authored
Implement a builder API for Now (#296)
This PR implements a builder API for now and closes #249. It has some notable changes to the approach to `Now`. Mainly `Now` is essentially cannot be built without using the new `NowBuilder` and any method called on `Now` consumes it. For general purpose, a `Temporal` namespace struct was created with `now` and `try_now` methods, which uses the `NowBuilder` to construct a `Now` with values from the system. This does differ a bit from the specification in that we are eagerly calling the host system time zone, rather than lazily when the user time zone is `None`. But I think this can still be worked around on the engine side if lazily calling is preferred and is more a place where the native Rust API will differ from the engine implementation. The work around to this is a trait based approach that is available to see on the `now-builder` branch, but I think this a simpler and cleaner approach than leaning on traits. List of changes: - `Now` updated and `NowBuilder` added. - Feature flagged `Temporal` struct - `time` module renamed to `unix_time`
1 parent eaf0c7f commit cb8ff7b

File tree

11 files changed

+204
-206
lines changed

11 files changed

+204
-206
lines changed

src/builtins/compiled/now.rs

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,40 @@
1-
#![cfg(feature = "sys")]
2-
31
use crate::builtins::{
42
core::{Now, PlainDate, PlainDateTime, PlainTime},
53
TZ_PROVIDER,
64
};
7-
use crate::sys;
8-
use crate::{time::EpochNanoseconds, TemporalError, TemporalResult, TimeZone};
5+
use crate::{TemporalError, TemporalResult, TimeZone};
96

107
impl Now {
118
/// Returns the current system time as a [`PlainDateTime`] with an optional
129
/// [`TimeZone`].
1310
///
1411
/// Enable with the `compiled_data` and `sys` feature flags.
15-
pub fn plain_datetime_iso(timezone: Option<TimeZone>) -> TemporalResult<PlainDateTime> {
12+
pub fn plain_date_time_iso(self, time_zone: Option<TimeZone>) -> TemporalResult<PlainDateTime> {
1613
let provider = TZ_PROVIDER
1714
.lock()
1815
.map_err(|_| TemporalError::general("Unable to acquire lock"))?;
19-
let timezone = timezone.unwrap_or(TimeZone::IanaIdentifier(sys::get_system_timezone()?));
20-
let system_nanos = sys::get_system_nanoseconds()?;
21-
let epoch_nanos = EpochNanoseconds::try_from(system_nanos)?;
22-
Now::plain_datetime_iso_with_provider_and_system_info(epoch_nanos, timezone, &*provider)
16+
self.plain_date_time_iso_with_provider(time_zone, &*provider)
2317
}
2418

2519
/// Returns the current system time as a [`PlainDate`] with an optional
2620
/// [`TimeZone`].
2721
///
2822
/// Enable with the `compiled_data` and `sys` feature flags.
29-
pub fn plain_date_iso(timezone: Option<TimeZone>) -> TemporalResult<PlainDate> {
23+
pub fn plain_date_iso(self, time_zone: Option<TimeZone>) -> TemporalResult<PlainDate> {
3024
let provider = TZ_PROVIDER
3125
.lock()
3226
.map_err(|_| TemporalError::general("Unable to acquire lock"))?;
33-
let timezone = timezone.unwrap_or(TimeZone::IanaIdentifier(sys::get_system_timezone()?));
34-
let system_nanos = sys::get_system_nanoseconds()?;
35-
let epoch_nanos = EpochNanoseconds::try_from(system_nanos)?;
36-
Now::plain_date_iso_with_provider_and_system_info(epoch_nanos, timezone, &*provider)
27+
self.plain_date_iso_with_provider(time_zone, &*provider)
3728
}
3829

3930
/// Returns the current system time as a [`PlainTime`] with an optional
4031
/// [`TimeZone`].
4132
///
4233
/// Enable with the `compiled_data` and `sys` feature flags.
43-
pub fn plain_time_iso(timezone: Option<TimeZone>) -> TemporalResult<PlainTime> {
34+
pub fn plain_time_iso(self, time_zone: Option<TimeZone>) -> TemporalResult<PlainTime> {
4435
let provider = TZ_PROVIDER
4536
.lock()
4637
.map_err(|_| TemporalError::general("Unable to acquire lock"))?;
47-
let timezone = timezone.unwrap_or(TimeZone::IanaIdentifier(sys::get_system_timezone()?));
48-
let system_nanos = sys::get_system_nanoseconds()?;
49-
let epoch_nanos = EpochNanoseconds::try_from(system_nanos)?;
50-
Now::plain_time_iso_with_provider_and_system_info(epoch_nanos, timezone, &*provider)
38+
self.plain_time_with_provider(time_zone, &*provider)
5139
}
5240
}

src/builtins/core/instant.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::{
1515
parsers::{parse_instant, IxdtfStringBuilder},
1616
provider::TimeZoneProvider,
1717
rounding::{IncrementRounder, Round},
18-
time::EpochNanoseconds,
18+
unix_time::EpochNanoseconds,
1919
Calendar, TemporalError, TemporalResult, TemporalUnwrap, TimeZone,
2020
};
2121

@@ -335,7 +335,7 @@ mod tests {
335335
use crate::{
336336
builtins::core::{duration::TimeDuration, Instant},
337337
options::{DifferenceSettings, RoundingMode, Unit},
338-
time::EpochNanoseconds,
338+
unix_time::EpochNanoseconds,
339339
NS_MAX_INSTANT, NS_MIN_INSTANT,
340340
};
341341

src/builtins/core/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub(crate) mod zoneddatetime;
2121
mod now;
2222

2323
#[doc(inline)]
24-
pub use now::Now;
24+
pub use now::{Now, NowBuilder};
2525

2626
#[doc(inline)]
2727
pub use date::{PartialDate, PlainDate};

0 commit comments

Comments
 (0)