While temporal_rs can be used in native Rust, it is designed for ECMAScript implementations. By default, it does not embed time zone data or access the host environment to remain lightweight and flexible for interpreters.
To provide these capabilities, the library uses two main traits:
HostHooks: For accessing the host system.TimeZoneProvider: For sourcing time zone data.
Power User Pattern:
- Any API with the suffix
_with_provider allows you to supply a custom or specific TimeZoneProvider. - APIs without the
_with_provider suffix will automatically use the default provider (if enabled via feature flags).
If you are working without default features (e.g., in a constrained environment), you must manually provide a TimeZoneProvider to methods like time_zone_with_provider.
use temporal_rs::{Instant, now::Now, host::EmptyHostSystem};
use timezone_provider::tzif::CompiledTzdbProvider;
let provider = CompiledTzdbProvider::default();
// The empty host system is a system implementation HostHooks that always
// returns the UNIX_EPOCH and the "+00:00" time zone.
let now = Now::new(EmptyHostSystem);
let time_zone = now.time_zone_with_provider(&provider).unwrap();
assert_eq!(time_zone.identifier_with_provider(&provider).unwrap(), "+00:00");
let now = Now::new(EmptyHostSystem);
assert_eq!(now.instant(), Instant::try_new(0));