Skip to content

Implement a builder API for Now #296

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 11, 2025
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
26 changes: 7 additions & 19 deletions src/builtins/compiled/now.rs
Original file line number Diff line number Diff line change
@@ -1,52 +1,40 @@
#![cfg(feature = "sys")]

use crate::builtins::{
core::{Now, PlainDate, PlainDateTime, PlainTime},
TZ_PROVIDER,
};
use crate::sys;
use crate::{time::EpochNanoseconds, TemporalError, TemporalResult, TimeZone};
use crate::{TemporalError, TemporalResult, TimeZone};

impl Now {
/// Returns the current system time as a [`PlainDateTime`] with an optional
/// [`TimeZone`].
///
/// Enable with the `compiled_data` and `sys` feature flags.
pub fn plain_datetime_iso(timezone: Option<TimeZone>) -> TemporalResult<PlainDateTime> {
pub fn plain_date_time_iso(self, time_zone: Option<TimeZone>) -> TemporalResult<PlainDateTime> {
let provider = TZ_PROVIDER
.lock()
.map_err(|_| TemporalError::general("Unable to acquire lock"))?;
let timezone = timezone.unwrap_or(TimeZone::IanaIdentifier(sys::get_system_timezone()?));
let system_nanos = sys::get_system_nanoseconds()?;
let epoch_nanos = EpochNanoseconds::try_from(system_nanos)?;
Now::plain_datetime_iso_with_provider_and_system_info(epoch_nanos, timezone, &*provider)
self.plain_date_time_iso_with_provider(time_zone, &*provider)
}

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

/// Returns the current system time as a [`PlainTime`] with an optional
/// [`TimeZone`].
///
/// Enable with the `compiled_data` and `sys` feature flags.
pub fn plain_time_iso(timezone: Option<TimeZone>) -> TemporalResult<PlainTime> {
pub fn plain_time_iso(self, time_zone: Option<TimeZone>) -> TemporalResult<PlainTime> {
let provider = TZ_PROVIDER
.lock()
.map_err(|_| TemporalError::general("Unable to acquire lock"))?;
let timezone = timezone.unwrap_or(TimeZone::IanaIdentifier(sys::get_system_timezone()?));
let system_nanos = sys::get_system_nanoseconds()?;
let epoch_nanos = EpochNanoseconds::try_from(system_nanos)?;
Now::plain_time_iso_with_provider_and_system_info(epoch_nanos, timezone, &*provider)
self.plain_time_with_provider(time_zone, &*provider)
}
}
4 changes: 2 additions & 2 deletions src/builtins/core/instant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
parsers::{parse_instant, IxdtfStringBuilder},
provider::TimeZoneProvider,
rounding::{IncrementRounder, Round},
time::EpochNanoseconds,
unix_time::EpochNanoseconds,
Calendar, TemporalError, TemporalResult, TemporalUnwrap, TimeZone,
};

Expand Down Expand Up @@ -335,7 +335,7 @@ mod tests {
use crate::{
builtins::core::{duration::TimeDuration, Instant},
options::{DifferenceSettings, RoundingMode, Unit},
time::EpochNanoseconds,
unix_time::EpochNanoseconds,
NS_MAX_INSTANT, NS_MIN_INSTANT,
};

Expand Down
2 changes: 1 addition & 1 deletion src/builtins/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub(crate) mod zoneddatetime;
mod now;

#[doc(inline)]
pub use now::Now;
pub use now::{Now, NowBuilder};

#[doc(inline)]
pub use date::{PartialDate, PlainDate};
Expand Down
Loading