Skip to content

Commit

Permalink
Merge pull request #62 from Kijewski/issue-61-follow-up
Browse files Browse the repository at this point in the history
Follow up to #61
  • Loading branch information
astraw committed Sep 23, 2022
2 parents fc15df6 + ecacfc0 commit 14dfc44
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
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() }
}

0 comments on commit 14dfc44

Please sign in to comment.