rust_macios/foundation/
ns_date.rs

1use objc::{msg_send, sel, sel_impl};
2
3use crate::{object, 
4    objective_c_runtime::{
5        macros::{interface_impl},
6        traits::{FromId, PNSObject},
7    },
8    utils::to_bool,
9};
10
11use super::{NSComparisonResult, NSTimeInterval};
12
13object! {
14    /// A representation of a specific point in time, independent of any calendar or time zone.
15    unsafe pub struct NSDate;
16}
17
18#[interface_impl(NSObject)]
19impl NSDate {
20    /* Creating a Date
21     */
22
23    /// Creates and returns a new date object set to the current date and time.
24    #[method]
25    pub fn date() -> Self
26    where
27        Self: Sized + FromId,
28    {
29        unsafe { Self::from_id(msg_send![Self::m_class(), date]) }
30    }
31
32    /// Creates and returns a date object set to a given number of seconds from the current date and time.
33    #[method]
34    pub fn date_with_time_interval_since_now(time_interval: NSTimeInterval) -> Self
35    where
36        Self: Sized + FromId,
37    {
38        unsafe {
39            Self::from_id(msg_send![
40                Self::m_class(),
41                dateWithTimeIntervalSinceNow: time_interval
42            ])
43        }
44    }
45
46    /// Creates and returns a date object set to a given number of seconds from the specified date.
47    #[method]
48    pub fn date_with_time_interval_since_date(
49        secs_to_be_added: NSTimeInterval,
50        date: NSDate,
51    ) -> Self
52    where
53        Self: Sized + FromId,
54    {
55        unsafe {
56            Self::from_id(msg_send![
57                Self::m_class(),
58                dateWithTimeInterval: secs_to_be_added
59                sinceDate: date
60            ])
61        }
62    }
63
64    /// Creates and returns a date object set to a given number of seconds from 00:00:00 UTC on 1 January 2001.
65    #[method]
66    pub fn date_with_time_interval_since_reference_date(secs_to_be_added: NSTimeInterval) -> Self
67    where
68        Self: Sized + FromId,
69    {
70        unsafe {
71            Self::from_id(msg_send![
72                Self::m_class(),
73                dateWithTimeIntervalSinceReferenceDate: secs_to_be_added
74            ])
75        }
76    }
77
78    /// Creates and returns a date object set to the given number of seconds from 00:00:00 UTC on 1 January 1970.
79    #[method]
80    pub fn date_with_time_interval_since1970(secs_to_be_added: NSTimeInterval) -> Self
81    where
82        Self: Sized + FromId,
83    {
84        unsafe {
85            Self::from_id(msg_send![
86                Self::m_class(),
87                dateWithTimeIntervalSince1970: secs_to_be_added
88            ])
89        }
90    }
91
92    /* Getting Temporal Boundaries
93     */
94
95    /// A date object representing a date in the distant future.
96    #[property]
97    pub fn distant_future() -> NSDate {
98        unsafe { NSDate::from_id(msg_send![Self::m_class(), distantFuture]) }
99    }
100
101    /// A date object representing a date in the distant past.
102    #[property]
103    pub fn distant_past() -> NSDate {
104        unsafe { NSDate::from_id(msg_send![Self::m_class(), distantPast]) }
105    }
106
107    /* Retrieving the Current Date
108     */
109
110    /// The current date and time, as of the time of access.
111    #[property]
112    pub fn now() -> NSDate {
113        unsafe { NSDate::from_id(msg_send![Self::m_class(), now]) }
114    }
115
116    /* Comparing Dates
117     */
118
119    /// Returns a Boolean value that indicates whether a given object is a date that is exactly equal the receiver.
120    #[method]
121    pub fn is_equal_to_date(&self, date: NSDate) -> bool {
122        unsafe { to_bool(msg_send![self.m_self(), isEqualToDate: date]) }
123    }
124
125    /// Returns the earlier of the receiver and another given date.
126    #[method]
127    pub fn earlier_date(&self, date: NSDate) -> Self
128    where
129        Self: Sized + FromId,
130    {
131        unsafe { Self::from_id(msg_send![self.m_self(), earlierDate: date]) }
132    }
133
134    /// Returns the later of the receiver and another given date.
135    #[method]
136    pub fn later_date(&self, date: NSDate) -> Self
137    where
138        Self: Sized + FromId,
139    {
140        unsafe { Self::from_id(msg_send![self.m_self(), laterDate: date]) }
141    }
142
143    /// Indicates the temporal ordering of the receiver and another given date.
144    #[method]
145    pub fn compare(&self, date: NSDate) -> NSComparisonResult {
146        unsafe { msg_send![self.m_self(), compare: date] }
147    }
148
149    /* Getting Time Intervals
150     */
151
152    /// Returns the interval between the receiver and another given date.
153    #[method]
154    pub fn time_interval_since_date(&self, date: NSDate) -> NSTimeInterval {
155        unsafe { msg_send![self.m_self(), timeIntervalSinceDate: date] }
156    }
157
158    /// The interval between the date object and the current date and time.
159    #[property]
160    pub fn time_interval_since_now(&self) -> NSTimeInterval {
161        unsafe { msg_send![self.m_self(), timeIntervalSinceNow] }
162    }
163
164    /// The interval between the date object and 00:00:00 UTC on 1 January 2001.
165    #[property]
166    pub fn time_interval_since_reference_date() -> NSTimeInterval {
167        unsafe { msg_send![Self::m_class(), timeIntervalSinceReferenceDate] }
168    }
169
170    /// The interval between the date object and 00:00:00 UTC on 1 January 1970.
171    #[property]
172    pub fn time_interval_since_1970() -> NSTimeInterval {
173        unsafe { msg_send![Self::m_class(), timeIntervalSince1970] }
174    }
175
176    /* Adding Time Intervals
177     */
178
179    /// Returns a new date object that is set to a given number of seconds relative to the receiver.
180    #[method]
181    pub fn date_by_adding_time_interval(&self, secs_to_be_added: NSTimeInterval) -> Self
182    where
183        Self: Sized + FromId,
184    {
185        unsafe {
186            Self::from_id(msg_send![
187                self.m_self(),
188                dateByAddingTimeInterval: secs_to_be_added
189            ])
190        }
191    }
192}