From a4e29832190de6d58bd59ccd52289476689cd995 Mon Sep 17 00:00:00 2001 From: Bryan Burgers Date: Mon, 29 Jun 2020 07:10:15 -0500 Subject: [PATCH] Add `CFTimeZone::name` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add a `name` method to `CFTimeZone` to get the time zone ID (e.g., "America/Los_Angeles") of the current time zone. Core Foundation calls this "name" instead of "id" in its method signatures, but acknowledges it in the closely-related NSTimeZone related docs. Since most of the other methods in this library match closely with CF naming, also use `name` here. > Time zone database entries such as “America/Los_Angeles” are IDs, not > names. An example of a time zone name is “Pacific Daylight Time”. > Although many NSTimeZone symbols include the word “name”, they > actually refer to IDs. --- core-foundation-sys/src/timezone.rs | 2 ++ core-foundation/src/timezone.rs | 9 +++++++++ 2 files changed, 11 insertions(+) diff --git a/core-foundation-sys/src/timezone.rs b/core-foundation-sys/src/timezone.rs index 376cfdcde..0b279db18 100644 --- a/core-foundation-sys/src/timezone.rs +++ b/core-foundation-sys/src/timezone.rs @@ -11,6 +11,7 @@ use std::os::raw::c_void; use base::{CFAllocatorRef, CFTypeID}; use date::{CFTimeInterval, CFAbsoluteTime}; +use string::CFStringRef; #[repr(C)] pub struct __CFTimeZone(c_void); @@ -24,4 +25,5 @@ extern { pub fn CFTimeZoneGetSecondsFromGMT(tz: CFTimeZoneRef, time: CFAbsoluteTime) -> CFTimeInterval; pub fn CFTimeZoneGetTypeID() -> CFTypeID; + pub fn CFTimeZoneGetName(tz: CFTimeZoneRef) -> CFStringRef; } diff --git a/core-foundation/src/timezone.rs b/core-foundation/src/timezone.rs index 66aadb77a..a8bb2ed1d 100644 --- a/core-foundation/src/timezone.rs +++ b/core-foundation/src/timezone.rs @@ -14,6 +14,7 @@ use core_foundation_sys::base::kCFAllocatorDefault; use base::TCFType; use date::{CFDate, CFTimeInterval}; +use string::CFString; #[cfg(feature = "with-chrono")] use chrono::{FixedOffset, NaiveDateTime}; @@ -68,6 +69,14 @@ impl CFTimeZone { pub fn from_offset(offset: FixedOffset) -> CFTimeZone { CFTimeZone::new(offset.local_minus_utc() as f64) } + + /// The timezone database ID that identifies the time zone. E.g. "America/Los_Angeles" or + /// "Europe/Paris". + pub fn name(&self) -> CFString { + unsafe { + CFString::wrap_under_get_rule(CFTimeZoneGetName(self.0)) + } + } } #[cfg(test)]