| 1 |
pub(crate) fn get_timezone_inner() -> Result<String, crate::GetTimezoneError> { |
| 2 |
get_timezone().ok_or(crate::GetTimezoneError::OsError) |
| 3 |
} |
| 4 |
|
| 5 |
#[inline] |
| 6 |
fn get_timezone() -> Option<String> { |
| 7 |
// The longest name in the IANA time zone database is 25 ASCII characters long. |
| 8 |
const MAX_LEN: usize = 32; |
| 9 |
let mut buf = [0; MAX_LEN]; |
| 10 |
|
| 11 |
// Get system time zone, and borrow its name. |
| 12 |
let tz = system_time_zone::SystemTimeZone::new()?; |
| 13 |
let name = tz.name()?; |
| 14 |
|
| 15 |
// If the name is encoded in UTF-8, copy it directly. |
| 16 |
let name = if let Some(name) = name.as_utf8() { |
| 17 |
name |
| 18 |
} else { |
| 19 |
// Otherwise convert the name to UTF-8. |
| 20 |
name.to_utf8(&mut buf)? |
| 21 |
}; |
| 22 |
|
| 23 |
if name.is_empty() || name.len() >= MAX_LEN { |
| 24 |
// The name should not be empty, or excessively long. |
| 25 |
None |
| 26 |
} else { |
| 27 |
Some(name.to_owned()) |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
mod system_time_zone { |
| 32 |
//! create a safe wrapper around `CFTimeZoneRef` |
| 33 |
|
| 34 |
use core_foundation_sys::base::{CFRelease, CFTypeRef}; |
| 35 |
use core_foundation_sys::timezone::{CFTimeZoneCopySystem, CFTimeZoneGetName, CFTimeZoneRef}; |
| 36 |
|
| 37 |
pub(crate) struct SystemTimeZone(CFTimeZoneRef); |
| 38 |
|
| 39 |
impl Drop for SystemTimeZone { |
| 40 |
fn drop(&mut self) { |
| 41 |
// SAFETY: `SystemTimeZone` is only ever created with a valid `CFTimeZoneRef`. |
| 42 |
unsafe { CFRelease(self.0 as CFTypeRef) }; |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
impl SystemTimeZone { |
| 47 |
pub(crate) fn new() -> Option<Self> { |
| 48 |
// SAFETY: No invariants to uphold. We'll release the pointer when we don't need it anymore. |
| 49 |
let v: CFTimeZoneRef = unsafe { CFTimeZoneCopySystem() }; |
| 50 |
if v.is_null() { |
| 51 |
None |
| 52 |
} else { |
| 53 |
Some(SystemTimeZone(v)) |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
/// Get the time zone name as a [super::string_ref::StringRef]. |
| 58 |
/// |
| 59 |
/// The lifetime of the `StringRef` is bound to our lifetime. Mutable |
| 60 |
/// access is also prevented by taking a reference to `self`. |
| 61 |
pub(crate) fn name(&self) -> Option<super::string_ref::StringRef<'_, Self>> { |
| 62 |
// SAFETY: `SystemTimeZone` is only ever created with a valid `CFTimeZoneRef`. |
| 63 |
let string = unsafe { CFTimeZoneGetName(self.0) }; |
| 64 |
if string.is_null() { |
| 65 |
None |
| 66 |
} else { |
| 67 |
// SAFETY: here we ensure that `string` is a valid pointer. |
| 68 |
Some(unsafe { super::string_ref::StringRef::new(string, self) }) |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
mod string_ref { |
| 75 |
//! create safe wrapper around `CFStringRef` |
| 76 |
|
| 77 |
use std::convert::TryInto; |
| 78 |
|
| 79 |
use core_foundation_sys::base::{Boolean, CFRange}; |
| 80 |
use core_foundation_sys::string::{ |
| 81 |
kCFStringEncodingUTF8, CFStringGetBytes, CFStringGetCStringPtr, CFStringGetLength, |
| 82 |
CFStringRef, |
| 83 |
}; |
| 84 |
|
| 85 |
pub(crate) struct StringRef<'a, T> { |
| 86 |
string: CFStringRef, |
| 87 |
// We exclude mutable access to the parent by taking a reference to the |
| 88 |
// parent (rather than, for example, just using a marker to enforce the |
| 89 |
// parent's lifetime). |
| 90 |
_parent: &'a T, |
| 91 |
} |
| 92 |
|
| 93 |
impl<'a, T> StringRef<'a, T> { |
| 94 |
// SAFETY: `StringRef` must be valid pointer |
| 95 |
pub(crate) unsafe fn new(string: CFStringRef, _parent: &'a T) -> Self { |
| 96 |
Self { string, _parent } |
| 97 |
} |
| 98 |
|
| 99 |
pub(crate) fn as_utf8(&self) -> Option<&'a str> { |
| 100 |
// SAFETY: `StringRef` is only ever created with a valid `CFStringRef`. |
| 101 |
let v = unsafe { CFStringGetCStringPtr(self.string, kCFStringEncodingUTF8) }; |
| 102 |
if !v.is_null() { |
| 103 |
// SAFETY: `CFStringGetCStringPtr()` returns NUL-terminated strings. |
| 104 |
let v = unsafe { std::ffi::CStr::from_ptr(v) }; |
| 105 |
if let Ok(v) = v.to_str() { |
| 106 |
return Some(v); |
| 107 |
} |
| 108 |
} |
| 109 |
None |
| 110 |
} |
| 111 |
|
| 112 |
pub(crate) fn to_utf8<'b>(&self, buf: &'b mut [u8]) -> Option<&'b str> { |
| 113 |
// SAFETY: `StringRef` is only ever created with a valid `CFStringRef`. |
| 114 |
let length = unsafe { CFStringGetLength(self.string) }; |
| 115 |
|
| 116 |
let mut buf_bytes = 0; |
| 117 |
let range = CFRange { |
| 118 |
location: 0, |
| 119 |
length, |
| 120 |
}; |
| 121 |
|
| 122 |
let converted_bytes = unsafe { |
| 123 |
// SAFETY: `StringRef` is only ever created with a valid `CFStringRef`. |
| 124 |
CFStringGetBytes( |
| 125 |
self.string, |
| 126 |
range, |
| 127 |
kCFStringEncodingUTF8, |
| 128 |
b'\0', |
| 129 |
false as Boolean, |
| 130 |
buf.as_mut_ptr(), |
| 131 |
buf.len() as isize, |
| 132 |
&mut buf_bytes, |
| 133 |
) |
| 134 |
}; |
| 135 |
if converted_bytes != length { |
| 136 |
return None; |
| 137 |
} |
| 138 |
|
| 139 |
let len = buf_bytes.try_into().ok()?; |
| 140 |
let s = buf.get(..len)?; |
| 141 |
std::str::from_utf8(s).ok() |
| 142 |
} |
| 143 |
} |
| 144 |
} |