Skip to content

Commit b244cba

Browse files
authored
Rename methods on Now and add a test (#243)
This PR adjusts the `Now` core API and adds a test using mock system data. The two goals of this PR are the API and implementing at least one more `Now` test. As far as the API goal, this PR attempts to make the API a bit more explicit and clear. If there's any suggestion for a better method name for the core methods, please feel free to provide feedback. In the future, we should could add more now unit tests using mock data to ensure the behavior is stable and as expected.
1 parent 7306387 commit b244cba

File tree

2 files changed

+82
-28
lines changed

2 files changed

+82
-28
lines changed

src/builtins/compiled/now.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ impl Now {
1919
let timezone = timezone.unwrap_or(TimeZone::IanaIdentifier(sys::get_system_timezone()?));
2020
let system_nanos = sys::get_system_nanoseconds()?;
2121
let epoch_nanos = EpochNanoseconds::try_from(system_nanos)?;
22-
Now::plain_datetime_iso_with_provider(epoch_nanos, timezone, &*provider)
22+
Now::plain_datetime_iso_with_provider_and_system_info(epoch_nanos, timezone, &*provider)
2323
}
2424

2525
/// Returns the current system time as a [`PlainDate`] with an optional
@@ -33,7 +33,7 @@ impl Now {
3333
let timezone = timezone.unwrap_or(TimeZone::IanaIdentifier(sys::get_system_timezone()?));
3434
let system_nanos = sys::get_system_nanoseconds()?;
3535
let epoch_nanos = EpochNanoseconds::try_from(system_nanos)?;
36-
Now::plain_date_iso_with_provider(epoch_nanos, timezone, &*provider)
36+
Now::plain_date_iso_with_provider_and_system_info(epoch_nanos, timezone, &*provider)
3737
}
3838

3939
/// Returns the current system time as a [`PlainTime`] with an optional
@@ -47,6 +47,6 @@ impl Now {
4747
let timezone = timezone.unwrap_or(TimeZone::IanaIdentifier(sys::get_system_timezone()?));
4848
let system_nanos = sys::get_system_nanoseconds()?;
4949
let epoch_nanos = EpochNanoseconds::try_from(system_nanos)?;
50-
Now::plain_time_iso_with_provider(epoch_nanos, timezone, &*provider)
50+
Now::plain_time_iso_with_provider_and_system_info(epoch_nanos, timezone, &*provider)
5151
}
5252
}

src/builtins/core/now.rs

