glib/
date.rs

1// Copyright 2017, The Gtk-rs Project Developers.
2// See the COPYRIGHT file at the top-level directory of this distribution.
3// Licensed under the MIT license, see the LICENSE file or <http://opensource.org/licenses/MIT>
4
5use glib_sys;
6use gobject_sys;
7use libc;
8use std::cmp;
9use std::fmt;
10use std::hash;
11use translate::*;
12use DateDay;
13use DateMonth;
14use DateWeekday;
15use DateYear;
16use Time;
17
18glib_wrapper! {
19    pub struct Date(Boxed<glib_sys::GDate>);
20
21    match fn {
22        copy => |ptr| gobject_sys::g_boxed_copy(glib_sys::g_date_get_type(), ptr as *const _) as *mut _,
23        free => |ptr| glib_sys::g_date_free(ptr),
24        init => |_ptr| (),
25        clear => |ptr| glib_sys::g_date_clear(ptr, 1),
26        get_type => || glib_sys::g_date_get_type(),
27    }
28}
29
30unsafe impl Send for Date {}
31unsafe impl Sync for Date {}
32
33impl Date {
34    pub fn new() -> Date {
35        unsafe { from_glib_full(glib_sys::g_date_new()) }
36    }
37
38    pub fn new_dmy(day: DateDay, month: DateMonth, year: DateYear) -> Date {
39        unsafe { from_glib_full(glib_sys::g_date_new_dmy(day, month.to_glib(), year)) }
40    }
41
42    pub fn new_julian(julian_day: u32) -> Date {
43        unsafe { from_glib_full(glib_sys::g_date_new_julian(julian_day)) }
44    }
45
46    pub fn add_days(&mut self, n_days: u32) {
47        unsafe {
48            glib_sys::g_date_add_days(self.to_glib_none_mut().0, n_days);
49        }
50    }
51
52    pub fn add_months(&mut self, n_months: u32) {
53        unsafe {
54            glib_sys::g_date_add_months(self.to_glib_none_mut().0, n_months);
55        }
56    }
57
58    pub fn add_years(&mut self, n_years: u32) {
59        unsafe {
60            glib_sys::g_date_add_years(self.to_glib_none_mut().0, n_years);
61        }
62    }
63
64    pub fn clamp(&mut self, min_date: &Date, max_date: &Date) {
65        unsafe {
66            glib_sys::g_date_clamp(
67                self.to_glib_none_mut().0,
68                min_date.to_glib_none().0,
69                max_date.to_glib_none().0,
70            );
71        }
72    }
73
74    pub fn clear(&mut self, n_dates: u32) {
75        unsafe {
76            glib_sys::g_date_clear(self.to_glib_none_mut().0, n_dates);
77        }
78    }
79
80    fn compare(&self, rhs: &Date) -> i32 {
81        unsafe { glib_sys::g_date_compare(self.to_glib_none().0, rhs.to_glib_none().0) }
82    }
83
84    pub fn days_between(&self, date2: &Date) -> i32 {
85        unsafe { glib_sys::g_date_days_between(self.to_glib_none().0, date2.to_glib_none().0) }
86    }
87
88    pub fn get_day(&self) -> DateDay {
89        unsafe { glib_sys::g_date_get_day(self.to_glib_none().0) }
90    }
91
92    pub fn get_day_of_year(&self) -> u32 {
93        unsafe { glib_sys::g_date_get_day_of_year(self.to_glib_none().0) }
94    }
95
96    pub fn get_iso8601_week_of_year(&self) -> u32 {
97        unsafe { glib_sys::g_date_get_iso8601_week_of_year(self.to_glib_none().0) }
98    }
99
100    pub fn get_julian(&self) -> u32 {
101        unsafe { glib_sys::g_date_get_julian(self.to_glib_none().0) }
102    }
103
104    pub fn get_monday_week_of_year(&self) -> u32 {
105        unsafe { glib_sys::g_date_get_monday_week_of_year(self.to_glib_none().0) }
106    }
107
108    pub fn get_month(&self) -> DateMonth {
109        unsafe { from_glib(glib_sys::g_date_get_month(self.to_glib_none().0)) }
110    }
111
112    pub fn get_sunday_week_of_year(&self) -> u32 {
113        unsafe { glib_sys::g_date_get_sunday_week_of_year(self.to_glib_none().0) }
114    }
115
116    pub fn get_weekday(&self) -> DateWeekday {
117        unsafe { from_glib(glib_sys::g_date_get_weekday(self.to_glib_none().0)) }
118    }
119
120    pub fn get_year(&self) -> DateYear {
121        unsafe { glib_sys::g_date_get_year(self.to_glib_none().0) }
122    }
123
124    pub fn is_first_of_month(&self) -> bool {
125        unsafe { from_glib(glib_sys::g_date_is_first_of_month(self.to_glib_none().0)) }
126    }
127
128    pub fn is_last_of_month(&self) -> bool {
129        unsafe { from_glib(glib_sys::g_date_is_last_of_month(self.to_glib_none().0)) }
130    }
131
132    pub fn order(&mut self, date2: &mut Date) {
133        unsafe {
134            glib_sys::g_date_order(self.to_glib_none_mut().0, date2.to_glib_none_mut().0);
135        }
136    }
137
138    pub fn set_day(&mut self, day: DateDay) {
139        unsafe {
140            glib_sys::g_date_set_day(self.to_glib_none_mut().0, day);
141        }
142    }
143
144    pub fn set_dmy(&mut self, day: DateDay, month: DateMonth, y: DateYear) {
145        unsafe {
146            glib_sys::g_date_set_dmy(self.to_glib_none_mut().0, day, month.to_glib(), y);
147        }
148    }
149
150    pub fn set_julian(&mut self, julian_date: u32) {
151        unsafe {
152            glib_sys::g_date_set_julian(self.to_glib_none_mut().0, julian_date);
153        }
154    }
155
156    pub fn set_month(&mut self, month: DateMonth) {
157        unsafe {
158            glib_sys::g_date_set_month(self.to_glib_none_mut().0, month.to_glib());
159        }
160    }
161
162    pub fn set_parse(&mut self, str: &str) {
163        unsafe {
164            glib_sys::g_date_set_parse(self.to_glib_none_mut().0, str.to_glib_none().0);
165        }
166    }
167
168    pub fn set_time(&mut self, time_: Time) {
169        unsafe {
170            glib_sys::g_date_set_time(self.to_glib_none_mut().0, time_);
171        }
172    }
173
174    pub fn set_time_t(&mut self, timet: libc::c_long) {
175        unsafe {
176            glib_sys::g_date_set_time_t(self.to_glib_none_mut().0, timet);
177        }
178    }
179
180    //pub fn set_time_val(&mut self, timeval: /*Ignored*/&mut TimeVal) {
181    //    unsafe { TODO: call glib_sys::g_date_set_time_val() }
182    //}
183
184    pub fn set_year(&mut self, year: DateYear) {
185        unsafe {
186            glib_sys::g_date_set_year(self.to_glib_none_mut().0, year);
187        }
188    }
189
190    pub fn subtract_days(&mut self, n_days: u32) {
191        unsafe {
192            glib_sys::g_date_subtract_days(self.to_glib_none_mut().0, n_days);
193        }
194    }
195
196    pub fn subtract_months(&mut self, n_months: u32) {
197        unsafe {
198            glib_sys::g_date_subtract_months(self.to_glib_none_mut().0, n_months);
199        }
200    }
201
202    pub fn subtract_years(&mut self, n_years: u32) {
203        unsafe {
204            glib_sys::g_date_subtract_years(self.to_glib_none_mut().0, n_years);
205        }
206    }
207
208    //pub fn to_struct_tm(&self, tm: /*Unimplemented*/Fundamental: Pointer) {
209    //    unsafe { TODO: call glib_sys::g_date_to_struct_tm() }
210    //}
211
212    pub fn valid(&self) -> bool {
213        unsafe { from_glib(glib_sys::g_date_valid(self.to_glib_none().0)) }
214    }
215
216    pub fn get_days_in_month(month: DateMonth, year: DateYear) -> u8 {
217        unsafe { glib_sys::g_date_get_days_in_month(month.to_glib(), year) }
218    }
219
220    pub fn get_monday_weeks_in_year(year: DateYear) -> u8 {
221        unsafe { glib_sys::g_date_get_monday_weeks_in_year(year) }
222    }
223
224    pub fn get_sunday_weeks_in_year(year: DateYear) -> u8 {
225        unsafe { glib_sys::g_date_get_sunday_weeks_in_year(year) }
226    }
227
228    pub fn is_leap_year(year: DateYear) -> bool {
229        unsafe { from_glib(glib_sys::g_date_is_leap_year(year)) }
230    }
231
232    pub fn strftime(s: &str, format: &str, date: &Date) -> usize {
233        let slen = s.len() as usize;
234        unsafe {
235            glib_sys::g_date_strftime(
236                s.to_glib_none().0,
237                slen,
238                format.to_glib_none().0,
239                date.to_glib_none().0,
240            )
241        }
242    }
243
244    pub fn valid_day(day: DateDay) -> bool {
245        unsafe { from_glib(glib_sys::g_date_valid_day(day)) }
246    }
247
248    pub fn valid_dmy(day: DateDay, month: DateMonth, year: DateYear) -> bool {
249        unsafe { from_glib(glib_sys::g_date_valid_dmy(day, month.to_glib(), year)) }
250    }
251
252    pub fn valid_julian(julian_date: u32) -> bool {
253        unsafe { from_glib(glib_sys::g_date_valid_julian(julian_date)) }
254    }
255
256    pub fn valid_month(month: DateMonth) -> bool {
257        unsafe { from_glib(glib_sys::g_date_valid_month(month.to_glib())) }
258    }
259
260    pub fn valid_weekday(weekday: DateWeekday) -> bool {
261        unsafe { from_glib(glib_sys::g_date_valid_weekday(weekday.to_glib())) }
262    }
263
264    pub fn valid_year(year: DateYear) -> bool {
265        unsafe { from_glib(glib_sys::g_date_valid_year(year)) }
266    }
267}
268
269impl Default for Date {
270    fn default() -> Self {
271        Self::new()
272    }
273}
274
275impl PartialEq for Date {
276    #[inline]
277    fn eq(&self, other: &Self) -> bool {
278        self.compare(other) == 0
279    }
280}
281
282impl Eq for Date {}
283
284impl PartialOrd for Date {
285    #[inline]
286    fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
287        self.compare(other).partial_cmp(&0)
288    }
289}
290
291impl Ord for Date {
292    #[inline]
293    fn cmp(&self, other: &Self) -> cmp::Ordering {
294        self.compare(other).cmp(&0)
295    }
296}
297
298impl fmt::Debug for Date {
299    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
300        f.debug_struct("Date")
301            .field("year", &self.get_year())
302            .field("month", &self.get_month())
303            .field("day", &self.get_day())
304            .finish()
305    }
306}
307
308impl hash::Hash for Date {
309    fn hash<H>(&self, state: &mut H)
310    where
311        H: hash::Hasher,
312    {
313        self.get_year().hash(state);
314        self.get_month().hash(state);
315        self.get_day().hash(state);
316    }
317}