forked from strawlab/iana-time-zone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tz_android.rs
27 lines (21 loc) · 1.12 KB
/
tz_android.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
use std::ffi::CStr;
use std::sync::Once;
use android_system_properties::AndroidSystemProperties;
static INITALIZED: Once = Once::new();
static mut PROPERTIES: Option<AndroidSystemProperties> = None;
// From https://android.googlesource.com/platform/ndk/+/android-4.2.2_r1.2/docs/system/libc/OVERVIEW.html
// The system property named 'persist.sys.timezone' contains the name of the current timezone.
// SAFETY: the key is NUL-terminated and there are no other NULs
const KEY: &CStr = unsafe { CStr::from_bytes_with_nul_unchecked(b"persist.sys.timezone\0") };
pub(crate) fn get_timezone_inner() -> Result<String, crate::GetTimezoneError> {
INITALIZED.call_once(|| {
let properties = AndroidSystemProperties::new();
// SAFETY: `INITALIZED` is synchronizing. The variable is only assigned to once.
unsafe { PROPERTIES = Some(properties) };
});
// SAFETY: `INITALIZED` is synchronizing. The variable is only assigned to once.
let properties = unsafe { PROPERTIES.as_ref() };
properties
.and_then(|properties| properties.get_from_cstr(KEY))
.ok_or(crate::GetTimezoneError::OsError)
}