Skip to main content

reifydb_value/factory/
time.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2026 ReifyDB
3
4use crate::value::{datetime::DateTime, duration::Duration};
5
6pub fn millis(value: u64) -> Duration {
7	Duration::from_milliseconds_const(value as i64)
8}
9
10pub fn secs(value: u64) -> Duration {
11	Duration::from_seconds_const(value as i64)
12}
13
14pub fn at_millis(value: u64) -> DateTime {
15	DateTime::from_millis(value)
16}
17
18pub fn at_nanos(value: u64) -> DateTime {
19	DateTime::from_nanos(value)
20}
21
22#[cfg(test)]
23mod tests {
24	use super::*;
25
26	#[test]
27	fn the_two_datetime_factories_disagree_by_exactly_a_million() {
28		// These replace a workspace of hand-rolled `fn at(u64) -> DateTime` helpers that silently
29		// disagreed on their unit: five read millis, three read nanos, all named `at`. Pinning the
30		// ratio is what makes a future edit that "simplifies" one of them into the other fail here
31		// instead of in whichever suite happened to depend on the offset.
32		assert_eq!(at_millis(1), at_nanos(1_000_000));
33		assert_ne!(at_millis(1), at_nanos(1));
34	}
35
36	#[test]
37	fn the_duration_factories_carry_their_named_unit() {
38		// A fixture that windows on `millis(60)` and one that ages on `secs(60)` must not be the
39		// same span; conflating them is how a retention test passes while retaining nothing.
40		assert_eq!(millis(1_000).milliseconds().expect("millis"), secs(1).milliseconds().expect("millis"));
41		assert_eq!(millis(60).milliseconds().expect("millis"), 60);
42		assert_eq!(secs(60).milliseconds().expect("millis"), 60_000);
43	}
44
45	#[test]
46	fn the_epoch_is_the_zero_of_both_datetime_factories() {
47		// An unstamped row reads as the epoch rather than as absent, so fixtures compare against
48		// this value to tell "never stamped" from "stamped at zero". Both factories must agree on
49		// where that point is.
50		assert_eq!(at_millis(0), at_nanos(0));
51	}
52}