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