1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from gir-files (https://github.com/gtk-rs/gir-files)
// from gst-gir-files (https://gitlab.freedesktop.org/gstreamer/gir-files-rs.git)
// DO NOT EDIT

use crate::ffi;
use glib::translate::*;

glib::wrapper! {
    /// Struct to store date, time and timezone information altogether.
    /// [`DateTime`][crate::DateTime] is refcounted and immutable.
    ///
    /// Date information is handled using the [proleptic Gregorian calendar].
    ///
    /// Provides basic creation functions and accessor functions to its fields.
    ///
    /// [proleptic Gregorian calendar]: https://en.wikipedia.org/wiki/Proleptic_Gregorian_calendar
    #[derive()]
    pub struct DateTime(Shared<ffi::GstDateTime>);

    match fn {
        ref => |ptr| ffi::gst_date_time_ref(ptr),
        unref => |ptr| ffi::gst_date_time_unref(ptr),
        type_ => || ffi::gst_date_time_get_type(),
    }
}

impl DateTime {
    /// Creates a new [`DateTime`][crate::DateTime] from a [`glib::DateTime`][crate::glib::DateTime] object.
    /// ## `dt`
    /// the [`glib::DateTime`][crate::glib::DateTime].
    ///
    /// # Returns
    ///
    /// a newly created [`DateTime`][crate::DateTime],
    /// or [`None`] if `dt` is [`None`].
    #[doc(alias = "gst_date_time_new_from_g_date_time")]
    #[doc(alias = "new_from_g_date_time")]
    pub fn from_g_date_time(dt: glib::DateTime) -> DateTime {
        assert_initialized_main_thread!();
        unsafe { from_glib_full(ffi::gst_date_time_new_from_g_date_time(dt.into_glib_ptr())) }
    }

    /// Tries to parse common variants of ISO-8601 datetime strings into a
    /// [`DateTime`][crate::DateTime]. Possible input formats are (for example):
    /// `2012-06-30T22:46:43Z`, `2012`, `2012-06`, `2012-06-30`, `2012-06-30T22:46:43-0430`,
    /// `2012-06-30T22:46Z`, `2012-06-30T22:46-0430`, `2012-06-30 22:46`,
    /// `2012-06-30 22:46:43`, `2012-06-00`, `2012-00-00`, `2012-00-30`, `22:46:43Z`, `22:46Z`,
    /// `22:46:43-0430`, `22:46-0430`, `22:46:30`, `22:46`
    /// If no date is provided, it is assumed to be "today" in the timezone
    /// provided (if any), otherwise UTC.
    /// ## `string`
    /// ISO 8601-formatted datetime string.
    ///
    /// # Returns
    ///
    /// a newly created [`DateTime`][crate::DateTime],
    /// or [`None`] on error
    #[doc(alias = "gst_date_time_new_from_iso8601_string")]
    #[doc(alias = "new_from_iso8601_string")]
    pub fn from_iso8601_string(string: &str) -> Result<DateTime, glib::BoolError> {
        assert_initialized_main_thread!();
        unsafe {
            Option::<_>::from_glib_full(ffi::gst_date_time_new_from_iso8601_string(
                string.to_glib_none().0,
            ))
            .ok_or_else(|| glib::bool_error!("Failed to create DateTime from ISO-8601 string"))
        }
    }

    /// Creates a new [`DateTime`][crate::DateTime] using the time since Jan 1, 1970 specified by
    /// `secs`. The [`DateTime`][crate::DateTime] is in the local timezone.
    /// ## `secs`
    /// seconds from the Unix epoch
    ///
    /// # Returns
    ///
    /// the newly created [`DateTime`][crate::DateTime],
    /// or [`None`] on error.
    #[doc(alias = "gst_date_time_new_from_unix_epoch_local_time")]
    #[doc(alias = "new_from_unix_epoch_local_time")]
    pub fn from_unix_epoch_local_time(secs: i64) -> Result<DateTime, glib::BoolError> {
        assert_initialized_main_thread!();
        unsafe {
            Option::<_>::from_glib_full(ffi::gst_date_time_new_from_unix_epoch_local_time(secs))
                .ok_or_else(|| glib::bool_error!("Can't create DateTime from UNIX epoch"))
        }
    }

    /// Creates a new [`DateTime`][crate::DateTime] using the time since Jan 1, 1970 specified by
    /// `usecs`. The [`DateTime`][crate::DateTime] is in the local timezone.
    /// ## `usecs`
    /// microseconds from the Unix epoch
    ///
    /// # Returns
    ///
    /// a newly created [`DateTime`][crate::DateTime], or [`None`]
    /// on error.
    #[cfg(feature = "v1_18")]
    #[cfg_attr(docsrs, doc(cfg(feature = "v1_18")))]
    #[doc(alias = "gst_date_time_new_from_unix_epoch_local_time_usecs")]
    #[doc(alias = "new_from_unix_epoch_local_time_usecs")]
    pub fn from_unix_epoch_local_time_usecs(usecs: i64) -> Result<DateTime, glib::BoolError> {
        assert_initialized_main_thread!();
        unsafe {
            Option::<_>::from_glib_full(ffi::gst_date_time_new_from_unix_epoch_local_time_usecs(
                usecs,
            ))
            .ok_or_else(|| glib::bool_error!("Can't create DateTime from UNIX epoch"))
        }
    }

    /// Creates a new [`DateTime`][crate::DateTime] using the time since Jan 1, 1970 specified by
    /// `secs`. The [`DateTime`][crate::DateTime] is in the UTC timezone.
    /// ## `secs`
    /// seconds from the Unix epoch
    ///
    /// # Returns
    ///
    /// the newly created [`DateTime`][crate::DateTime],
    /// or [`None`] on error.
    #[doc(alias = "gst_date_time_new_from_unix_epoch_utc")]
    #[doc(alias = "new_from_unix_epoch_utc")]
    pub fn from_unix_epoch_utc(secs: i64) -> Result<DateTime, glib::BoolError> {
        assert_initialized_main_thread!();
        unsafe {
            Option::<_>::from_glib_full(ffi::gst_date_time_new_from_unix_epoch_utc(secs))
                .ok_or_else(|| glib::bool_error!("Can't create DateTime from UNIX epoch"))
        }
    }

    /// Creates a new [`DateTime`][crate::DateTime] using the time since Jan 1, 1970 specified by
    /// `usecs`. The [`DateTime`][crate::DateTime] is in UTC.
    /// ## `usecs`
    /// microseconds from the Unix epoch
    ///
    /// # Returns
    ///
    /// a newly created [`DateTime`][crate::DateTime], or [`None`]
    /// on error.
    #[cfg(feature = "v1_18")]
    #[cfg_attr(docsrs, doc(cfg(feature = "v1_18")))]
    #[doc(alias = "gst_date_time_new_from_unix_epoch_utc_usecs")]
    #[doc(alias = "new_from_unix_epoch_utc_usecs")]
    pub fn from_unix_epoch_utc_usecs(usecs: i64) -> Result<DateTime, glib::BoolError> {
        assert_initialized_main_thread!();
        unsafe {
            Option::<_>::from_glib_full(ffi::gst_date_time_new_from_unix_epoch_utc_usecs(usecs))
                .ok_or_else(|| glib::bool_error!("Can't create DateTime from UNIX epoch"))
        }
    }

    /// Creates a new [`DateTime`][crate::DateTime] representing the current date and time.
    ///
    /// # Returns
    ///
    /// the newly created [`DateTime`][crate::DateTime] which should
    ///  be freed with `gst_date_time_unref()`, or [`None`] on error.
    #[doc(alias = "gst_date_time_new_now_local_time")]
    pub fn new_now_local_time() -> Option<DateTime> {
        assert_initialized_main_thread!();
        unsafe { from_glib_full(ffi::gst_date_time_new_now_local_time()) }
    }

    /// Creates a new [`DateTime`][crate::DateTime] that represents the current instant at Universal
    /// coordinated time.
    ///
    /// # Returns
    ///
    /// the newly created [`DateTime`][crate::DateTime] which should
    ///  be freed with `gst_date_time_unref()`, or [`None`] on error.
    #[doc(alias = "gst_date_time_new_now_utc")]
    pub fn new_now_utc() -> Option<DateTime> {
        assert_initialized_main_thread!();
        unsafe { from_glib_full(ffi::gst_date_time_new_now_utc()) }
    }

    /// Returns the year of this [`DateTime`][crate::DateTime].
    /// Call [`has_year()`][Self::has_year()] before, to avoid warnings.
    ///
    /// # Returns
    ///
    /// The year of this [`DateTime`][crate::DateTime]
    #[doc(alias = "gst_date_time_get_year")]
    #[doc(alias = "get_year")]
    pub fn year(&self) -> i32 {
        unsafe { ffi::gst_date_time_get_year(self.to_glib_none().0) }
    }

    ///
    /// # Returns
    ///
    /// [`true`] if `self`<!-- -->'s day field is set, otherwise [`false`]
    #[doc(alias = "gst_date_time_has_day")]
    pub fn has_day(&self) -> bool {
        unsafe { from_glib(ffi::gst_date_time_has_day(self.to_glib_none().0)) }
    }

    ///
    /// # Returns
    ///
    /// [`true`] if `self`<!-- -->'s month field is set, otherwise [`false`]
    #[doc(alias = "gst_date_time_has_month")]
    pub fn has_month(&self) -> bool {
        unsafe { from_glib(ffi::gst_date_time_has_month(self.to_glib_none().0)) }
    }

    ///
    /// # Returns
    ///
    /// [`true`] if `self`<!-- -->'s second field is set, otherwise [`false`]
    #[doc(alias = "gst_date_time_has_second")]
    pub fn has_second(&self) -> bool {
        unsafe { from_glib(ffi::gst_date_time_has_second(self.to_glib_none().0)) }
    }

    ///
    /// # Returns
    ///
    /// [`true`] if `self`<!-- -->'s hour and minute fields are set,
    ///  otherwise [`false`]
    #[doc(alias = "gst_date_time_has_time")]
    pub fn has_time(&self) -> bool {
        unsafe { from_glib(ffi::gst_date_time_has_time(self.to_glib_none().0)) }
    }

    ///
    /// # Returns
    ///
    /// [`true`] if `self`<!-- -->'s year field is set (which should always
    ///  be the case), otherwise [`false`]
    #[doc(alias = "gst_date_time_has_year")]
    pub fn has_year(&self) -> bool {
        unsafe { from_glib(ffi::gst_date_time_has_year(self.to_glib_none().0)) }
    }

    /// Creates a new [`glib::DateTime`][crate::glib::DateTime] from a fully defined [`DateTime`][crate::DateTime] object.
    ///
    /// # Returns
    ///
    /// a newly created [`glib::DateTime`][crate::glib::DateTime], or
    /// [`None`] on error or if `self` does not have a year, month, day, hour,
    /// minute and second.
    #[doc(alias = "gst_date_time_to_g_date_time")]
    pub fn to_g_date_time(&self) -> Result<glib::DateTime, glib::BoolError> {
        unsafe {
            Option::<_>::from_glib_full(ffi::gst_date_time_to_g_date_time(self.to_glib_none().0))
                .ok_or_else(|| glib::bool_error!("Can't create glib::DateTime from DateTime"))
        }
    }

    /// Create a minimal string compatible with ISO-8601. Possible output formats
    /// are (for example): `2012`, `2012-06`, `2012-06-23`, `2012-06-23T23:30Z`,
    /// `2012-06-23T23:30+0100`, `2012-06-23T23:30:59Z`, `2012-06-23T23:30:59+0100`
    ///
    /// # Returns
    ///
    /// a newly allocated string formatted according
    ///  to ISO 8601 and only including the datetime fields that are
    ///  valid, or [`None`] in case there was an error.
    #[doc(alias = "gst_date_time_to_iso8601_string")]
    pub fn to_iso8601_string(&self) -> Result<glib::GString, glib::BoolError> {
        unsafe {
            Option::<_>::from_glib_full(ffi::gst_date_time_to_iso8601_string(self.to_glib_none().0))
                .ok_or_else(|| glib::bool_error!("Failed to create ISO-8601 string from DateTime"))
        }
    }
}

unsafe impl Send for DateTime {}
unsafe impl Sync for DateTime {}