core_foundation/
timezone.rs

1// Copyright 2013 The Servo Project Developers. See the COPYRIGHT
2// file at the top-level directory of this distribution.
3//
4// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7// option. This file may not be copied, modified, or distributed
8// except according to those terms.
9
10//! Core Foundation time zone objects.
11
12use core_foundation_sys::base::kCFAllocatorDefault;
13pub use core_foundation_sys::timezone::*;
14
15use crate::base::TCFType;
16use crate::date::{CFDate, CFTimeInterval};
17use crate::string::CFString;
18
19declare_TCFType! {
20    /// A time zone.
21    CFTimeZone, CFTimeZoneRef
22}
23impl_TCFType!(CFTimeZone, CFTimeZoneRef, CFTimeZoneGetTypeID);
24impl_CFTypeDescription!(CFTimeZone);
25
26impl Default for CFTimeZone {
27    fn default() -> CFTimeZone {
28        unsafe {
29            let tz_ref = CFTimeZoneCopyDefault();
30            TCFType::wrap_under_create_rule(tz_ref)
31        }
32    }
33}
34
35impl CFTimeZone {
36    #[inline]
37    pub fn new(interval: CFTimeInterval) -> CFTimeZone {
38        unsafe {
39            let tz_ref = CFTimeZoneCreateWithTimeIntervalFromGMT(kCFAllocatorDefault, interval);
40            TCFType::wrap_under_create_rule(tz_ref)
41        }
42    }
43
44    #[inline]
45    pub fn system() -> CFTimeZone {
46        unsafe {
47            let tz_ref = CFTimeZoneCopySystem();
48            TCFType::wrap_under_create_rule(tz_ref)
49        }
50    }
51
52    pub fn seconds_from_gmt(&self, date: CFDate) -> CFTimeInterval {
53        unsafe { CFTimeZoneGetSecondsFromGMT(self.0, date.abs_time()) }
54    }
55
56    /// The timezone database ID that identifies the time zone. E.g. `"America/Los_Angeles" `or
57    /// `"Europe/Paris"`.
58    pub fn name(&self) -> CFString {
59        unsafe { CFString::wrap_under_get_rule(CFTimeZoneGetName(self.0)) }
60    }
61}
62
63#[cfg(test)]
64mod test {
65    use super::CFTimeZone;
66
67    #[test]
68    fn timezone_comparison() {
69        let system = CFTimeZone::system();
70        let default = CFTimeZone::default();
71        assert_eq!(system, default);
72    }
73}