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 | //! ```rust |
29 | //! let tz_str = iana_time_zone::get_timezone()?; |
30 | //! let tz: chrono_tz::Tz = tz_str.parse()?; |
31 | //! # Ok::<(), Box<dyn std::error::Error>>(()) |
32 | //! ``` |
33 | |
34 | #[allow (dead_code)] |
35 | mod 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" )] |
63 | mod platform; |
64 | |
65 | /// Error types |
66 | #[derive (Debug)] |
67 | pub enum GetTimezoneError { |
68 | /// Failed to parse |
69 | FailedParsingString, |
70 | /// Wrapped IO error |
71 | IoError(std::io::Error), |
72 | /// Platform-specific error from the operating system |
73 | OsError, |
74 | } |
75 | |
76 | impl 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: &Error) => Some(err), |
81 | GetTimezoneError::OsError => None, |
82 | } |
83 | } |
84 | } |
85 | |
86 | impl std::fmt::Display for GetTimezoneError { |
87 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { |
88 | f.write_str(data:match self { |
89 | GetTimezoneError::FailedParsingString => "GetTimezoneError::FailedParsingString" , |
90 | GetTimezoneError::IoError(err: &Error) => return err.fmt(f), |
91 | GetTimezoneError::OsError => "OsError" , |
92 | }) |
93 | } |
94 | } |
95 | |
96 | impl From<std::io::Error> for GetTimezoneError { |
97 | fn from(orig: std::io::Error) -> Self { |
98 | GetTimezoneError::IoError(orig) |
99 | } |
100 | } |
101 | |
102 | /// Get the current IANA time zone as a string. |
103 | /// |
104 | /// See the module-level documentation for a usage example and more details |
105 | /// about this function. |
106 | #[inline ] |
107 | pub fn get_timezone() -> Result<String, GetTimezoneError> { |
108 | platform::get_timezone_inner() |
109 | } |
110 | |
111 | #[cfg (test)] |
112 | mod tests { |
113 | use super::*; |
114 | |
115 | #[test ] |
116 | fn get_current() { |
117 | println!("current: {}" , get_timezone().unwrap()); |
118 | } |
119 | } |
120 | |