1use core_foundation_sys::base::kCFAllocatorDefault;
13pub use core_foundation_sys::date::*;
14
15use crate::base::TCFType;
16
17declare_TCFType! {
18 CFDate, CFDateRef
20}
21impl_TCFType!(CFDate, CFDateRef, CFDateGetTypeID);
22impl_CFTypeDescription!(CFDate);
23impl_CFComparison!(CFDate, CFDateCompare);
24
25impl CFDate {
26 #[inline]
27 pub fn new(time: CFAbsoluteTime) -> CFDate {
28 unsafe {
29 let date_ref = CFDateCreate(kCFAllocatorDefault, time);
30 TCFType::wrap_under_create_rule(date_ref)
31 }
32 }
33
34 #[inline]
35 pub fn now() -> CFDate {
36 CFDate::new(unsafe { CFAbsoluteTimeGetCurrent() })
37 }
38
39 #[inline]
40 pub fn abs_time(&self) -> CFAbsoluteTime {
41 unsafe { CFDateGetAbsoluteTime(self.0) }
42 }
43}
44
45#[cfg(test)]
46mod test {
47 use super::CFDate;
48 use std::cmp::Ordering;
49
50 #[test]
51 fn date_comparison() {
52 let now = CFDate::now();
53 let past = CFDate::new(now.abs_time() - 1.0);
54 assert_eq!(now.cmp(&past), Ordering::Greater);
55 assert_eq!(now.cmp(&now), Ordering::Equal);
56 assert_eq!(past.cmp(&now), Ordering::Less);
57 }
58
59 #[test]
60 fn date_equality() {
61 let now = CFDate::now();
62 let same_time = CFDate::new(now.abs_time());
63 assert_eq!(now, same_time);
64 }
65}