Skip to content

Commit db35907

Browse files
committed
Remove hooks in favor of user defined values
1 parent b5e73f7 commit db35907

File tree

4 files changed

+128
-94
lines changed

4 files changed

+128
-94
lines changed

Diff for: src/builtins/compiled/now.rs

+25-11
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
use crate::builtins::{
2-
core::{Now, PlainDate, PlainDateTime, PlainTime},
2+
core::{Now, PlainDate, PlainDateTime, PlainTime, ZonedDateTime},
33
TZ_PROVIDER,
44
};
5-
use crate::{TemporalError, TemporalResult, TimeZone};
6-
7-
#[cfg(feature = "sys")]
8-
use crate::sys::DefaultSystemHooks;
5+
use crate::sys;
6+
use crate::{time::EpochNanoseconds, TemporalError, TemporalResult, TimeZone};
97

108
#[cfg(feature = "sys")]
119
impl Now {
10+
/// Returns the current system time as a [`PlainDateTime`] with an optional
11+
/// [`TimeZone`].
12+
pub fn zoneddatetime_iso(timezone: Option<TimeZone>) -> TemporalResult<ZonedDateTime> {
13+
let timezone =
14+
timezone.unwrap_or(TimeZone::IanaIdentifier(crate::sys::get_system_timezone()?));
15+
let system_nanos = sys::get_system_nanoseconds()?;
16+
let epoch_nanos = EpochNanoseconds::try_from(system_nanos)?;
17+
Now::zoneddatetime_iso_with_system_values(epoch_nanos, timezone)
18+
}
19+
1220
/// Returns the current system time as a [`PlainDateTime`] with an optional
1321
/// [`TimeZone`].
1422
///
@@ -17,8 +25,10 @@ impl Now {
1725
let provider = TZ_PROVIDER
1826
.lock()
1927
.map_err(|_| TemporalError::general("Unable to acquire lock"))?;
20-
Now::plain_datetime_iso_with_hooks_and_provider(timezone, &DefaultSystemHooks, &*provider)
21-
.map(Into::into)
28+
let timezone = timezone.unwrap_or(TimeZone::IanaIdentifier(sys::get_system_timezone()?));
29+
let system_nanos = sys::get_system_nanoseconds()?;
30+
let epoch_nanos = EpochNanoseconds::try_from(system_nanos)?;
31+
Now::plain_datetime_iso_with_provider(epoch_nanos, timezone, &*provider)
2232
}
2333

2434
/// Returns the current system time as a [`PlainDate`] with an optional
@@ -29,8 +39,10 @@ impl Now {
2939
let provider = TZ_PROVIDER
3040
.lock()
3141
.map_err(|_| TemporalError::general("Unable to acquire lock"))?;
32-
Now::plain_date_iso_with_hooks_and_provider(timezone, &DefaultSystemHooks, &*provider)
33-
.map(Into::into)
42+
let timezone = timezone.unwrap_or(TimeZone::IanaIdentifier(sys::get_system_timezone()?));
43+
let system_nanos = sys::get_system_nanoseconds()?;
44+
let epoch_nanos = EpochNanoseconds::try_from(system_nanos)?;
45+
Now::plain_date_iso_with_provider(epoch_nanos, timezone, &*provider)
3446
}
3547

3648
/// Returns the current system time as a [`PlainTime`] with an optional
@@ -41,7 +53,9 @@ impl Now {
4153
let provider = TZ_PROVIDER
4254
.lock()
4355
.map_err(|_| TemporalError::general("Unable to acquire lock"))?;
44-
Now::plain_time_iso_with_hooks_and_provider(timezone, &DefaultSystemHooks, &*provider)
45-
.map(Into::into)
56+
let timezone = timezone.unwrap_or(TimeZone::IanaIdentifier(sys::get_system_timezone()?));
57+
let system_nanos = sys::get_system_nanoseconds()?;
58+
let epoch_nanos = EpochNanoseconds::try_from(system_nanos)?;
59+
Now::plain_time_iso_with_provider(epoch_nanos, timezone, &*provider)
4660
}
4761
}

Diff for: src/builtins/core/now.rs

+96-58
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22
33
use crate::iso::IsoDateTime;
44
use crate::provider::TimeZoneProvider;
5-
use crate::sys::SystemHooks;
5+
use crate::time::EpochNanoseconds;
66
use crate::TemporalResult;
7-
use alloc::string::String;
87

98
#[cfg(feature = "sys")]
10-
use crate::sys::DefaultSystemHooks;
9+
use alloc::string::String;
1110

