iana_time_zone/
lib.rs
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#[allow(dead_code)]
35mod ffi_utils;
36
37#[cfg_attr(
38 any(all(target_os = "linux", not(target_env = "ohos")), target_os = "hurd"),
39 path = "tz_linux.rs"
40)]
41#[cfg_attr(all(target_os = "linux", target_env = "ohos"), path = "tz_ohos.rs")]
42#[cfg_attr(target_os = "windows", path = "tz_windows.rs")]
43#[cfg_attr(target_vendor = "apple", path = "tz_darwin.rs")]
44#[cfg_attr(
45 all(target_arch = "wasm32", target_os = "unknown"),
46 path = "tz_wasm32_unknown.rs"
47)]
48#[cfg_attr(
49 any(target_os = "freebsd", target_os = "dragonfly"),
50 path = "tz_freebsd.rs"
51)]
52#[cfg_attr(
53 any(target_os = "netbsd", target_os = "openbsd"),
54 path = "tz_netbsd.rs"
55)]
56#[cfg_attr(
57 any(target_os = "illumos", target_os = "solaris"),
58 path = "tz_illumos.rs"
59)]
60#[cfg_attr(target_os = "aix", path = "tz_aix.rs")]
61#[cfg_attr(target_os = "android", path = "tz_android.rs")]
62#[cfg_attr(target_os = "haiku", path = "tz_haiku.rs")]
63mod platform;
64
65#[derive(Debug)]
67pub enum GetTimezoneError {
68 FailedParsingString,
70 IoError(std::io::Error),
72 OsError,
74}
75
76impl std::error::Error for GetTimezoneError {
77 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
78 match self {
79 GetTimezoneError::FailedParsingString => None,
80 GetTimezoneError::IoError(err) => Some(err),
81 GetTimezoneError::OsError => None,
82 }
83 }
84}
85
86impl std::fmt::Display for GetTimezoneError {
87 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
88 f.write_str(match self {
89 GetTimezoneError::FailedParsingString => "GetTimezoneError::FailedParsingString",
90 GetTimezoneError::IoError(err) => return err.fmt(f),
91 GetTimezoneError::OsError => "OsError",
92 })
93 }
94}
95
96impl From<std::io::Error> for GetTimezoneError {
97 fn from(orig: std::io::Error) -> Self {
98 GetTimezoneError::IoError(orig)
99 }
100}
101
102#[inline]
107pub fn get_timezone() -> Result<String, GetTimezoneError> {
108 platform::get_timezone_inner()
109}
110
111#[cfg(test)]
112mod tests {
113 use super::*;
114
115 #[test]
116 fn get_current() {
117 println!("current: {}", get_timezone().unwrap());
118 }
119}