1use glib_sys;
6use std::cmp;
7use std::hash;
8use std::mem;
9use translate::*;
10use GString;
11use TimeSpan;
12use TimeZone;
13
14glib_wrapper! {
15 #[derive(Debug)]
16 pub struct DateTime(Shared<glib_sys::GDateTime>);
17
18 match fn {
19 ref => |ptr| glib_sys::g_date_time_ref(ptr),
20 unref => |ptr| glib_sys::g_date_time_unref(ptr),
21 get_type => || glib_sys::g_date_time_get_type(),
22 }
23}
24
25impl DateTime {
26 pub fn new(
27 tz: &TimeZone,
28 year: i32,
29 month: i32,
30 day: i32,
31 hour: i32,
32 minute: i32,
33 seconds: f64,
34 ) -> DateTime {
35 unsafe {
36 from_glib_full(glib_sys::g_date_time_new(
37 tz.to_glib_none().0,
38 year,
39 month,
40 day,
41 hour,
42 minute,
43 seconds,
44 ))
45 }
46 }
47
48 #[cfg(any(feature = "v2_56", feature = "dox"))]
49 pub fn new_from_iso8601(text: &str, default_tz: Option<&TimeZone>) -> Option<DateTime> {
50 unsafe {
51 from_glib_full(glib_sys::g_date_time_new_from_iso8601(
52 text.to_glib_none().0,
53 default_tz.to_glib_none().0,
54 ))
55 }
56 }
57
58 pub fn new_from_unix_local(t: i64) -> DateTime {
67 unsafe { from_glib_full(glib_sys::g_date_time_new_from_unix_local(t)) }
68 }
69
70 pub fn new_from_unix_utc(t: i64) -> DateTime {
71 unsafe { from_glib_full(glib_sys::g_date_time_new_from_unix_utc(t)) }
72 }
73
74 pub fn new_local(
75 year: i32,
76 month: i32,
77 day: i32,
78 hour: i32,
79 minute: i32,
80 seconds: f64,
81 ) -> DateTime {
82 unsafe {
83 from_glib_full(glib_sys::g_date_time_new_local(
84 year, month, day, hour, minute, seconds,
85 ))
86 }
87 }
88
89 pub fn new_now(tz: &TimeZone) -> DateTime {
90 unsafe { from_glib_full(glib_sys::g_date_time_new_now(tz.to_glib_none().0)) }
91 }
92
93 pub fn new_now_local() -> DateTime {
94 unsafe { from_glib_full(glib_sys::g_date_time_new_now_local()) }
95 }
96
97 pub fn new_now_utc() -> DateTime {
98 unsafe { from_glib_full(glib_sys::g_date_time_new_now_utc()) }
99 }
100
101 pub fn new_utc(
102 year: i32,
103 month: i32,
104 day: i32,
105 hour: i32,
106 minute: i32,
107 seconds: f64,
108 ) -> DateTime {
109 unsafe {
110 from_glib_full(glib_sys::g_date_time_new_utc(
111 year, month, day, hour, minute, seconds,
112 ))
113 }
114 }
115
116 pub fn add(&self, timespan: TimeSpan) -> Option<DateTime> {
117 unsafe { from_glib_full(glib_sys::g_date_time_add(self.to_glib_none().0, timespan)) }
118 }
119
120 pub fn add_days(&self, days: i32) -> Option<DateTime> {
121 unsafe { from_glib_full(glib_sys::g_date_time_add_days(self.to_glib_none().0, days)) }
122 }
123
124 pub fn add_full(
125 &self,
126 years: i32,
127 months: i32,
128 days: i32,
129 hours: i32,
130 minutes: i32,
131 seconds: f64,
132 ) -> Option<DateTime> {
133 unsafe {
134 from_glib_full(glib_sys::g_date_time_add_full(
135 self.to_glib_none().0,
136 years,
137 months,
138 days,
139 hours,
140 minutes,
141 seconds,
142 ))
143 }
144 }
145
146 pub fn add_hours(&self, hours: i32) -> Option<DateTime> {
147 unsafe {
148 from_glib_full(glib_sys::g_date_time_add_hours(
149 self.to_glib_none().0,
150 hours,
151 ))
152 }
153 }
154
155 pub fn add_minutes(&self, minutes: i32) -> Option<DateTime> {
156 unsafe {
157 from_glib_full(glib_sys::g_date_time_add_minutes(
158 self.to_glib_none().0,
159 minutes,
160 ))
161 }
162 }
163
164 pub fn add_months(&self, months: i32) -> Option<DateTime> {
165 unsafe {
166 from_glib_full(glib_sys::g_date_time_add_months(
167 self.to_glib_none().0,
168 months,
169 ))
170 }
171 }
172
173 pub fn add_seconds(&self, seconds: f64) -> Option<DateTime> {
174 unsafe {
175 from_glib_full(glib_sys::g_date_time_add_seconds(
176 self.to_glib_none().0,
177 seconds,
178 ))
179 }
180 }
181
182 pub fn add_weeks(&self, weeks: i32) -> Option<DateTime> {
183 unsafe {
184 from_glib_full(glib_sys::g_date_time_add_weeks(
185 self.to_glib_none().0,
186 weeks,
187 ))
188 }
189 }
190
191 pub fn add_years(&self, years: i32) -> Option<DateTime> {
192 unsafe {
193 from_glib_full(glib_sys::g_date_time_add_years(
194 self.to_glib_none().0,
195 years,
196 ))
197 }
198 }
199
200 pub fn difference(&self, begin: &DateTime) -> TimeSpan {
201 unsafe { glib_sys::g_date_time_difference(self.to_glib_none().0, begin.to_glib_none().0) }
202 }
203
204 pub fn format(&self, format: &str) -> Option<GString> {
205 unsafe {
206 from_glib_full(glib_sys::g_date_time_format(
207 self.to_glib_none().0,
208 format.to_glib_none().0,
209 ))
210 }
211 }
212
213 pub fn get_day_of_month(&self) -> i32 {
214 unsafe { glib_sys::g_date_time_get_day_of_month(self.to_glib_none().0) }
215 }
216
217 pub fn get_day_of_week(&self) -> i32 {
218 unsafe { glib_sys::g_date_time_get_day_of_week(self.to_glib_none().0) }
219 }
220
221 pub fn get_day_of_year(&self) -> i32 {
222 unsafe { glib_sys::g_date_time_get_day_of_year(self.to_glib_none().0) }
223 }
224
225 pub fn get_hour(&self) -> i32 {
226 unsafe { glib_sys::g_date_time_get_hour(self.to_glib_none().0) }
227 }
228
229 pub fn get_microsecond(&self) -> i32 {
230 unsafe { glib_sys::g_date_time_get_microsecond(self.to_glib_none().0) }
231 }
232
233 pub fn get_minute(&self) -> i32 {
234 unsafe { glib_sys::g_date_time_get_minute(self.to_glib_none().0) }
235 }
236
237 pub fn get_month(&self) -> i32 {
238 unsafe { glib_sys::g_date_time_get_month(self.to_glib_none().0) }
239 }
240
241 pub fn get_second(&self) -> i32 {
242 unsafe { glib_sys::g_date_time_get_second(self.to_glib_none().0) }
243 }
244
245 pub fn get_seconds(&self) -> f64 {
246 unsafe { glib_sys::g_date_time_get_seconds(self.to_glib_none().0) }
247 }
248
249 #[cfg(any(feature = "v2_58", feature = "dox"))]
250 pub fn get_timezone(&self) -> Option<TimeZone> {
251 unsafe { from_glib_none(glib_sys::g_date_time_get_timezone(self.to_glib_none().0)) }
252 }
253
254 pub fn get_timezone_abbreviation(&self) -> Option<GString> {
255 unsafe {
256 from_glib_none(glib_sys::g_date_time_get_timezone_abbreviation(
257 self.to_glib_none().0,
258 ))
259 }
260 }
261
262 pub fn get_utc_offset(&self) -> TimeSpan {
263 unsafe { glib_sys::g_date_time_get_utc_offset(self.to_glib_none().0) }
264 }
265
266 pub fn get_week_numbering_year(&self) -> i32 {
267 unsafe { glib_sys::g_date_time_get_week_numbering_year(self.to_glib_none().0) }
268 }
269
270 pub fn get_week_of_year(&self) -> i32 {
271 unsafe { glib_sys::g_date_time_get_week_of_year(self.to_glib_none().0) }
272 }
273
274 pub fn get_year(&self) -> i32 {
275 unsafe { glib_sys::g_date_time_get_year(self.to_glib_none().0) }
276 }
277
278 pub fn get_ymd(&self) -> (i32, i32, i32) {
279 unsafe {
280 let mut year = mem::uninitialized();
281 let mut month = mem::uninitialized();
282 let mut day = mem::uninitialized();
283 glib_sys::g_date_time_get_ymd(self.to_glib_none().0, &mut year, &mut month, &mut day);
284 (year, month, day)
285 }
286 }
287
288 pub fn is_daylight_savings(&self) -> bool {
289 unsafe {
290 from_glib(glib_sys::g_date_time_is_daylight_savings(
291 self.to_glib_none().0,
292 ))
293 }
294 }
295
296 pub fn to_local(&self) -> Option<DateTime> {
297 unsafe { from_glib_full(glib_sys::g_date_time_to_local(self.to_glib_none().0)) }
298 }
299
300 pub fn to_timezone(&self, tz: &TimeZone) -> Option<DateTime> {
305 unsafe {
306 from_glib_full(glib_sys::g_date_time_to_timezone(
307 self.to_glib_none().0,
308 tz.to_glib_none().0,
309 ))
310 }
311 }
312
313 pub fn to_unix(&self) -> i64 {
314 unsafe { glib_sys::g_date_time_to_unix(self.to_glib_none().0) }
315 }
316
317 pub fn to_utc(&self) -> Option<DateTime> {
318 unsafe { from_glib_full(glib_sys::g_date_time_to_utc(self.to_glib_none().0)) }
319 }
320
321 fn compare(&self, dt2: &DateTime) -> i32 {
322 unsafe {
323 glib_sys::g_date_time_compare(
324 ToGlibPtr::<*mut glib_sys::GDateTime>::to_glib_none(self).0
325 as glib_sys::gconstpointer,
326 ToGlibPtr::<*mut glib_sys::GDateTime>::to_glib_none(dt2).0
327 as glib_sys::gconstpointer,
328 )
329 }
330 }
331
332 fn equal(&self, dt2: &DateTime) -> bool {
333 unsafe {
334 from_glib(glib_sys::g_date_time_equal(
335 ToGlibPtr::<*mut glib_sys::GDateTime>::to_glib_none(self).0
336 as glib_sys::gconstpointer,
337 ToGlibPtr::<*mut glib_sys::GDateTime>::to_glib_none(dt2).0
338 as glib_sys::gconstpointer,
339 ))
340 }
341 }
342
343 fn hash(&self) -> u32 {
344 unsafe {
345 glib_sys::g_date_time_hash(
346 ToGlibPtr::<*mut glib_sys::GDateTime>::to_glib_none(self).0
347 as glib_sys::gconstpointer,
348 )
349 }
350 }
351}
352
353impl PartialOrd for DateTime {
354 #[inline]
355 fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
356 self.compare(other).partial_cmp(&0)
357 }
358}
359
360impl Ord for DateTime {
361 #[inline]
362 fn cmp(&self, other: &Self) -> cmp::Ordering {
363 self.compare(other).cmp(&0)
364 }
365}
366
367impl PartialEq for DateTime {
368 #[inline]
369 fn eq(&self, other: &Self) -> bool {
370 self.equal(other)
371 }
372}
373
374impl Eq for DateTime {}
375
376impl hash::Hash for DateTime {
377 #[inline]
378 fn hash<H>(&self, state: &mut H)
379 where
380 H: hash::Hasher,
381 {
382 hash::Hash::hash(&self.hash(), state)
383 }
384}
385
386unsafe impl Send for DateTime {}
387unsafe impl Sync for DateTime {}