1211
use super::{
1312
calendar::Calendar, timezone::TimeZone, Instant, PlainDate, PlainDateTime, PlainTime,
@@ -18,47 +17,61 @@ use super::{
1817
pub struct Now;
1918

2019
impl Now {
21-
pub fn instant_with_hooks(system_hooks: &impl SystemHooks) -> TemporalResult<Instant> {
22-
let epoch_nanoseconds = system_hooks.get_system_nanoseconds()?;
23-
Ok(Instant::from(epoch_nanoseconds))
24-
}
25-
26-
pub fn system_time_zone_identifier_with_hooks(
27-
system_hooks: &impl SystemHooks,
28-
) -> TemporalResult<String> {
29-
system_hooks.get_system_time_zone()
20+
pub fn instant_with_epoch_nanoseconds(epoch_nanoseconds: EpochNanoseconds) -> Instant {
21+
Instant::from(epoch_nanoseconds)
3022
}
3123

32-
pub fn system_datetime_with_hooks_and_provider(
33-
time_zone: Option<TimeZone>,
34-
system_hooks: &impl SystemHooks,
24+
/// Returns the current system `DateTime` based off the provided system args
25+
///
26+
/// ## Order of operations
27+
///
28+
/// The order of operations for this method requires the `GetSystemTimeZone` call
29+
/// to occur prior to calling system time and resolving the `EpochNanoseconds`
30+
/// value.
31+
///
32+
/// A correct implementation will follow the following steps:
33+
///
34+
/// 1. Resolve user input `TimeZone` with the `SystemTimeZone`.
35+
/// 2. Get the `SystemNanoseconds`
36+
///
37+
/// For an example implementation see [`Self::zoneddatetime_iso`]
38+
///
39+
pub(crate) fn system_datetime_with_provider(
40+
epoch_nanoseconds: EpochNanoseconds,
41+
timezone: TimeZone,
3542
provider: &impl TimeZoneProvider,
3643
) -> TemporalResult<IsoDateTime> {
3744
// 1. If temporalTimeZoneLike is undefined, then
3845
// a. Let timeZone be SystemTimeZoneIdentifier().
3946
// 2. Else,
4047
// a. Let timeZone be ? ToTemporalTimeZoneIdentifier(temporalTimeZoneLike).
41-
let tz = time_zone.unwrap_or(TimeZone::IanaIdentifier(
42-
system_hooks.get_system_time_zone()?,
43-
));
4448
// 3. Let epochNs be SystemUTCEpochNanoseconds().
45-
let epoch_ns = system_hooks.get_system_nanoseconds()?;
4649
// 4. Return GetISODateTimeFor(timeZone, epochNs).
47-
tz.get_iso_datetime_for(&Instant::from(epoch_ns), provider)
50+
timezone.get_iso_datetime_for(&Instant::from(epoch_nanoseconds), provider)
4851
}
4952

5053
/// Returns the current system time as a `ZonedDateTime` with an ISO8601 calendar.
5154
///
5255
/// The time zone will be set to either the `TimeZone` if a value is provided, or
5356
/// according to the system timezone if no value is provided.
54-
pub fn zoneddatetime_iso_with_hooks(
55-
timezone: Option<TimeZone>,
56-
system_hooks: &impl SystemHooks,
57+
///
58+
/// ## Order of operations
59+
///
60+
/// The order of operations for this method requires the `GetSystemTimeZone` call
61+
/// to occur prior to calling system time and resolving the `EpochNanoseconds`
62+
/// value.
63+
///
64+
/// A correct implementation will follow the following steps:
65+
///
66+
/// 1. Resolve user input `TimeZone` with the `SystemTimeZone`.
67+
/// 2. Get the `SystemNanoseconds`
68+
///
69+
/// For an example implementation see [`Self::zoneddatetime_iso`]
70+
pub fn zoneddatetime_iso_with_system_values(
71+
epoch_nanos: EpochNanoseconds,
72+
timezone: TimeZone,
5773
) -> TemporalResult<ZonedDateTime> {
58-
let timezone = timezone.unwrap_or(TimeZone::IanaIdentifier(
59-
system_hooks.get_system_time_zone()?,
60-
));
61-
let instant = Self::instant_with_hooks(system_hooks)?;
74+
let instant = Self::instant_with_epoch_nanoseconds(epoch_nanos);
6275
Ok(ZonedDateTime::new_unchecked(
6376
instant,
6477
Calendar::default(),
@@ -71,79 +84,104 @@ impl Now {
7184
impl Now {
7285
/// Returns the current instant
7386
pub fn instant() -> TemporalResult<Instant> {
74-
Self::instant_with_hooks(&DefaultSystemHooks)
87+
let system_nanos = crate::sys::get_system_nanoseconds()?;
88+
let epoch_nanos = EpochNanoseconds::try_from(system_nanos)?;
89+
Ok(Self::instant_with_epoch_nanoseconds(epoch_nanos))
7590
}
7691

7792
/// Returns the current time zone.
7893
pub fn time_zone_identifier() -> TemporalResult<String> {
79-
Self::system_time_zone_identifier_with_hooks(&DefaultSystemHooks)
94+
crate::sys::get_system_timezone()
8095
}
8196
}
8297

8398
impl Now {
8499
/// Returns the current system time as a `PlainDateTime` with an ISO8601 calendar.
85100
///
86-
/// The time zone used to calculate the `PlainDateTime` will be set to either the
87-
/// `TimeZone` if a value is provided, or according to the system timezone if no
88-
/// value is provided.
89-
pub fn plain_datetime_iso_with_hooks_and_provider(
90-
timezone: Option<TimeZone>,
91-
system_hooks: &impl SystemHooks,
101+
/// ## Order of operations
102+
///
103+
/// The order of operations for this method requires the `GetSystemTimeZone` call
104+
/// to occur prior to calling system time and resolving the `EpochNanoseconds`
105+
/// value.
106+
///
107+
/// A correct implementation will follow the following steps:
108+
///
109+
/// 1. Resolve user input `TimeZone` with the `SystemTimeZone`.
110+
/// 2. Get the `SystemNanoseconds`
111+
///
112+
/// For an example implementation see [`Self::plain_date_time_iso`]
113+
pub fn plain_datetime_iso_with_provider(
114+
epoch_nanos: EpochNanoseconds,
115+
timezone: TimeZone,
92116
provider: &impl TimeZoneProvider,
93117
) -> TemporalResult<PlainDateTime> {
94-
let iso = Self::system_datetime_with_hooks_and_provider(timezone, system_hooks, provider)?;
118+
let iso = Self::system_datetime_with_provider(epoch_nanos, timezone, provider)?;
95119
Ok(PlainDateTime::new_unchecked(iso, Calendar::default()))
96120
}
97121

98122
/// Returns the current system time as a `PlainDate` with an ISO8601 calendar.
99123
///
100-
/// The time zone used to calculate the `PlainDate` will be set to either the
101-
/// `TimeZone` if a value is provided, or according to the system timezone if no
102-
/// value is provided.
103-
pub fn plain_date_iso_with_hooks_and_provider(
104-
timezone: Option<TimeZone>,
105-
system_hooks: &impl SystemHooks,
124+
/// ## Order of operations
125+
///
126+
/// The order of operations for this method requires the `GetSystemTimeZone` call
127+
/// to occur prior to calling system time and resolving the `EpochNanoseconds`
128+
/// value.
129+
///
130+
/// A correct implementation will follow the following steps:
131+
///
132+
/// 1. Resolve user input `TimeZone` with the `SystemTimeZone`.
133+
/// 2. Get the `SystemNanoseconds`
134+
///
135+
/// For an example implementation see [`Self::plain_date_iso`]
136+
pub fn plain_date_iso_with_provider(
137+
epoch_nanos: EpochNanoseconds,
138+
timezone: TimeZone,
106139
provider: &impl TimeZoneProvider,
107140
) -> TemporalResult<PlainDate> {
108-
let iso = Self::system_datetime_with_hooks_and_provider(timezone, system_hooks, provider)?;
141+
let iso = Self::system_datetime_with_provider(epoch_nanos, timezone, provider)?;
109142
Ok(PlainDate::new_unchecked(iso.date, Calendar::default()))
110143
}
111144

112145
/// Returns the current system time as a `PlainTime` according to an ISO8601 calendar.
113146
///
114-
/// The time zone used to calculate the `PlainTime` will be set to either the
115-
/// `TimeZone` if a value is provided, or according to the system timezone if no
116-
/// value is provided.
117-
pub fn plain_time_iso_with_hooks_and_provider(
118-
timezone: Option<TimeZone>,
119-
system_hooks: &impl SystemHooks,
147+
/// ## Order of operations
148+
///
149+
/// The order of operations for this method requires the `GetSystemTimeZone` call
150+
/// to occur prior to calling system time and resolving the `EpochNanoseconds`
151+
/// value.
152+
///
153+
/// A correct implementation will follow the following steps:
154+
///
155+
/// 1. Resolve user input `TimeZone` with the `SystemTimeZone`.
156+
/// 2. Get the `SystemNanoseconds`
157+
///
158+
/// For an example implementation see [`Self::plain_time_iso`]
159+
pub fn plain_time_iso_with_provider(
160+
epoch_nanos: EpochNanoseconds,
161+
timezone: TimeZone,
120162
provider: &impl TimeZoneProvider,
121163
) -> TemporalResult<PlainTime> {
122-
let iso = Self::system_datetime_with_hooks_and_provider(timezone, system_hooks, provider)?;
164+
let iso = Self::system_datetime_with_provider(epoch_nanos, timezone, provider)?;
123165
Ok(PlainTime::new_unchecked(iso.time))
124166
}
125167
}
126168

127169
#[cfg(all(feature = "tzdb", feature = "sys"))]
128170
#[cfg(test)]
129171
mod tests {
172+
use crate::builtins::core::Now;
130173
use std::thread;
131174
use std::time::Duration as StdDuration;
132175

133-
use crate::builtins::core::Now;
134-
use crate::{options::DifferenceSettings, sys::DefaultSystemHooks, tzdb::FsTzdbProvider};
176+
use crate::options::DifferenceSettings;
135177

136178
#[test]
137179
fn now_datetime_test() {
138-
let provider = &FsTzdbProvider::default();
139-
let system_hooks = DefaultSystemHooks;
140180
let sleep = 2;
141181

142-
let before =
143-
Now::plain_datetime_iso_with_hooks_and_provider(None, &system_hooks, provider).unwrap();
182+
let before = Now::plain_datetime_iso(None).unwrap();
144183
thread::sleep(StdDuration::from_secs(sleep));
145-
let after =
146-
Now::plain_datetime_iso_with_hooks_and_provider(None, &system_hooks, provider).unwrap();
184+
let after = Now::plain_datetime_iso(None).unwrap();
147185

148186
let diff = after.since(&before, DifferenceSettings::default()).unwrap();
149187

Diff for: src/lib.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,12 @@ pub mod options;
5252
pub mod parsers;
5353
pub mod primitive;
5454
pub mod provider;
55-
pub mod sys;
5655

57-
mod epoch_nanoseconds;
56+
#[cfg(feature = "sys")]
57+
pub(crate) mod sys;
5858

5959
mod builtins;
60+
mod epoch_nanoseconds;
6061

6162
#[cfg(feature = "tzdb")]
6263
pub mod tzdb;

Diff for: src/sys.rs

+4-23
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,16 @@
11
use alloc::string::String;
22

3-
use crate::{time::EpochNanoseconds, TemporalResult};
3+
use crate::TemporalResult;
44

5-
#[cfg(feature = "sys")]
65
use crate::TemporalError;
7-
#[cfg(feature = "sys")]
86
use alloc::string::ToString;
9-
#[cfg(feature = "sys")]
107
use web_time::{SystemTime, UNIX_EPOCH};
118

129
// TODO: Need to implement SystemTime handling for non_std.
1310

14-
pub trait SystemHooks {
15-
/// Returns the current system time in `EpochNanoseconds`.
16-
fn get_system_nanoseconds(&self) -> TemporalResult<EpochNanoseconds>;
17-
/// Returns the current system IANA Time Zone Identifier.
18-
fn get_system_time_zone(&self) -> TemporalResult<String>;
19-
}
20-
21-
#[cfg(feature = "sys")]
22-
pub struct DefaultSystemHooks;
23-
24-
#[cfg(feature = "sys")]
25-
impl SystemHooks for DefaultSystemHooks {
26-
fn get_system_nanoseconds(&self) -> TemporalResult<EpochNanoseconds> {
27-
EpochNanoseconds::try_from(get_system_nanoseconds()?)
28-
}
29-
30-
fn get_system_time_zone(&self) -> TemporalResult<String> {
31-
iana_time_zone::get_timezone().map_err(|e| TemporalError::general(e.to_string()))
32-
}
11+
#[inline]
12+
pub(crate) fn get_system_timezone() -> TemporalResult<String> {
13+
iana_time_zone::get_timezone().map_err(|e| TemporalError::general(e.to_string()))
3314
}
3415

3516
/// Returns the system time in nanoseconds.

0 commit comments

Comments
 (0)