Skip to content

Commit

Permalink
Add timezone. (#327)
Browse files Browse the repository at this point in the history
* Add timezone.

* Move implementation to cap-time-ext.

The cap-std crate is intended to focus on features which are provided by
Rust's `std`.
  • Loading branch information
cdmurph32 authored Nov 1, 2023
1 parent b8e430b commit 3663202
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cap-time-ext/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ repository = "https://github.com/bytecodealliance/cap-std"
edition = "2021"

[dependencies]
ambient-authority = "0.0.2"
cap-primitives = { path = "../cap-primitives", version = "^2.0.0" }
cap-std = { path = "../cap-std", optional = true, version = "^2.0.0" }
iana-time-zone = "0.1.57"

[target.'cfg(not(windows))'.dependencies]
rustix = { version = "0.38.0", features = ["time"] }
Expand Down
2 changes: 2 additions & 0 deletions cap-time-ext/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

mod monotonic_clock;
mod system_clock;
mod timezone;

pub use monotonic_clock::MonotonicClockExt;
pub use system_clock::SystemClockExt;
pub use timezone::Timezone;
29 changes: 29 additions & 0 deletions cap-time-ext/src/timezone.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use ambient_authority::AmbientAuthority;
use iana_time_zone::get_timezone;

/// A reference to a timezone resource.
pub struct Timezone(());

#[derive(Debug)]
pub struct TimezoneError(String);

impl Timezone {
/// Constructs a new instance of `Self`.
///
/// # Ambient Authority
///
/// This uses ambient authority to accesses clocks.
#[inline]
pub const fn new(ambient_authority: AmbientAuthority) -> Self {
let _ = ambient_authority;
Self(())
}

/// Returns the combined date and time with timezone.
///
/// Converts NaiveTime to DateTime
#[inline]
pub fn timezone_name(&self) -> Result<String, TimezoneError> {
get_timezone().map_err(|e| TimezoneError(e.to_string()))
}
}

0 comments on commit 3663202

Please sign in to comment.