| 1 |
use crate::ffi_utils::buffer::{tzname_buf, MAX_LEN}; |
| 2 |
|
| 3 |
pub(crate) fn get_timezone_inner() -> Result<String, crate::GetTimezoneError> { |
| 4 |
get_timezone().ok_or(crate::GetTimezoneError::OsError) |
| 5 |
} |
| 6 |
|
| 7 |
#[inline] |
| 8 |
fn get_timezone() -> Option<String> { |
| 9 |
let mut buf = tzname_buf(); |
| 10 |
// Get system time zone, and borrow its name. |
| 11 |
let tz = system_time_zone::SystemTimeZone::new()?; |
| 12 |
let name = tz.name()?; |
| 13 |
|
| 14 |
// If the name is encoded in UTF-8, copy it directly. |
| 15 |
let name = if let Some(name) = name.as_utf8() { |
| 16 |
name |
| 17 |
} else { |
| 18 |
// Otherwise convert the name to UTF-8. |
| 19 |
name.to_utf8(&mut buf)? |
| 20 |
}; |
| 21 |
|
| 22 |
if name.is_empty() || name.len() > MAX_LEN { |
| 23 |
// The name should not be empty, or excessively long. |
| 24 |
None |
| 25 |
} else { |
| 26 |
Some(name.to_owned()) |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
mod system_time_zone { |
| 31 |
//! create a safe wrapper around `CFTimeZoneRef` |
| 32 |
|
| 33 |
use core_foundation_sys::base::{CFRelease, CFTypeRef}; |
| 34 |
use core_foundation_sys::timezone::{ |
| 35 |
CFTimeZoneCopySystem, CFTimeZoneGetName, CFTimeZoneRef, CFTimeZoneResetSystem, |
| 36 |
}; |
| 37 |
|
| 38 |
pub(crate) struct SystemTimeZone(CFTimeZoneRef); |
| 39 |
|
| 40 |
impl Drop for SystemTimeZone { |
| 41 |
fn drop(&mut self) { |
| 42 |
// SAFETY: `SystemTimeZone` is only ever created with a valid `CFTimeZoneRef`. |
| 43 |
unsafe { CFRelease(self.0 as CFTypeRef) }; |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
impl SystemTimeZone { |
| 48 |
/// Creates a new `SystemTimeZone` by querying the current Darwin system |
| 49 |
/// timezone. |
| 50 |
/// |
| 51 |
/// This function implicitly calls `CFTimeZoneResetSystem` to invalidate |
| 52 |
/// the cached timezone and ensure we always retrieve the current system |
| 53 |
/// timezone. |
| 54 |
/// |
| 55 |
/// Due to CoreFoundation's internal caching mechanism, subsequent calls |
| 56 |
/// to `CFTimeZoneCopySystem` do not reflect system timezone changes |
| 57 |
/// made while the process is running. Thus, we explicitly call |
| 58 |
/// `CFTimeZoneResetSystem` first to invalidate the cached value and |
| 59 |
/// ensure we always retrieve the current system timezone. |
| 60 |
pub(crate) fn new() -> Option<Self> { |
| 61 |
// SAFETY: |
| 62 |
// - Both `CFTimeZoneResetSystem` and `CFTimeZoneCopySystem` are FFI |
| 63 |
// calls to macOS CoreFoundation. |
| 64 |
// - `CFTimeZoneResetSystem` safely invalidates the cached timezone |
| 65 |
// without any external invariants. |
| 66 |
// - The pointer returned by `CFTimeZoneCopySystem` is managed and |
| 67 |
// released properly within `Drop`. |
| 68 |
let v: CFTimeZoneRef = unsafe { |
| 69 |
// First, clear the potentially cached timezone. This call will |
| 70 |
// take the global lock on the timezone data. |
| 71 |
// |
| 72 |
// See <https://github.com/strawlab/iana-time-zone/issues/145#issuecomment-2745934606> |
| 73 |
// for context on why we reset the timezone here. |
| 74 |
CFTimeZoneResetSystem(); |
| 75 |
|
| 76 |
// Fetch the current value. This will likely allocate. This call |
| 77 |
// will again take the global lock on the timezone data. |
| 78 |
CFTimeZoneCopySystem() |
| 79 |
}; |
| 80 |
if v.is_null() { |
| 81 |
None |
| 82 |
} else { |
| 83 |
Some(SystemTimeZone(v)) |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
/// Get the time zone name as a [`StringRef`]. |
| 88 |
/// |
| 89 |
/// The lifetime of the `StringRef` is bound to our lifetime. Mutable |
| 90 |
/// access is also prevented by taking a reference to `self`. |
| 91 |
/// |
| 92 |
/// [`StringRef`]: super::string_ref::StringRef |
| 93 |
pub(crate) fn name(&self) -> Option<super::string_ref::StringRef<'_, Self>> { |
| 94 |
// SAFETY: `SystemTimeZone` is only ever created with a valid `CFTimeZoneRef`. |
| 95 |
let string = unsafe { CFTimeZoneGetName(self.0) }; |
| 96 |
if string.is_null() { |
| 97 |
None |
| 98 |
} else { |
| 99 |
// SAFETY: here we ensure that `string` is a valid pointer. |
| 100 |
Some(unsafe { super::string_ref::StringRef::new(string, self) }) |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
mod string_ref { |
| 107 |
//! create safe wrapper around `CFStringRef` |
| 108 |
|
| 109 |
use core::convert::TryInto; |
| 110 |
|
| 111 |
use core_foundation_sys::base::{Boolean, CFRange}; |
| 112 |
use core_foundation_sys::string::{ |
| 113 |
kCFStringEncodingUTF8, CFStringGetBytes, CFStringGetCStringPtr, CFStringGetLength, |
| 114 |
CFStringRef, |
| 115 |
}; |
| 116 |
|
| 117 |
pub(crate) struct StringRef<'a, T> { |
| 118 |
string: CFStringRef, |
| 119 |
// We exclude mutable access to the parent by taking a reference to the |
| 120 |
// parent (rather than, for example, just using a marker to enforce the |
| 121 |
// parent's lifetime). |
| 122 |
_parent: &'a T, |
| 123 |
} |
| 124 |
|
| 125 |
impl<'a, T> StringRef<'a, T> { |
| 126 |
// SAFETY: `string` must be valid pointer |
| 127 |
pub(crate) unsafe fn new(string: CFStringRef, _parent: &'a T) -> Self { |
| 128 |
Self { string, _parent } |
| 129 |
} |
| 130 |
|
| 131 |
pub(crate) fn as_utf8(&self) -> Option<&'a str> { |
| 132 |
// SAFETY: `StringRef` is only ever created with a valid `CFStringRef`. |
| 133 |
let v = unsafe { CFStringGetCStringPtr(self.string, kCFStringEncodingUTF8) }; |
| 134 |
if !v.is_null() { |
| 135 |
// SAFETY: `CFStringGetCStringPtr()` returns NUL-terminated |
| 136 |
// strings and will return NULL if the internal representation |
| 137 |
// of the `CFString`` is not compatible with the requested |
| 138 |
// encoding. |
| 139 |
let v = unsafe { std::ffi::CStr::from_ptr(v) }; |
| 140 |
if let Ok(v) = v.to_str() { |
| 141 |
return Some(v); |
| 142 |
} |
| 143 |
} |
| 144 |
None |
| 145 |
} |
| 146 |
|
| 147 |
pub(crate) fn to_utf8<'b>(&self, buf: &'b mut [u8]) -> Option<&'b str> { |
| 148 |
// SAFETY: `StringRef` is only ever created with a valid `CFStringRef`. |
| 149 |
let length = unsafe { CFStringGetLength(self.string) }; |
| 150 |
|
| 151 |
let mut buf_bytes = 0; |
| 152 |
let range = CFRange { |
| 153 |
location: 0, |
| 154 |
length, |
| 155 |
}; |
| 156 |
|
| 157 |
// SAFETY: `StringRef` is only ever created with a valid `CFStringRef`. |
| 158 |
let converted_bytes = unsafe { |
| 159 |
CFStringGetBytes( |
| 160 |
self.string, |
| 161 |
range, |
| 162 |
kCFStringEncodingUTF8, |
| 163 |
b'\0', |
| 164 |
false as Boolean, |
| 165 |
buf.as_mut_ptr(), |
| 166 |
buf.len() as isize, |
| 167 |
&mut buf_bytes, |
| 168 |
) |
| 169 |
}; |
| 170 |
if converted_bytes != length { |
| 171 |
return None; |
| 172 |
} |
| 173 |
|
| 174 |
let len = buf_bytes.try_into().ok()?; |
| 175 |
let s = buf.get(..len)?; |
| 176 |
std::str::from_utf8(s).ok() |
| 177 |
} |
| 178 |
} |
| 179 |
} |