Debian Codegraph @

debian/rust-vendor/iana-time-zone/src/lib.rs

raw
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 all(target_arch = "wasm32", target_os = "emscripten"),
50 path = "tz_wasm32_emscripten.rs"
51 )]
52 #[cfg_attr(
53 any(target_os = "freebsd", target_os = "dragonfly"),
54 path = "tz_freebsd.rs"
55 )]
56 #[cfg_attr(
57 any(target_os = "netbsd", target_os = "openbsd"),
58 path = "tz_netbsd.rs"
59 )]
60 #[cfg_attr(
61 any(target_os = "illumos", target_os = "solaris"),
62 path = "tz_illumos.rs"
63 )]
64 #[cfg_attr(target_os = "aix", path = "tz_aix.rs")]
65 #[cfg_attr(target_os = "android", path = "tz_android.rs")]
66 #[cfg_attr(target_os = "haiku", path = "tz_haiku.rs")]
67 #[cfg_attr(
68 all(target_arch = "wasm32", target_os = "wasi"),
69 path = "tz_wasm32_wasi.rs"
70 )]
71 mod platform;
72
73 /// Error types
74 #[derive(Debug)]
75 pub enum GetTimezoneError {
76 /// Failed to parse
77 FailedParsingString,
78 /// Wrapped IO error
79 IoError(std::io::Error),
80 /// Platform-specific error from the operating system
81 OsError,
82 }
83
84 impl std::error::Error for GetTimezoneError {
85 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
86 match self {
87 GetTimezoneError::FailedParsingString => None,
88 GetTimezoneError::IoError(err) => Some(err),
89 GetTimezoneError::OsError => None,
90 }
91 }
92 }
93
94 impl std::fmt::Display for GetTimezoneError {
95 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
96 f.write_str(match self {
97 GetTimezoneError::FailedParsingString => "GetTimezoneError::FailedParsingString",
98 GetTimezoneError::IoError(err) => return err.fmt(f),
99 GetTimezoneError::OsError => "OsError",
100 })
101 }
102 }
103
104 impl From<std::io::Error> for GetTimezoneError {
105 fn from(orig: std::io::Error) -> Self {
106 GetTimezoneError::IoError(orig)
107 }
108 }
109
110 /// Get the current IANA time zone as a string.
111 ///
112 /// See the module-level documentation for a usage example and more details
113 /// about this function.
114 #[inline]
115 pub fn get_timezone() -> Result<String, GetTimezoneError> {
116 platform::get_timezone_inner()
117 }
118
119 #[cfg(test)]
120 mod tests {
121 use super::*;
122
123 #[test]
124 fn get_current() {
125 println!("current: {}", get_timezone().unwrap());
126 }
127 }