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