Debian Codegraph @

third_party/rust/core-foundation/src/date.rs

raw
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 date objects.
11
12 use core_foundation_sys::base::kCFAllocatorDefault;
13 pub use core_foundation_sys::date::*;
14
15 use crate::base::TCFType;
16
17 declare_TCFType! {
18 /// A date.
19 CFDate, CFDateRef
20 }
21 impl_TCFType!(CFDate, CFDateRef, CFDateGetTypeID);
22 impl_CFTypeDescription!(CFDate);
23 impl_CFComparison!(CFDate, CFDateCompare);
24
25 impl 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)]
46 mod 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 }