iana_time_zone/
lib.rs

1//! get the IANA time zone for the current system
2//!
3//! This small utility crate provides the
4//! [`get_timezone()`](fn.get_timezone.html) function.
5//!
6//! ```rust
7//! // Get the current time zone as a string.
8//! let tz_str = iana_time_zone::get_timezone()?;
9//! println!("The current time zone is: {}", tz_str);
10//! # Ok::<(), iana_time_zone::GetTimezoneError>(())
11//! ```
12//!
13//! The resulting string can be parsed to a
14//! [`chrono-tz::Tz`](https://docs.rs/chrono-tz/latest/chrono_tz/enum.Tz.html)
15//! variant like this:
16//! ```ignore
17//! let tz_str = iana_time_zone::get_timezone()?;
18//! let tz: chrono_tz::Tz = tz_str.parse()?;
19//! ```
20
21#[cfg_attr(target_os = "linux", path = "tz_linux.rs")]
22#[cfg_attr(target_os = "windows", path = "tz_windows.rs")]
23#[cfg_attr(any(target_os = "macos", target_os = "ios"), path = "tz_macos.rs")]
24#[cfg_attr(
25    all(target_arch = "wasm32", not(target_os = "wasi")),
26    path = "tz_wasm32.rs"
27)]
28#[cfg_attr(
29    any(target_os = "freebsd", target_os = "dragonfly"),
30    path = "tz_freebsd.rs"
31)]
32#[cfg_attr(
33    any(target_os = "netbsd", target_os = "openbsd"),
34    path = "tz_netbsd.rs"
35)]
36#[cfg_attr(
37    any(target_os = "illumos", target_os = "solaris"),
38    path = "tz_illumos.rs"
39)]
40#[cfg_attr(target_os = "android", path = "tz_android.rs")]
41mod platform;
42
43/// Error types
44#[derive(Debug)]
45pub enum GetTimezoneError {
46    /// Failed to parse
47    FailedParsingString,
48    /// Wrapped IO error
49    IoError(std::io::Error),
50    /// Platform-specific error from the operating system
51    OsError,
52}
53
54impl std::error::Error for GetTimezoneError {
55    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
56        match self {
57            GetTimezoneError::FailedParsingString => None,
58            GetTimezoneError::IoError(err) => Some(err),
59            GetTimezoneError::OsError => None,
60        }
61    }
62}
63
64impl std::fmt::Display for GetTimezoneError {
65    fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
66        f.write_str(match self {
67            Self::FailedParsingString => "GetTimezoneError::FailedParsingString",
68            Self::IoError(err) => return err.fmt(f),
69            Self::OsError => "OsError",
70        })
71    }
72}
73
74impl std::convert::From<std::io::Error> for GetTimezoneError {
75    fn from(orig: std::io::Error) -> Self {
76        GetTimezoneError::IoError(orig)
77    }
78}
79
80/// Get the current IANA time zone as a string.
81///
82/// See the module-level documentatation for a usage example and more details
83/// about this function.
84#[inline]
85pub fn get_timezone() -> std::result::Result<String, crate::GetTimezoneError> {
86    platform::get_timezone_inner()
87}
88
89#[cfg(test)]
90mod tests {
91    use super::*;
92
93    #[test]
94    fn get_current() {
95        println!("current: {}", get_timezone().unwrap());
96    }
97}