| 2cd40ea1 Andy McCall ↶ |
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 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 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_linux.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 |
|
| 74 |
#[derive(Debug)] |
| 75 |
pub enum GetTimezoneError { |
| 76 |
|
| 77 |
FailedParsingString, |
| 78 |
|
| 79 |
IoError(std::io::Error), |
| 80 |
|
| 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 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 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 |
} |