Skip to content

Commit

Permalink
Auto merge of #395 - bryanburgers:timezone-name, r=jdm
Browse files Browse the repository at this point in the history
Add `CFTimeZone::name`

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.
  • Loading branch information
bors-servo committed Jun 29, 2020
2 parents 749c085 + a4e2983 commit 3754007
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
2 changes: 2 additions & 0 deletions core-foundation-sys/src/timezone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -24,4 +25,5 @@ extern {
pub fn CFTimeZoneGetSecondsFromGMT(tz: CFTimeZoneRef, time: CFAbsoluteTime) -> CFTimeInterval;

pub fn CFTimeZoneGetTypeID() -> CFTypeID;
pub fn CFTimeZoneGetName(tz: CFTimeZoneRef) -> CFStringRef;
}
9 changes: 9 additions & 0 deletions core-foundation/src/timezone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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)]
Expand Down

0 comments on commit 3754007

Please sign in to comment.