1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
pub use core_foundation_sys::timezone::*;
use core_foundation_sys::base::kCFAllocatorDefault;
use base::TCFType;
use date::{CFDate, CFTimeInterval};
#[cfg(feature = "with-chrono")]
use chrono::{FixedOffset, NaiveDateTime};
declare_TCFType!{
CFTimeZone, CFTimeZoneRef
}
impl_TCFType!(CFTimeZone, CFTimeZoneRef, CFTimeZoneGetTypeID);
impl_CFTypeDescription!(CFTimeZone);
impl Default for CFTimeZone {
fn default() -> CFTimeZone {
unsafe {
let tz_ref = CFTimeZoneCopyDefault();
TCFType::wrap_under_create_rule(tz_ref)
}
}
}
impl CFTimeZone {
#[inline]
pub fn new(interval: CFTimeInterval) -> CFTimeZone {
unsafe {
let tz_ref = CFTimeZoneCreateWithTimeIntervalFromGMT(kCFAllocatorDefault, interval);
TCFType::wrap_under_create_rule(tz_ref)
}
}
#[inline]
pub fn system() -> CFTimeZone {
unsafe {
let tz_ref = CFTimeZoneCopySystem();
TCFType::wrap_under_create_rule(tz_ref)
}
}
pub fn seconds_from_gmt(&self, date: CFDate) -> CFTimeInterval {
unsafe {
CFTimeZoneGetSecondsFromGMT(self.0, date.abs_time())
}
}
#[cfg(feature = "with-chrono")]
pub fn offset_at_date(&self, date: NaiveDateTime) -> FixedOffset {
let date = CFDate::from_naive_utc(date);
FixedOffset::east(self.seconds_from_gmt(date) as i32)
}
#[cfg(feature = "with-chrono")]
pub fn from_offset(offset: FixedOffset) -> CFTimeZone {
CFTimeZone::new(offset.local_minus_utc() as f64)
}
}
#[cfg(test)]
mod test {
use super::CFTimeZone;
#[cfg(feature = "with-chrono")]
use chrono::{NaiveDateTime, FixedOffset};
#[test]
fn timezone_comparison() {
let system = CFTimeZone::system();
let default = CFTimeZone::default();
assert_eq!(system, default);
}
#[test]
#[cfg(feature = "with-chrono")]
fn timezone_chrono_conversion() {
let offset = FixedOffset::west(28800);
let tz = CFTimeZone::from_offset(offset);
let converted = tz.offset_at_date(NaiveDateTime::from_timestamp(0, 0));
assert_eq!(offset, converted);
}
}