glib/auto/date_time.rs
1// This file was generated by gir (https://github.com/gtk-rs/gir)
2// from gir-files (https://github.com/gtk-rs/gir-files)
3// DO NOT EDIT
4
5use crate::{BoolError, TimeSpan, TimeZone, ffi, translate::*};
6
7crate::wrapper! {
8 /// `GDateTime` is a structure that combines a Gregorian date and time
9 /// into a single structure.
10 ///
11 /// `GDateTime` provides many conversion and methods to manipulate dates and times.
12 /// Time precision is provided down to microseconds and the time can range
13 /// (proleptically) from 0001-01-01 00:00:00 to 9999-12-31 23:59:59.999999.
14 /// `GDateTime` follows POSIX time in the sense that it is oblivious to leap
15 /// seconds.
16 ///
17 /// `GDateTime` is an immutable object; once it has been created it cannot
18 /// be modified further. All modifiers will create a new `GDateTime`.
19 /// Nearly all such functions can fail due to the date or time going out
20 /// of range, in which case [`None`] will be returned.
21 ///
22 /// `GDateTime` is reference counted: the reference count is increased by calling
23 /// `GLib::DateTime::ref()` and decreased by calling `GLib::DateTime::unref()`.
24 /// When the reference count drops to 0, the resources allocated by the `GDateTime`
25 /// structure are released.
26 ///
27 /// Many parts of the API may produce non-obvious results. As an
28 /// example, adding two months to January 31st will yield March 31st
29 /// whereas adding one month and then one month again will yield either
30 /// March 28th or March 29th. Also note that adding 24 hours is not
31 /// always the same as adding one day (since days containing daylight
32 /// savings time transitions are either 23 or 25 hours in length).
33 #[derive(Debug)]
34 pub struct DateTime(Shared<ffi::GDateTime>);
35
36 match fn {
37 ref => |ptr| ffi::g_date_time_ref(ptr),
38 unref => |ptr| ffi::g_date_time_unref(ptr),
39 type_ => || ffi::g_date_time_get_type(),
40 }
41}
42
43impl DateTime {
44 /// Creates a new #GDateTime corresponding to the given date and time in
45 /// the time zone @tz.
46 ///
47 /// The @year must be between 1 and 9999, @month between 1 and 12 and @day
48 /// between 1 and 28, 29, 30 or 31 depending on the month and the year.
49 ///
50 /// @hour must be between 0 and 23 and @minute must be between 0 and 59.
51 ///
52 /// @seconds must be at least 0.0 and must be strictly less than 60.0.
53 /// It will be rounded down to the nearest microsecond.
54 ///
55 /// If the given time is not representable in the given time zone (for
56 /// example, 02:30 on March 14th 2010 in Toronto, due to daylight savings
57 /// time) then the time will be rounded up to the nearest existing time
58 /// (in this case, 03:00). If this matters to you then you should verify
59 /// the return value for containing the same as the numbers you gave.
60 ///
61 /// In the case that the given time is ambiguous in the given time zone
62 /// (for example, 01:30 on November 7th 2010 in Toronto, due to daylight
63 /// savings time) then the time falling within standard (ie:
64 /// non-daylight) time is taken.
65 ///
66 /// It not considered a programmer error for the values to this function
67 /// to be out of range, but in the case that they are, the function will
68 /// return [`None`].
69 ///
70 /// You should release the return value by calling g_date_time_unref()
71 /// when you are done with it.
72 /// ## `tz`
73 /// a #GTimeZone
74 /// ## `year`
75 /// the year component of the date
76 /// ## `month`
77 /// the month component of the date
78 /// ## `day`
79 /// the day component of the date
80 /// ## `hour`
81 /// the hour component of the date
82 /// ## `minute`
83 /// the minute component of the date
84 /// ## `seconds`
85 /// the number of seconds past the minute
86 ///
87 /// # Returns
88 ///
89 /// a new #GDateTime, or [`None`]
90 #[doc(alias = "g_date_time_new")]
91 pub fn new(
92 tz: &TimeZone,
93 year: i32,
94 month: i32,
95 day: i32,
96 hour: i32,
97 minute: i32,
98 seconds: f64,
99 ) -> Result<DateTime, BoolError> {
100 unsafe {
101 Option::<_>::from_glib_full(ffi::g_date_time_new(
102 tz.to_glib_none().0,
103 year,
104 month,
105 day,
106 hour,
107 minute,
108 seconds,
109 ))
110 .ok_or_else(|| crate::bool_error!("Invalid date"))
111 }
112 }
113
114 /// ` is an optional timezone suffix of the form:
115 ///
116 /// - `Z` - UTC.
117 /// - `+hh:mm` or `-hh:mm` - Offset from UTC in hours and minutes, e.g. +12:00.
118 /// - `+hh` or `-hh` - Offset from UTC in hours, e.g. +12.
119 ///
120 /// If the timezone is not provided in @text it must be provided in @default_tz
121 /// (this field is otherwise ignored).
122 ///
123 /// This call can fail (returning [`None`]) if @text is not a valid ISO 8601
124 /// formatted string.
125 ///
126 /// You should release the return value by calling g_date_time_unref()
127 /// when you are done with it.
128 /// ## `text`
129 /// an ISO 8601 formatted time string.
130 /// ## `default_tz`
131 /// a #GTimeZone to use if the text doesn't contain a
132 /// timezone, or [`None`].
133 ///
134 /// # Returns
135 ///
136 /// a new #GDateTime, or [`None`]
137 #[doc(alias = "g_date_time_new_from_iso8601")]
138 #[doc(alias = "new_from_iso8601")]
139 pub fn from_iso8601(text: &str, default_tz: Option<&TimeZone>) -> Result<DateTime, BoolError> {
140 unsafe {
141 Option::<_>::from_glib_full(ffi::g_date_time_new_from_iso8601(
142 text.to_glib_none().0,
143 default_tz.to_glib_none().0,
144 ))
145 .ok_or_else(|| crate::bool_error!("Invalid date"))
146 }
147 }
148
149 //#[cfg_attr(feature = "v2_62", deprecated = "Since 2.62")]
150 //#[allow(deprecated)]
151 //#[doc(alias = "g_date_time_new_from_timeval_local")]
152 //#[doc(alias = "new_from_timeval_local")]
153 //pub fn from_timeval_local(tv: /*Ignored*/&TimeVal) -> Result<DateTime, BoolError> {
154 // unsafe { TODO: call ffi:g_date_time_new_from_timeval_local() }
155 //}
156
157 //#[cfg_attr(feature = "v2_62", deprecated = "Since 2.62")]
158 //#[allow(deprecated)]
159 //#[doc(alias = "g_date_time_new_from_timeval_utc")]
160 //#[doc(alias = "new_from_timeval_utc")]
161 //pub fn from_timeval_utc(tv: /*Ignored*/&TimeVal) -> Result<DateTime, BoolError> {
162 // unsafe { TODO: call ffi:g_date_time_new_from_timeval_utc() }
163 //}
164
165 /// Creates a #GDateTime corresponding to the given Unix time @t in the
166 /// local time zone.
167 ///
168 /// Unix time is the number of seconds that have elapsed since 1970-01-01
169 /// 00:00:00 UTC, regardless of the local time offset.
170 ///
171 /// This call can fail (returning [`None`]) if @t represents a time outside
172 /// of the supported range of #GDateTime.
173 ///
174 /// You should release the return value by calling g_date_time_unref()
175 /// when you are done with it.
176 /// ## `t`
177 /// the Unix time
178 ///
179 /// # Returns
180 ///
181 /// a new #GDateTime, or [`None`]
182 #[doc(alias = "g_date_time_new_from_unix_local")]
183 #[doc(alias = "new_from_unix_local")]
184 pub fn from_unix_local(t: i64) -> Result<DateTime, BoolError> {
185 unsafe {
186 Option::<_>::from_glib_full(ffi::g_date_time_new_from_unix_local(t))
187 .ok_or_else(|| crate::bool_error!("Invalid date"))
188 }
189 }
190
191 /// Creates a [`DateTime`][crate::DateTime] corresponding to the given Unix time @t in the
192 /// local time zone.
193 ///
194 /// Unix time is the number of microseconds that have elapsed since 1970-01-01
195 /// 00:00:00 UTC, regardless of the local time offset.
196 ///
197 /// This call can fail (returning `NULL`) if @t represents a time outside
198 /// of the supported range of #GDateTime.
199 ///
200 /// You should release the return value by calling `GLib::DateTime::unref()`
201 /// when you are done with it.
202 /// ## `usecs`
203 /// the Unix time in microseconds
204 ///
205 /// # Returns
206 ///
207 /// a new [`DateTime`][crate::DateTime], or `NULL`
208 #[cfg(feature = "v2_80")]
209 #[cfg_attr(docsrs, doc(cfg(feature = "v2_80")))]
210 #[doc(alias = "g_date_time_new_from_unix_local_usec")]
211 #[doc(alias = "new_from_unix_local_usec")]
212 pub fn from_unix_local_usec(usecs: i64) -> Result<DateTime, BoolError> {
213 unsafe {
214 Option::<_>::from_glib_full(ffi::g_date_time_new_from_unix_local_usec(usecs))
215 .ok_or_else(|| crate::bool_error!("Invalid date"))
216 }
217 }
218
219 /// Creates a #GDateTime corresponding to the given Unix time @t in UTC.
220 ///
221 /// Unix time is the number of seconds that have elapsed since 1970-01-01
222 /// 00:00:00 UTC.
223 ///
224 /// This call can fail (returning [`None`]) if @t represents a time outside
225 /// of the supported range of #GDateTime.
226 ///
227 /// You should release the return value by calling g_date_time_unref()
228 /// when you are done with it.
229 /// ## `t`
230 /// the Unix time
231 ///
232 /// # Returns
233 ///
234 /// a new #GDateTime, or [`None`]
235 #[doc(alias = "g_date_time_new_from_unix_utc")]
236 #[doc(alias = "new_from_unix_utc")]
237 pub fn from_unix_utc(t: i64) -> Result<DateTime, BoolError> {
238 unsafe {
239 Option::<_>::from_glib_full(ffi::g_date_time_new_from_unix_utc(t))
240 .ok_or_else(|| crate::bool_error!("Invalid date"))
241 }
242 }
243
244 /// Creates a [`DateTime`][crate::DateTime] corresponding to the given Unix time @t in UTC.
245 ///
246 /// Unix time is the number of microseconds that have elapsed since 1970-01-01
247 /// 00:00:00 UTC.
248 ///
249 /// This call can fail (returning `NULL`) if @t represents a time outside
250 /// of the supported range of #GDateTime.
251 ///
252 /// You should release the return value by calling `GLib::DateTime::unref()`
253 /// when you are done with it.
254 /// ## `usecs`
255 /// the Unix time in microseconds
256 ///
257 /// # Returns
258 ///
259 /// a new [`DateTime`][crate::DateTime], or `NULL`
260 #[cfg(feature = "v2_80")]
261 #[cfg_attr(docsrs, doc(cfg(feature = "v2_80")))]
262 #[doc(alias = "g_date_time_new_from_unix_utc_usec")]
263 #[doc(alias = "new_from_unix_utc_usec")]
264 pub fn from_unix_utc_usec(usecs: i64) -> Result<DateTime, BoolError> {
265 unsafe {
266 Option::<_>::from_glib_full(ffi::g_date_time_new_from_unix_utc_usec(usecs))
267 .ok_or_else(|| crate::bool_error!("Invalid date"))
268 }
269 }
270
271 /// Creates a new #GDateTime corresponding to the given date and time in
272 /// the local time zone.
273 ///
274 /// This call is equivalent to calling g_date_time_new() with the time
275 /// zone returned by g_time_zone_new_local().
276 /// ## `year`
277 /// the year component of the date
278 /// ## `month`
279 /// the month component of the date
280 /// ## `day`
281 /// the day component of the date
282 /// ## `hour`
283 /// the hour component of the date
284 /// ## `minute`
285 /// the minute component of the date
286 /// ## `seconds`
287 /// the number of seconds past the minute
288 ///
289 /// # Returns
290 ///
291 /// a #GDateTime, or [`None`]
292 #[doc(alias = "g_date_time_new_local")]
293 #[doc(alias = "new_local")]
294 pub fn from_local(
295 year: i32,
296 month: i32,
297 day: i32,
298 hour: i32,
299 minute: i32,
300 seconds: f64,
301 ) -> Result<DateTime, BoolError> {
302 unsafe {
303 Option::<_>::from_glib_full(ffi::g_date_time_new_local(
304 year, month, day, hour, minute, seconds,
305 ))
306 .ok_or_else(|| crate::bool_error!("Invalid date"))
307 }
308 }
309
310 /// Creates a #GDateTime corresponding to this exact instant in the given
311 /// time zone @tz. The time is as accurate as the system allows, to a
312 /// maximum accuracy of 1 microsecond.
313 ///
314 /// This function will always succeed unless GLib is still being used after the
315 /// year 9999.
316 ///
317 /// You should release the return value by calling g_date_time_unref()
318 /// when you are done with it.
319 /// ## `tz`
320 /// a #GTimeZone
321 ///
322 /// # Returns
323 ///
324 /// a new #GDateTime, or [`None`]
325 #[doc(alias = "g_date_time_new_now")]
326 #[doc(alias = "new_now")]
327 pub fn now(tz: &TimeZone) -> Result<DateTime, BoolError> {
328 unsafe {
329 Option::<_>::from_glib_full(ffi::g_date_time_new_now(tz.to_glib_none().0))
330 .ok_or_else(|| crate::bool_error!("Invalid date"))
331 }
332 }
333
334 /// Creates a #GDateTime corresponding to this exact instant in the local
335 /// time zone.
336 ///
337 /// This is equivalent to calling g_date_time_new_now() with the time
338 /// zone returned by g_time_zone_new_local().
339 ///
340 /// # Returns
341 ///
342 /// a new #GDateTime, or [`None`]
343 #[doc(alias = "g_date_time_new_now_local")]
344 #[doc(alias = "new_now_local")]
345 pub fn now_local() -> Result<DateTime, BoolError> {
346 unsafe {
347 Option::<_>::from_glib_full(ffi::g_date_time_new_now_local())
348 .ok_or_else(|| crate::bool_error!("Invalid date"))
349 }
350 }
351
352 /// Creates a #GDateTime corresponding to this exact instant in UTC.
353 ///
354 /// This is equivalent to calling g_date_time_new_now() with the time
355 /// zone returned by g_time_zone_new_utc().
356 ///
357 /// # Returns
358 ///
359 /// a new #GDateTime, or [`None`]
360 #[doc(alias = "g_date_time_new_now_utc")]
361 #[doc(alias = "new_now_utc")]
362 pub fn now_utc() -> Result<DateTime, BoolError> {
363 unsafe {
364 Option::<_>::from_glib_full(ffi::g_date_time_new_now_utc())
365 .ok_or_else(|| crate::bool_error!("Invalid date"))
366 }
367 }
368
369 /// Creates a new #GDateTime corresponding to the given date and time in
370 /// UTC.
371 ///
372 /// This call is equivalent to calling g_date_time_new() with the time
373 /// zone returned by g_time_zone_new_utc().
374 /// ## `year`
375 /// the year component of the date
376 /// ## `month`
377 /// the month component of the date
378 /// ## `day`
379 /// the day component of the date
380 /// ## `hour`
381 /// the hour component of the date
382 /// ## `minute`
383 /// the minute component of the date
384 /// ## `seconds`
385 /// the number of seconds past the minute
386 ///
387 /// # Returns
388 ///
389 /// a #GDateTime, or [`None`]
390 #[doc(alias = "g_date_time_new_utc")]
391 #[doc(alias = "new_utc")]
392 pub fn from_utc(
393 year: i32,
394 month: i32,
395 day: i32,
396 hour: i32,
397 minute: i32,
398 seconds: f64,
399 ) -> Result<DateTime, BoolError> {
400 unsafe {
401 Option::<_>::from_glib_full(ffi::g_date_time_new_utc(
402 year, month, day, hour, minute, seconds,
403 ))
404 .ok_or_else(|| crate::bool_error!("Invalid date"))
405 }
406 }
407
408 /// Creates a copy of @self and adds the specified timespan to the copy.
409 /// ## `timespan`
410 /// a #GTimeSpan
411 ///
412 /// # Returns
413 ///
414 /// the newly created #GDateTime which
415 /// should be freed with g_date_time_unref(), or [`None`]
416 #[doc(alias = "g_date_time_add")]
417 pub fn add(&self, timespan: TimeSpan) -> Result<DateTime, BoolError> {
418 unsafe {
419 Option::<_>::from_glib_full(ffi::g_date_time_add(
420 self.to_glib_none().0,
421 timespan.into_glib(),
422 ))
423 .ok_or_else(|| crate::bool_error!("Invalid date"))
424 }
425 }
426
427 /// Creates a copy of @self and adds the specified number of days to the
428 /// copy. Add negative values to subtract days.
429 /// ## `days`
430 /// the number of days
431 ///
432 /// # Returns
433 ///
434 /// the newly created #GDateTime which
435 /// should be freed with g_date_time_unref(), or [`None`]
436 #[doc(alias = "g_date_time_add_days")]
437 pub fn add_days(&self, days: i32) -> Result<DateTime, BoolError> {
438 unsafe {
439 Option::<_>::from_glib_full(ffi::g_date_time_add_days(self.to_glib_none().0, days))
440 .ok_or_else(|| crate::bool_error!("Invalid date"))
441 }
442 }
443
444 /// Creates a new #GDateTime adding the specified values to the current date and
445 /// time in @self. Add negative values to subtract.
446 /// ## `years`
447 /// the number of years to add
448 /// ## `months`
449 /// the number of months to add
450 /// ## `days`
451 /// the number of days to add
452 /// ## `hours`
453 /// the number of hours to add
454 /// ## `minutes`
455 /// the number of minutes to add
456 /// ## `seconds`
457 /// the number of seconds to add
458 ///
459 /// # Returns
460 ///
461 /// the newly created #GDateTime which
462 /// should be freed with g_date_time_unref(), or [`None`]
463 #[doc(alias = "g_date_time_add_full")]
464 pub fn add_full(
465 &self,
466 years: i32,
467 months: i32,
468 days: i32,
469 hours: i32,
470 minutes: i32,
471 seconds: f64,
472 ) -> Result<DateTime, BoolError> {
473 unsafe {
474 Option::<_>::from_glib_full(ffi::g_date_time_add_full(
475 self.to_glib_none().0,
476 years,
477 months,
478 days,
479 hours,
480 minutes,
481 seconds,
482 ))
483 .ok_or_else(|| crate::bool_error!("Invalid date"))
484 }
485 }
486
487 /// Creates a copy of @self and adds the specified number of hours.
488 /// Add negative values to subtract hours.
489 /// ## `hours`
490 /// the number of hours to add
491 ///
492 /// # Returns
493 ///
494 /// the newly created #GDateTime which
495 /// should be freed with g_date_time_unref(), or [`None`]
496 #[doc(alias = "g_date_time_add_hours")]
497 pub fn add_hours(&self, hours: i32) -> Result<DateTime, BoolError> {
498 unsafe {
499 Option::<_>::from_glib_full(ffi::g_date_time_add_hours(self.to_glib_none().0, hours))
500 .ok_or_else(|| crate::bool_error!("Invalid date"))
501 }
502 }
503
504 /// Creates a copy of @self adding the specified number of minutes.
505 /// Add negative values to subtract minutes.
506 /// ## `minutes`
507 /// the number of minutes to add
508 ///
509 /// # Returns
510 ///
511 /// the newly created #GDateTime which
512 /// should be freed with g_date_time_unref(), or [`None`]
513 #[doc(alias = "g_date_time_add_minutes")]
514 pub fn add_minutes(&self, minutes: i32) -> Result<DateTime, BoolError> {
515 unsafe {
516 Option::<_>::from_glib_full(ffi::g_date_time_add_minutes(
517 self.to_glib_none().0,
518 minutes,
519 ))
520 .ok_or_else(|| crate::bool_error!("Invalid date"))
521 }
522 }
523
524 /// Creates a copy of @self and adds the specified number of months to the
525 /// copy. Add negative values to subtract months.
526 ///
527 /// The day of the month of the resulting #GDateTime is clamped to the number
528 /// of days in the updated calendar month. For example, if adding 1 month to
529 /// 31st January 2018, the result would be 28th February 2018. In 2020 (a leap
530 /// year), the result would be 29th February.
531 /// ## `months`
532 /// the number of months
533 ///
534 /// # Returns
535 ///
536 /// the newly created #GDateTime which
537 /// should be freed with g_date_time_unref(), or [`None`]
538 #[doc(alias = "g_date_time_add_months")]
539 pub fn add_months(&self, months: i32) -> Result<DateTime, BoolError> {
540 unsafe {
541 Option::<_>::from_glib_full(ffi::g_date_time_add_months(self.to_glib_none().0, months))
542 .ok_or_else(|| crate::bool_error!("Invalid date"))
543 }
544 }
545
546 /// Creates a copy of @self and adds the specified number of seconds.
547 /// Add negative values to subtract seconds.
548 /// ## `seconds`
549 /// the number of seconds to add
550 ///
551 /// # Returns
552 ///
553 /// the newly created #GDateTime which
554 /// should be freed with g_date_time_unref(), or [`None`]
555 #[doc(alias = "g_date_time_add_seconds")]
556 pub fn add_seconds(&self, seconds: f64) -> Result<DateTime, BoolError> {
557 unsafe {
558 Option::<_>::from_glib_full(ffi::g_date_time_add_seconds(
559 self.to_glib_none().0,
560 seconds,
561 ))
562 .ok_or_else(|| crate::bool_error!("Invalid date"))
563 }
564 }
565
566 /// Creates a copy of @self and adds the specified number of weeks to the
567 /// copy. Add negative values to subtract weeks.
568 /// ## `weeks`
569 /// the number of weeks
570 ///
571 /// # Returns
572 ///
573 /// the newly created #GDateTime which
574 /// should be freed with g_date_time_unref(), or [`None`]
575 #[doc(alias = "g_date_time_add_weeks")]
576 pub fn add_weeks(&self, weeks: i32) -> Result<DateTime, BoolError> {
577 unsafe {
578 Option::<_>::from_glib_full(ffi::g_date_time_add_weeks(self.to_glib_none().0, weeks))
579 .ok_or_else(|| crate::bool_error!("Invalid date"))
580 }
581 }
582
583 /// Creates a copy of @self and adds the specified number of years to the
584 /// copy. Add negative values to subtract years.
585 ///
586 /// As with g_date_time_add_months(), if the resulting date would be 29th
587 /// February on a non-leap year, the day will be clamped to 28th February.
588 /// ## `years`
589 /// the number of years
590 ///
591 /// # Returns
592 ///
593 /// the newly created #GDateTime which
594 /// should be freed with g_date_time_unref(), or [`None`]
595 #[doc(alias = "g_date_time_add_years")]
596 pub fn add_years(&self, years: i32) -> Result<DateTime, BoolError> {
597 unsafe {
598 Option::<_>::from_glib_full(ffi::g_date_time_add_years(self.to_glib_none().0, years))
599 .ok_or_else(|| crate::bool_error!("Invalid date"))
600 }
601 }
602
603 #[doc(alias = "g_date_time_compare")]
604 fn compare(&self, dt2: &DateTime) -> i32 {
605 unsafe {
606 ffi::g_date_time_compare(
607 ToGlibPtr::<*mut ffi::GDateTime>::to_glib_none(self).0 as ffi::gconstpointer,
608 ToGlibPtr::<*mut ffi::GDateTime>::to_glib_none(dt2).0 as ffi::gconstpointer,
609 )
610 }
611 }
612
613 /// Calculates the difference in time between @self and @begin. The
614 /// #GTimeSpan that is returned is effectively @self - @begin (ie:
615 /// positive if the first parameter is larger).
616 /// ## `begin`
617 /// a #GDateTime
618 ///
619 /// # Returns
620 ///
621 /// the difference between the two #GDateTime, as a time
622 /// span expressed in microseconds.
623 #[doc(alias = "g_date_time_difference")]
624 pub fn difference(&self, begin: &DateTime) -> TimeSpan {
625 unsafe {
626 from_glib(ffi::g_date_time_difference(
627 self.to_glib_none().0,
628 begin.to_glib_none().0,
629 ))
630 }
631 }
632
633 #[doc(alias = "g_date_time_equal")]
634 fn equal(&self, dt2: &DateTime) -> bool {
635 unsafe {
636 from_glib(ffi::g_date_time_equal(
637 ToGlibPtr::<*mut ffi::GDateTime>::to_glib_none(self).0 as ffi::gconstpointer,
638 ToGlibPtr::<*mut ffi::GDateTime>::to_glib_none(dt2).0 as ffi::gconstpointer,
639 ))
640 }
641 }
642
643 /// use ``u`` instead.
644 /// - ``x``: the preferred date representation for the current locale without
645 /// the time
646 /// - ``X``: the preferred time representation for the current locale without
647 /// the date
648 /// - ``y``: the year as a decimal number without the century
649 /// - ``Y``: the year as a decimal number including the century
650 /// - ``z``: the time zone as an offset from UTC (`+hhmm`)
651 /// - `%:z`: the time zone as an offset from UTC (`+hh:mm`).
652 /// This is a gnulib `strftime()` extension. Since: 2.38
653 /// - `%::z`: the time zone as an offset from UTC (`+hh:mm:ss`). This is a
654 /// gnulib `strftime()` extension. Since: 2.38
655 /// - `%:::z`: the time zone as an offset from UTC, with `:` to necessary
656 /// precision (e.g., `-04`, `+05:30`). This is a gnulib `strftime()` extension. Since: 2.38
657 /// - ``Z``: the time zone or name or abbreviation
658 /// - `%%`: a literal `%` character
659 ///
660 /// Some conversion specifications can be modified by preceding the
661 /// conversion specifier by one or more modifier characters.
662 ///
663 /// The following modifiers are supported for many of the numeric
664 /// conversions:
665 ///
666 /// - `O`: Use alternative numeric symbols, if the current locale supports those.
667 /// - `_`: Pad a numeric result with spaces. This overrides the default padding
668 /// for the specifier.
669 /// - `-`: Do not pad a numeric result. This overrides the default padding
670 /// for the specifier.
671 /// - `0`: Pad a numeric result with zeros. This overrides the default padding
672 /// for the specifier.
673 ///
674 /// The following modifiers are supported for many of the alphabetic conversions:
675 ///
676 /// - `^`: Use upper case if possible. This is a gnulib `strftime()` extension.
677 /// Since: 2.80
678 /// - `#`: Use opposite case if possible. This is a gnulib `strftime()`
679 /// extension. Since: 2.80
680 ///
681 /// Additionally, when `O` is used with `B`, `b`, or `h`, it produces the alternative
682 /// form of a month name. The alternative form should be used when the month
683 /// name is used without a day number (e.g., standalone). It is required in
684 /// some languages (Baltic, Slavic, Greek, and more) due to their grammatical
685 /// rules. For other languages there is no difference. ``OB`` is a GNU and BSD
686 /// `strftime()` extension expected to be added to the future POSIX specification,
687 /// ``Ob`` and ``Oh`` are GNU `strftime()` extensions. Since: 2.56
688 ///
689 /// Since GLib 2.80, when `E` is used with ``c``, ``C``, ``x``, ``X``, ``y`` or ``Y``,
690 /// the date is formatted using an alternate era representation specific to the
691 /// locale. This is typically used for the Thai solar calendar or Japanese era
692 /// names, for example.
693 ///
694 /// - ``Ec``: the preferred date and time representation for the current locale,
695 /// using the alternate era representation
696 /// - ``EC``: the name of the era
697 /// - ``Ex``: the preferred date representation for the current locale without
698 /// the time, using the alternate era representation
699 /// - ``EX``: the preferred time representation for the current locale without
700 /// the date, using the alternate era representation
701 /// - ``Ey``: the year since the beginning of the era denoted by the ``EC``
702 /// specifier
703 /// - ``EY``: the full alternative year representation
704 /// ## `format`
705 /// a valid UTF-8 string, containing the format for the
706 /// #GDateTime
707 ///
708 /// # Returns
709 ///
710 /// a newly allocated string formatted to
711 /// the requested format or [`None`] in the case that there was an error (such
712 /// as a format specifier not being supported in the current locale). The
713 /// string should be freed with g_free().
714 #[doc(alias = "g_date_time_format")]
715 pub fn format(&self, format: &str) -> Result<crate::GString, BoolError> {
716 unsafe {
717 Option::<_>::from_glib_full(ffi::g_date_time_format(
718 self.to_glib_none().0,
719 format.to_glib_none().0,
720 ))
721 .ok_or_else(|| crate::bool_error!("Invalid date"))
722 }
723 }
724
725 /// Format @self in [ISO 8601 format](https://en.wikipedia.org/wiki/ISO_8601),
726 /// including the date, time and time zone, and return that as a UTF-8 encoded
727 /// string.
728 ///
729 /// Since GLib 2.66, this will output to sub-second precision if needed.
730 ///
731 /// # Returns
732 ///
733 /// a newly allocated string formatted in
734 /// ISO 8601 format or [`None`] in the case that there was an error. The string
735 /// should be freed with g_free().
736 #[cfg(feature = "v2_62")]
737 #[cfg_attr(docsrs, doc(cfg(feature = "v2_62")))]
738 #[doc(alias = "g_date_time_format_iso8601")]
739 pub fn format_iso8601(&self) -> Result<crate::GString, BoolError> {
740 unsafe {
741 Option::<_>::from_glib_full(ffi::g_date_time_format_iso8601(self.to_glib_none().0))
742 .ok_or_else(|| crate::bool_error!("Invalid date"))
743 }
744 }
745
746 /// Retrieves the day of the month represented by @self in the gregorian
747 /// calendar.
748 ///
749 /// # Returns
750 ///
751 /// the day of the month
752 #[doc(alias = "g_date_time_get_day_of_month")]
753 #[doc(alias = "get_day_of_month")]
754 pub fn day_of_month(&self) -> i32 {
755 unsafe { ffi::g_date_time_get_day_of_month(self.to_glib_none().0) }
756 }
757
758 /// Retrieves the ISO 8601 day of the week on which @self falls (1 is
759 /// Monday, 2 is Tuesday... 7 is Sunday).
760 ///
761 /// # Returns
762 ///
763 /// the day of the week
764 #[doc(alias = "g_date_time_get_day_of_week")]
765 #[doc(alias = "get_day_of_week")]
766 pub fn day_of_week(&self) -> i32 {
767 unsafe { ffi::g_date_time_get_day_of_week(self.to_glib_none().0) }
768 }
769
770 /// Retrieves the day of the year represented by @self in the Gregorian
771 /// calendar.
772 ///
773 /// # Returns
774 ///
775 /// the day of the year
776 #[doc(alias = "g_date_time_get_day_of_year")]
777 #[doc(alias = "get_day_of_year")]
778 pub fn day_of_year(&self) -> i32 {
779 unsafe { ffi::g_date_time_get_day_of_year(self.to_glib_none().0) }
780 }
781
782 /// Retrieves the hour of the day represented by @self
783 ///
784 /// # Returns
785 ///
786 /// the hour of the day
787 #[doc(alias = "g_date_time_get_hour")]
788 #[doc(alias = "get_hour")]
789 pub fn hour(&self) -> i32 {
790 unsafe { ffi::g_date_time_get_hour(self.to_glib_none().0) }
791 }
792
793 /// Retrieves the microsecond of the date represented by @self
794 ///
795 /// # Returns
796 ///
797 /// the microsecond of the second
798 #[doc(alias = "g_date_time_get_microsecond")]
799 #[doc(alias = "get_microsecond")]
800 pub fn microsecond(&self) -> i32 {
801 unsafe { ffi::g_date_time_get_microsecond(self.to_glib_none().0) }
802 }
803
804 /// Retrieves the minute of the hour represented by @self
805 ///
806 /// # Returns
807 ///
808 /// the minute of the hour
809 #[doc(alias = "g_date_time_get_minute")]
810 #[doc(alias = "get_minute")]
811 pub fn minute(&self) -> i32 {
812 unsafe { ffi::g_date_time_get_minute(self.to_glib_none().0) }
813 }
814
815 /// Retrieves the month of the year represented by @self in the Gregorian
816 /// calendar.
817 ///
818 /// # Returns
819 ///
820 /// the month represented by @self
821 #[doc(alias = "g_date_time_get_month")]
822 #[doc(alias = "get_month")]
823 pub fn month(&self) -> i32 {
824 unsafe { ffi::g_date_time_get_month(self.to_glib_none().0) }
825 }
826
827 /// Retrieves the second of the minute represented by @self
828 ///
829 /// # Returns
830 ///
831 /// the second represented by @self
832 #[doc(alias = "g_date_time_get_second")]
833 #[doc(alias = "get_second")]
834 pub fn second(&self) -> i32 {
835 unsafe { ffi::g_date_time_get_second(self.to_glib_none().0) }
836 }
837
838 /// Retrieves the number of seconds since the start of the last minute,
839 /// including the fractional part.
840 ///
841 /// # Returns
842 ///
843 /// the number of seconds
844 #[doc(alias = "g_date_time_get_seconds")]
845 #[doc(alias = "get_seconds")]
846 pub fn seconds(&self) -> f64 {
847 unsafe { ffi::g_date_time_get_seconds(self.to_glib_none().0) }
848 }
849
850 /// Get the time zone for this @self.
851 ///
852 /// # Returns
853 ///
854 /// the time zone
855 #[cfg(feature = "v2_58")]
856 #[cfg_attr(docsrs, doc(cfg(feature = "v2_58")))]
857 #[doc(alias = "g_date_time_get_timezone")]
858 #[doc(alias = "get_timezone")]
859 pub fn timezone(&self) -> TimeZone {
860 unsafe { from_glib_none(ffi::g_date_time_get_timezone(self.to_glib_none().0)) }
861 }
862
863 /// Determines the time zone abbreviation to be used at the time and in
864 /// the time zone of @self.
865 ///
866 /// For example, in Toronto this is currently "EST" during the winter
867 /// months and "EDT" during the summer months when daylight savings
868 /// time is in effect.
869 ///
870 /// # Returns
871 ///
872 /// the time zone abbreviation. The returned
873 /// string is owned by the #GDateTime and it should not be
874 /// modified or freed
875 #[doc(alias = "g_date_time_get_timezone_abbreviation")]
876 #[doc(alias = "get_timezone_abbreviation")]
877 pub fn timezone_abbreviation(&self) -> crate::GString {
878 unsafe {
879 from_glib_none(ffi::g_date_time_get_timezone_abbreviation(
880 self.to_glib_none().0,
881 ))
882 }
883 }
884
885 /// Determines the offset to UTC in effect at the time and in the time
886 /// zone of @self.
887 ///
888 /// The offset is the number of microseconds that you add to UTC time to
889 /// arrive at local time for the time zone (ie: negative numbers for time
890 /// zones west of GMT, positive numbers for east).
891 ///
892 /// If @self represents UTC time, then the offset is always zero.
893 ///
894 /// # Returns
895 ///
896 /// the number of microseconds that should be added to UTC to
897 /// get the local time
898 #[doc(alias = "g_date_time_get_utc_offset")]
899 #[doc(alias = "get_utc_offset")]
900 pub fn utc_offset(&self) -> TimeSpan {
901 unsafe { from_glib(ffi::g_date_time_get_utc_offset(self.to_glib_none().0)) }
902 }
903
904 /// Returns the ISO 8601 week-numbering year in which the week containing
905 /// @self falls.
906 ///
907 /// This function, taken together with g_date_time_get_week_of_year() and
908 /// g_date_time_get_day_of_week() can be used to determine the full ISO
909 /// week date on which @self falls.
910 ///
911 /// This is usually equal to the normal Gregorian year (as returned by
912 /// g_date_time_get_year()), except as detailed below:
913 ///
914 /// For Thursday, the week-numbering year is always equal to the usual
915 /// calendar year. For other days, the number is such that every day
916 /// within a complete week (Monday to Sunday) is contained within the
917 /// same week-numbering year.
918 ///
919 /// For Monday, Tuesday and Wednesday occurring near the end of the year,
920 /// this may mean that the week-numbering year is one greater than the
921 /// calendar year (so that these days have the same week-numbering year
922 /// as the Thursday occurring early in the next year).
923 ///
924 /// For Friday, Saturday and Sunday occurring near the start of the year,
925 /// this may mean that the week-numbering year is one less than the
926 /// calendar year (so that these days have the same week-numbering year
927 /// as the Thursday occurring late in the previous year).
928 ///
929 /// An equivalent description is that the week-numbering year is equal to
930 /// the calendar year containing the majority of the days in the current
931 /// week (Monday to Sunday).
932 ///
933 /// Note that January 1 0001 in the proleptic Gregorian calendar is a
934 /// Monday, so this function never returns 0.
935 ///
936 /// # Returns
937 ///
938 /// the ISO 8601 week-numbering year for @self
939 #[doc(alias = "g_date_time_get_week_numbering_year")]
940 #[doc(alias = "get_week_numbering_year")]
941 pub fn week_numbering_year(&self) -> i32 {
942 unsafe { ffi::g_date_time_get_week_numbering_year(self.to_glib_none().0) }
943 }
944
945 /// Returns the ISO 8601 week number for the week containing @self.
946 /// The ISO 8601 week number is the same for every day of the week (from
947 /// Moday through Sunday). That can produce some unusual results
948 /// (described below).
949 ///
950 /// The first week of the year is week 1. This is the week that contains
951 /// the first Thursday of the year. Equivalently, this is the first week
952 /// that has more than 4 of its days falling within the calendar year.
953 ///
954 /// The value 0 is never returned by this function. Days contained
955 /// within a year but occurring before the first ISO 8601 week of that
956 /// year are considered as being contained in the last week of the
957 /// previous year. Similarly, the final days of a calendar year may be
958 /// considered as being part of the first ISO 8601 week of the next year
959 /// if 4 or more days of that week are contained within the new year.
960 ///
961 /// # Returns
962 ///
963 /// the ISO 8601 week number for @self.
964 #[doc(alias = "g_date_time_get_week_of_year")]
965 #[doc(alias = "get_week_of_year")]
966 pub fn week_of_year(&self) -> i32 {
967 unsafe { ffi::g_date_time_get_week_of_year(self.to_glib_none().0) }
968 }
969
970 /// Retrieves the year represented by @self in the Gregorian calendar.
971 ///
972 /// # Returns
973 ///
974 /// the year represented by @self
975 #[doc(alias = "g_date_time_get_year")]
976 #[doc(alias = "get_year")]
977 pub fn year(&self) -> i32 {
978 unsafe { ffi::g_date_time_get_year(self.to_glib_none().0) }
979 }
980
981 /// Retrieves the Gregorian day, month, and year of a given #GDateTime.
982 ///
983 /// # Returns
984 ///
985 ///
986 /// ## `year`
987 /// the return location for the gregorian year, or [`None`].
988 ///
989 /// ## `month`
990 /// the return location for the month of the year, or [`None`].
991 ///
992 /// ## `day`
993 /// the return location for the day of the month, or [`None`].
994 #[doc(alias = "g_date_time_get_ymd")]
995 #[doc(alias = "get_ymd")]
996 pub fn ymd(&self) -> (i32, i32, i32) {
997 unsafe {
998 let mut year = std::mem::MaybeUninit::uninit();
999 let mut month = std::mem::MaybeUninit::uninit();
1000 let mut day = std::mem::MaybeUninit::uninit();
1001 ffi::g_date_time_get_ymd(
1002 self.to_glib_none().0,
1003 year.as_mut_ptr(),
1004 month.as_mut_ptr(),
1005 day.as_mut_ptr(),
1006 );
1007 (year.assume_init(), month.assume_init(), day.assume_init())
1008 }
1009 }
1010
1011 #[doc(alias = "g_date_time_hash")]
1012 fn hash(&self) -> u32 {
1013 unsafe {
1014 ffi::g_date_time_hash(
1015 ToGlibPtr::<*mut ffi::GDateTime>::to_glib_none(self).0 as ffi::gconstpointer,
1016 )
1017 }
1018 }
1019
1020 /// Determines if daylight savings time is in effect at the time and in
1021 /// the time zone of @self.
1022 ///
1023 /// # Returns
1024 ///
1025 /// [`true`] if daylight savings time is in effect
1026 #[doc(alias = "g_date_time_is_daylight_savings")]
1027 pub fn is_daylight_savings(&self) -> bool {
1028 unsafe { from_glib(ffi::g_date_time_is_daylight_savings(self.to_glib_none().0)) }
1029 }
1030
1031 /// Creates a new #GDateTime corresponding to the same instant in time as
1032 /// @self, but in the local time zone.
1033 ///
1034 /// This call is equivalent to calling g_date_time_to_timezone() with the
1035 /// time zone returned by g_time_zone_new_local().
1036 ///
1037 /// # Returns
1038 ///
1039 /// the newly created #GDateTime which
1040 /// should be freed with g_date_time_unref(), or [`None`]
1041 #[doc(alias = "g_date_time_to_local")]
1042 pub fn to_local(&self) -> Result<DateTime, BoolError> {
1043 unsafe {
1044 Option::<_>::from_glib_full(ffi::g_date_time_to_local(self.to_glib_none().0))
1045 .ok_or_else(|| crate::bool_error!("Invalid date"))
1046 }
1047 }
1048
1049 //#[cfg_attr(feature = "v2_62", deprecated = "Since 2.62")]
1050 //#[allow(deprecated)]
1051 //#[doc(alias = "g_date_time_to_timeval")]
1052 //pub fn to_timeval(&self, tv: /*Ignored*/&mut TimeVal) -> bool {
1053 // unsafe { TODO: call ffi:g_date_time_to_timeval() }
1054 //}
1055
1056 /// Create a new #GDateTime corresponding to the same instant in time as
1057 /// @self, but in the time zone @tz.
1058 ///
1059 /// This call can fail in the case that the time goes out of bounds. For
1060 /// example, converting 0001-01-01 00:00:00 UTC to a time zone west of
1061 /// Greenwich will fail (due to the year 0 being out of range).
1062 /// ## `tz`
1063 /// the new #GTimeZone
1064 ///
1065 /// # Returns
1066 ///
1067 /// the newly created #GDateTime which
1068 /// should be freed with g_date_time_unref(), or [`None`]
1069 #[doc(alias = "g_date_time_to_timezone")]
1070 pub fn to_timezone(&self, tz: &TimeZone) -> Result<DateTime, BoolError> {
1071 unsafe {
1072 Option::<_>::from_glib_full(ffi::g_date_time_to_timezone(
1073 self.to_glib_none().0,
1074 tz.to_glib_none().0,
1075 ))
1076 .ok_or_else(|| crate::bool_error!("Invalid date"))
1077 }
1078 }
1079
1080 /// Gives the Unix time corresponding to @self, rounding down to the
1081 /// nearest second.
1082 ///
1083 /// Unix time is the number of seconds that have elapsed since 1970-01-01
1084 /// 00:00:00 UTC, regardless of the time zone associated with @self.
1085 ///
1086 /// # Returns
1087 ///
1088 /// the Unix time corresponding to @self
1089 #[doc(alias = "g_date_time_to_unix")]
1090 pub fn to_unix(&self) -> i64 {
1091 unsafe { ffi::g_date_time_to_unix(self.to_glib_none().0) }
1092 }
1093
1094 /// Gives the Unix time corresponding to @self, in microseconds.
1095 ///
1096 /// Unix time is the number of microseconds that have elapsed since 1970-01-01
1097 /// 00:00:00 UTC, regardless of the time zone associated with @self.
1098 ///
1099 /// # Returns
1100 ///
1101 /// the Unix time corresponding to @self
1102 #[cfg(feature = "v2_80")]
1103 #[cfg_attr(docsrs, doc(cfg(feature = "v2_80")))]
1104 #[doc(alias = "g_date_time_to_unix_usec")]
1105 pub fn to_unix_usec(&self) -> i64 {
1106 unsafe { ffi::g_date_time_to_unix_usec(self.to_glib_none().0) }
1107 }
1108
1109 /// Creates a new #GDateTime corresponding to the same instant in time as
1110 /// @self, but in UTC.
1111 ///
1112 /// This call is equivalent to calling g_date_time_to_timezone() with the
1113 /// time zone returned by g_time_zone_new_utc().
1114 ///
1115 /// # Returns
1116 ///
1117 /// the newly created #GDateTime which
1118 /// should be freed with g_date_time_unref(), or [`None`]
1119 #[doc(alias = "g_date_time_to_utc")]
1120 pub fn to_utc(&self) -> Result<DateTime, BoolError> {
1121 unsafe {
1122 Option::<_>::from_glib_full(ffi::g_date_time_to_utc(self.to_glib_none().0))
1123 .ok_or_else(|| crate::bool_error!("Invalid date"))
1124 }
1125 }
1126}
1127
1128impl PartialOrd for DateTime {
1129 #[inline]
1130 fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
1131 Some(self.cmp(other))
1132 }
1133}
1134
1135impl Ord for DateTime {
1136 #[inline]
1137 fn cmp(&self, other: &Self) -> std::cmp::Ordering {
1138 self.compare(other).cmp(&0)
1139 }
1140}
1141
1142impl PartialEq for DateTime {
1143 #[inline]
1144 fn eq(&self, other: &Self) -> bool {
1145 self.equal(other)
1146 }
1147}
1148
1149impl Eq for DateTime {}
1150
1151impl std::hash::Hash for DateTime {
1152 #[inline]
1153 fn hash<H>(&self, state: &mut H)
1154 where
1155 H: std::hash::Hasher,
1156 {
1157 std::hash::Hash::hash(&self.hash(), state)
1158 }
1159}
1160
1161unsafe impl Send for DateTime {}
1162unsafe impl Sync for DateTime {}