Lines changed: 79 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ impl Now {
3030
/// 1. Resolve user input `TimeZone` with the `SystemTimeZone`.
3131
/// 2. Get the `SystemNanoseconds`
3232
pub(crate) fn system_datetime_with_provider(
33-
epoch_nanoseconds: EpochNanoseconds,
34-
timezone: TimeZone,
33+
system_epoch_nanoseconds: EpochNanoseconds,
34+
system_timezone: TimeZone,
3535
provider: &impl TimeZoneProvider,
3636
) -> TemporalResult<IsoDateTime> {
3737
// 1. If temporalTimeZoneLike is undefined, then
@@ -40,7 +40,7 @@ impl Now {
4040
// a. Let timeZone be ? ToTemporalTimeZoneIdentifier(temporalTimeZoneLike).
4141
// 3. Let epochNs be SystemUTCEpochNanoseconds().
4242
// 4. Return GetISODateTimeFor(timeZone, epochNs).
43-
timezone.get_iso_datetime_for(&Instant::from(epoch_nanoseconds), provider)
43+
system_timezone.get_iso_datetime_for(&Instant::from(system_epoch_nanoseconds), provider)
4444
}
4545

4646
/// Returns the current system time as a `ZonedDateTime` with an ISO8601 calendar.
@@ -61,15 +61,15 @@ impl Now {
6161
///
6262
/// For an example implementation, see `Now::zoneddatetime_iso`; available with
6363
/// the `compiled_data` feature flag.
64-
pub fn zoneddatetime_iso_with_system_values(
65-
epoch_nanos: EpochNanoseconds,
66-
timezone: TimeZone,
64+
pub fn zoneddatetime_iso_with_system_info(
65+
sys_epoch_nanos: EpochNanoseconds,
66+
sys_timezone: TimeZone,
6767
) -> TemporalResult<ZonedDateTime> {
68-
let instant = Instant::from(epoch_nanos);
68+
let instant = Instant::from(sys_epoch_nanos);
6969
Ok(ZonedDateTime::new_unchecked(
7070
instant,
7171
Calendar::default(),
72-
timezone,
72+
sys_timezone,
7373
))
7474
}
7575
}
@@ -101,7 +101,7 @@ impl Now {
101101
timezone.unwrap_or(TimeZone::IanaIdentifier(crate::sys::get_system_timezone()?));
102102
let system_nanos = crate::sys::get_system_nanoseconds()?;
103103
let epoch_nanos = EpochNanoseconds::try_from(system_nanos)?;
104-
Now::zoneddatetime_iso_with_system_values(epoch_nanos, timezone)
104+
Now::zoneddatetime_iso_with_system_info(epoch_nanos, timezone)
105105
}
106106
}
107107

@@ -121,12 +121,12 @@ impl Now {
121121
///
122122
/// For an example implementation, see `Now::plain_datetime_iso`; available with the
123123
/// `compiled_data` feature flag.
124-
pub fn plain_datetime_iso_with_provider(
125-
epoch_nanos: EpochNanoseconds,
126-
timezone: TimeZone,
124+
pub fn plain_datetime_iso_with_provider_and_system_info(
125+
sys_epoch_nanos: EpochNanoseconds,
126+
sys_timezone: TimeZone,
127127
provider: &impl TimeZoneProvider,
128128
) -> TemporalResult<PlainDateTime> {
129-
let iso = Self::system_datetime_with_provider(epoch_nanos, timezone, provider)?;
129+
let iso = Self::system_datetime_with_provider(sys_epoch_nanos, sys_timezone, provider)?;
130130
Ok(PlainDateTime::new_unchecked(iso, Calendar::default()))
131131
}
132132

@@ -145,12 +145,12 @@ impl Now {
145145
///
146146
/// For an example implementation, see `Now::plain_date_iso`; available
147147
/// with the `compiled_data` feature flag.
148-
pub fn plain_date_iso_with_provider(
149-
epoch_nanos: EpochNanoseconds,
150-
timezone: TimeZone,
148+
pub fn plain_date_iso_with_provider_and_system_info(
149+
sys_epoch_nanos: EpochNanoseconds,
150+
sys_timezone: TimeZone,
151151
provider: &impl TimeZoneProvider,
152152
) -> TemporalResult<PlainDate> {
153-
let iso = Self::system_datetime_with_provider(epoch_nanos, timezone, provider)?;
153+
let iso = Self::system_datetime_with_provider(sys_epoch_nanos, sys_timezone, provider)?;
154154
Ok(PlainDate::new_unchecked(iso.date, Calendar::default()))
155155
}
156156

@@ -169,26 +169,80 @@ impl Now {
169169
///
170170
/// For an example implementation, see `Now::plain_time_iso`; available with the
171171
/// `compiled_data` feature flag.
172-
pub fn plain_time_iso_with_provider(
173-
epoch_nanos: EpochNanoseconds,
174-
timezone: TimeZone,
172+
pub fn plain_time_iso_with_provider_and_system_info(
173+
sys_epoch_nanos: EpochNanoseconds,
174+
sys_timezone: TimeZone,
175175
provider: &impl TimeZoneProvider,
176176
) -> TemporalResult<PlainTime> {
177-
let iso = Self::system_datetime_with_provider(epoch_nanos, timezone, provider)?;
177+
let iso = Self::system_datetime_with_provider(sys_epoch_nanos, sys_timezone, provider)?;
178178
Ok(PlainTime::new_unchecked(iso.time))
179179
}
180180
}
181181

182-
#[cfg(all(test, feature = "tzdb", feature = "sys", feature = "compiled_data"))]
182+
#[cfg(test)]
183183
mod tests {
184-
use crate::builtins::core::Now;
185-
use std::thread;
186-
use std::time::Duration as StdDuration;
187184

185+
#[cfg(feature = "tzdb")]
186+
use crate::builtins::core::Now;
187+
#[cfg(feature = "tzdb")]
188188
use crate::options::DifferenceSettings;
189+
#[cfg(feature = "tzdb")]
190+
use crate::time::EpochNanoseconds;
191+
192+
#[cfg(feature = "tzdb")]
193+
#[test]
194+
fn mocked_datetime() {
195+
use crate::{tzdb::FsTzdbProvider, TimeZone};
196+
let provider = FsTzdbProvider::default();
197+
198+
// 2025-03-11T10:47-06:00
199+
const TIME_BASE: u128 = 1_741_751_188_077_363_694;
200+
201+
let cdt = TimeZone::try_from_identifier_str("-05:00").unwrap();
202+
let uschi = TimeZone::try_from_identifier_str("America/Chicago").unwrap();
189203

204+
let base = EpochNanoseconds::try_from(TIME_BASE).unwrap();
205+
let now =
206+
Now::plain_datetime_iso_with_provider_and_system_info(base, cdt.clone(), &provider)
207+
.unwrap();
208+
assert_eq!(now.year(), 2025);
209+
assert_eq!(now.month(), 3);
210+
assert_eq!(now.month_code().as_str(), "M03");
211+
assert_eq!(now.day(), 11);
212+
assert_eq!(now.hour(), 22);
213+
assert_eq!(now.minute(), 46);
214+
assert_eq!(now.second(), 28);
215+
assert_eq!(now.millisecond(), 77);
216+
assert_eq!(now.microsecond(), 363);
217+
assert_eq!(now.nanosecond(), 694);
218+
219+
let now_iana =
220+
Now::plain_datetime_iso_with_provider_and_system_info(base, uschi.clone(), &provider)
221+
.unwrap();
222+
assert_eq!(now, now_iana);
223+
224+
let plus_5_secs = TIME_BASE + (5 * 1_000_000_000);
225+
let plus_5_epoch = EpochNanoseconds::try_from(plus_5_secs).unwrap();
226+
let now_plus_5 =
227+
Now::plain_datetime_iso_with_provider_and_system_info(plus_5_epoch, cdt, &provider)
228+
.unwrap();
229+
assert_eq!(now_plus_5.second(), 33);
230+
231+
let duration = now
232+
.until(&now_plus_5, DifferenceSettings::default())
233+
.unwrap();
234+
assert!(duration.hours().is_zero());
235+
assert!(duration.minutes().is_zero());
236+
assert_eq!(duration.seconds().as_inner(), 5.0);
237+
assert!(duration.milliseconds().is_zero());
238+
}
239+
240+
#[cfg(all(feature = "tzdb", feature = "sys", feature = "compiled_data"))]
190241
#[test]
191242
fn now_datetime_test() {
243+
use std::thread;
244+
use std::time::Duration as StdDuration;
245+
192246
let sleep = 2;
193247

194248
let before = Now::plain_datetime_iso(None).unwrap();

0 commit comments

Comments
 (0)