Skip to content

Commit 3663202

Browse files
authored
Add timezone. (#327)
* 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`.
1 parent b8e430b commit 3663202

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

cap-time-ext/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ repository = "https://github.com/bytecodealliance/cap-std"
1313
edition = "2021"
1414

1515
[dependencies]
16+
ambient-authority = "0.0.2"
1617
cap-primitives = { path = "../cap-primitives", version = "^2.0.0" }
1718
cap-std = { path = "../cap-std", optional = true, version = "^2.0.0" }
19+
iana-time-zone = "0.1.57"
1820

1921
[target.'cfg(not(windows))'.dependencies]
2022
rustix = { version = "0.38.0", features = ["time"] }

cap-time-ext/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
mod monotonic_clock;
1313
mod system_clock;
14+
mod timezone;
1415

1516
pub use monotonic_clock::MonotonicClockExt;
1617
pub use system_clock::SystemClockExt;
18+
pub use timezone::Timezone;

cap-time-ext/src/timezone.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use ambient_authority::AmbientAuthority;
2+
use iana_time_zone::get_timezone;
3+
4+
/// A reference to a timezone resource.
5+
pub struct Timezone(());
6+
7+
#[derive(Debug)]
8+
pub struct TimezoneError(String);
9+
10+
impl Timezone {
11+
/// Constructs a new instance of `Self`.
12+
///
13+
/// # Ambient Authority
14+
///
15+
/// This uses ambient authority to accesses clocks.
16+
#[inline]
17+
pub const fn new(ambient_authority: AmbientAuthority) -> Self {
18+
let _ = ambient_authority;
19+
Self(())
20+
}
21+
22+
/// Returns the combined date and time with timezone.
23+
///
24+
/// Converts NaiveTime to DateTime
25+
#[inline]
26+
pub fn timezone_name(&self) -> Result<String, TimezoneError> {
27+
get_timezone().map_err(|e| TimezoneError(e.to_string()))
28+
}
29+
}

0 commit comments

Comments
 (0)