Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Follow up to #61 #62

Merged
merged 2 commits into from Sep 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion CHANGELOG.md
Expand Up @@ -4,9 +4,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.1.50] - 2022-09-23
### Fixed
- Reduce MSRV for Android again ([#62](https://github.com/strawlab/iana-time-zone/pull/62))

## [0.1.49] - 2022-09-22
### Changed
- `once_cell` dependency is not needed ([#61](https://github.com/strawlab/iana-time-zone/pull/58))
- `once_cell` dependency is not needed ([#61](https://github.com/strawlab/iana-time-zone/pull/61))

## [0.1.48] - 2022-09-12
### Changed
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
@@ -1,7 +1,7 @@
[package]
name = "iana-time-zone"
description = "get the IANA time zone for the current system"
version = "0.1.49"
version = "0.1.50"
authors = ["Andrew Straw <strawman@astraw.com>"]
repository = "https://github.com/strawlab/iana-time-zone"
license = "MIT OR Apache-2.0"
Expand Down
26 changes: 14 additions & 12 deletions src/tz_android.rs
Expand Up @@ -3,25 +3,27 @@ use std::sync::Once;

use android_system_properties::AndroidSystemProperties;

static INITALIZED: Once = Once::new();
static mut PROPERTIES: Option<AndroidSystemProperties> = None;
pub(crate) fn get_timezone_inner() -> Result<String, crate::GetTimezoneError> {
// 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
let key = unsafe { CStr::from_bytes_with_nul_unchecked(b"persist.sys.timezone\0") };

// 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") };
get_properties()
.and_then(|properties| properties.get_from_cstr(key))
.ok_or(crate::GetTimezoneError::OsError)
}

fn get_properties() -> Option<&'static AndroidSystemProperties> {
static INITALIZED: Once = Once::new();
static mut PROPERTIES: Option<AndroidSystemProperties> = None;

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)
unsafe { PROPERTIES.as_ref() }
}