2 * Copyright (C) 2010 Thiago Santos <thiago.sousa.santos@collabora.co.uk>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
24 #include "gst_private.h"
25 #include "glib-compat-private.h"
26 #include "gstdatetime.h"
35 * @short_description: A date, time and timezone structure
37 * Struct to store date, time and timezone information altogether.
38 * #GstDateTime is refcounted and immutable.
40 * Date information is handled using the proleptic Gregorian calendar.
42 * Provides basic creation functions and accessor functions to its fields.
47 GST_DATE_TIME_FIELDS_INVALID = 0,
48 GST_DATE_TIME_FIELDS_Y, /* have year */
49 GST_DATE_TIME_FIELDS_YM, /* have year and month */
50 GST_DATE_TIME_FIELDS_YMD, /* have year, month and day */
51 GST_DATE_TIME_FIELDS_YMD_HM,
52 GST_DATE_TIME_FIELDS_YMD_HMS
53 /* Note: if we ever add more granularity here, e.g. for microsecs,
54 * the compare function will need updating */
59 GstMiniObject mini_object;
63 GstDateTimeFields fields;
66 GType _gst_date_time_type = 0;
67 GST_DEFINE_MINI_OBJECT_TYPE (GstDateTime, gst_date_time);
69 static void gst_date_time_free (GstDateTime * datetime);
72 * gst_date_time_new_from_g_date_time:
73 * @dt: (transfer full): the #GDateTime. The new #GstDateTime takes ownership.
75 * Creates a new #GstDateTime from a #GDateTime object.
77 * Free-function: gst_date_time_unref
79 * Returns: (transfer full) (nullable): a newly created #GstDateTime,
83 gst_date_time_new_from_g_date_time (GDateTime * dt)
90 gst_dt = g_slice_new (GstDateTime);
92 gst_mini_object_init (GST_MINI_OBJECT_CAST (gst_dt), 0, GST_TYPE_DATE_TIME,
93 NULL, NULL, (GstMiniObjectFreeFunction) gst_date_time_free);
95 gst_dt->datetime = dt;
96 gst_dt->fields = GST_DATE_TIME_FIELDS_YMD_HMS;
101 * gst_date_time_to_g_date_time:
102 * @datetime: GstDateTime.
104 * Creates a new #GDateTime from a fully defined #GstDateTime object.
106 * Free-function: g_date_time_unref
108 * Returns: (transfer full) (nullable): a newly created #GDateTime, or
112 gst_date_time_to_g_date_time (GstDateTime * datetime)
114 g_return_val_if_fail (datetime != NULL, NULL);
116 if (datetime->fields != GST_DATE_TIME_FIELDS_YMD_HMS)
119 return g_date_time_add (datetime->datetime, 0);
123 * gst_date_time_has_year:
124 * @datetime: a #GstDateTime
126 * Returns: %TRUE if @datetime<!-- -->'s year field is set (which should always
127 * be the case), otherwise %FALSE
130 gst_date_time_has_year (const GstDateTime * datetime)
132 g_return_val_if_fail (datetime != NULL, FALSE);
134 return (datetime->fields >= GST_DATE_TIME_FIELDS_Y);
138 * gst_date_time_has_month:
139 * @datetime: a #GstDateTime
141 * Returns: %TRUE if @datetime<!-- -->'s month field is set, otherwise %FALSE
144 gst_date_time_has_month (const GstDateTime * datetime)
146 g_return_val_if_fail (datetime != NULL, FALSE);
148 return (datetime->fields >= GST_DATE_TIME_FIELDS_YM);
152 * gst_date_time_has_day:
153 * @datetime: a #GstDateTime
155 * Returns: %TRUE if @datetime<!-- -->'s day field is set, otherwise %FALSE
158 gst_date_time_has_day (const GstDateTime * datetime)
160 g_return_val_if_fail (datetime != NULL, FALSE);
162 return (datetime->fields >= GST_DATE_TIME_FIELDS_YMD);
166 * gst_date_time_has_time:
167 * @datetime: a #GstDateTime
169 * Returns: %TRUE if @datetime<!-- -->'s hour and minute fields are set,
173 gst_date_time_has_time (const GstDateTime * datetime)
175 g_return_val_if_fail (datetime != NULL, FALSE);
177 return (datetime->fields >= GST_DATE_TIME_FIELDS_YMD_HM);
181 * gst_date_time_has_second:
182 * @datetime: a #GstDateTime
184 * Returns: %TRUE if @datetime<!-- -->'s second field is set, otherwise %FALSE
187 gst_date_time_has_second (const GstDateTime * datetime)
189 g_return_val_if_fail (datetime != NULL, FALSE);
191 return (datetime->fields >= GST_DATE_TIME_FIELDS_YMD_HMS);
195 * gst_date_time_get_year:
196 * @datetime: a #GstDateTime
198 * Returns the year of this #GstDateTime
199 * Call gst_date_time_has_year before, to avoid warnings.
201 * Return value: The year of this #GstDateTime
204 gst_date_time_get_year (const GstDateTime * datetime)
206 g_return_val_if_fail (datetime != NULL, 0);
208 return g_date_time_get_year (datetime->datetime);
212 * gst_date_time_get_month:
213 * @datetime: a #GstDateTime
215 * Returns the month of this #GstDateTime. January is 1, February is 2, etc..
216 * Call gst_date_time_has_month before, to avoid warnings.
218 * Return value: The month of this #GstDateTime
221 gst_date_time_get_month (const GstDateTime * datetime)
223 g_return_val_if_fail (datetime != NULL, 0);
224 g_return_val_if_fail (gst_date_time_has_month (datetime), 0);
226 return g_date_time_get_month (datetime->datetime);
230 * gst_date_time_get_day:
231 * @datetime: a #GstDateTime
233 * Returns the day of the month of this #GstDateTime.
234 * Call gst_date_time_has_day before, to avoid warnings.
236 * Return value: The day of this #GstDateTime
239 gst_date_time_get_day (const GstDateTime * datetime)
241 g_return_val_if_fail (datetime != NULL, 0);
242 g_return_val_if_fail (gst_date_time_has_day (datetime), 0);
244 return g_date_time_get_day_of_month (datetime->datetime);
248 * gst_date_time_get_hour:
249 * @datetime: a #GstDateTime
251 * Retrieves the hour of the day represented by @datetime in the gregorian
252 * calendar. The return is in the range of 0 to 23.
253 * Call gst_date_time_has_haur before, to avoid warnings.
255 * Return value: the hour of the day
258 gst_date_time_get_hour (const GstDateTime * datetime)
260 g_return_val_if_fail (datetime != NULL, 0);
261 g_return_val_if_fail (gst_date_time_has_time (datetime), 0);
263 return g_date_time_get_hour (datetime->datetime);
267 * gst_date_time_get_minute:
268 * @datetime: a #GstDateTime
270 * Retrieves the minute of the hour represented by @datetime in the gregorian
272 * Call gst_date_time_has_minute before, to avoid warnings.
274 * Return value: the minute of the hour
277 gst_date_time_get_minute (const GstDateTime * datetime)
279 g_return_val_if_fail (datetime != NULL, 0);
280 g_return_val_if_fail (gst_date_time_has_time (datetime), 0);
282 return g_date_time_get_minute (datetime->datetime);
286 * gst_date_time_get_second:
287 * @datetime: a #GstDateTime
289 * Retrieves the second of the minute represented by @datetime in the gregorian
291 * Call gst_date_time_has_second before, to avoid warnings.
293 * Return value: the second represented by @datetime
296 gst_date_time_get_second (const GstDateTime * datetime)
298 g_return_val_if_fail (datetime != NULL, 0);
299 g_return_val_if_fail (gst_date_time_has_second (datetime), 0);
301 return g_date_time_get_second (datetime->datetime);
305 * gst_date_time_get_microsecond:
306 * @datetime: a #GstDateTime
308 * Retrieves the fractional part of the seconds in microseconds represented by
309 * @datetime in the gregorian calendar.
311 * Return value: the microsecond of the second
314 gst_date_time_get_microsecond (const GstDateTime * datetime)
316 g_return_val_if_fail (datetime != NULL, 0);
317 g_return_val_if_fail (gst_date_time_has_second (datetime), 0);
319 return g_date_time_get_microsecond (datetime->datetime);
323 * gst_date_time_get_time_zone_offset:
324 * @datetime: a #GstDateTime
326 * Retrieves the offset from UTC in hours that the timezone specified
327 * by @datetime represents. Timezones ahead (to the east) of UTC have positive
328 * values, timezones before (to the west) of UTC have negative values.
329 * If @datetime represents UTC time, then the offset is zero.
331 * Return value: the offset from UTC in hours
334 gst_date_time_get_time_zone_offset (const GstDateTime * datetime)
336 g_return_val_if_fail (datetime != NULL, 0.0);
337 g_return_val_if_fail (gst_date_time_has_time (datetime), 0.0);
339 return (g_date_time_get_utc_offset (datetime->datetime) /
340 G_USEC_PER_SEC) / 3600.0;
344 * gst_date_time_new_y:
345 * @year: the gregorian year
347 * Creates a new #GstDateTime using the date and times in the gregorian calendar
348 * in the local timezone.
350 * @year should be from 1 to 9999.
352 * Free-function: gst_date_time_unref
354 * Return value: (transfer full): the newly created #GstDateTime
357 gst_date_time_new_y (gint year)
359 return gst_date_time_new (0.0, year, -1, -1, -1, -1, -1);
363 * gst_date_time_new_ym:
364 * @year: the gregorian year
365 * @month: the gregorian month
367 * Creates a new #GstDateTime using the date and times in the gregorian calendar
368 * in the local timezone.
370 * @year should be from 1 to 9999, @month should be from 1 to 12.
372 * If value is -1 then all over value will be ignored. For example
373 * if @month == -1, then #GstDateTime will created only for @year.
375 * Free-function: gst_date_time_unref
377 * Return value: (transfer full): the newly created #GstDateTime
380 gst_date_time_new_ym (gint year, gint month)
382 return gst_date_time_new (0.0, year, month, -1, -1, -1, -1);
386 * gst_date_time_new_ymd:
387 * @year: the gregorian year
388 * @month: the gregorian month
389 * @day: the day of the gregorian month
391 * Creates a new #GstDateTime using the date and times in the gregorian calendar
392 * in the local timezone.
394 * @year should be from 1 to 9999, @month should be from 1 to 12, @day from
397 * If value is -1 then all over value will be ignored. For example
398 * if @month == -1, then #GstDateTime will created only for @year. If
399 * @day == -1, then #GstDateTime will created for @year and @month and
402 * Free-function: gst_date_time_unref
404 * Return value: (transfer full): the newly created #GstDateTime
407 gst_date_time_new_ymd (gint year, gint month, gint day)
409 return gst_date_time_new (0.0, year, month, day, -1, -1, -1);
413 * gst_date_time_new_from_unix_epoch_local_time:
414 * @secs: seconds from the Unix epoch
416 * Creates a new #GstDateTime using the time since Jan 1, 1970 specified by
417 * @secs. The #GstDateTime is in the local timezone.
419 * Free-function: gst_date_time_unref
421 * Return value: (transfer full): the newly created #GstDateTime
424 gst_date_time_new_from_unix_epoch_local_time (gint64 secs)
428 datetime = g_date_time_new_from_unix_local (secs);
429 return gst_date_time_new_from_g_date_time (datetime);
433 * gst_date_time_new_from_unix_epoch_utc:
434 * @secs: seconds from the Unix epoch
436 * Creates a new #GstDateTime using the time since Jan 1, 1970 specified by
437 * @secs. The #GstDateTime is in the UTC timezone.
439 * Free-function: gst_date_time_unref
441 * Return value: (transfer full): the newly created #GstDateTime
444 gst_date_time_new_from_unix_epoch_utc (gint64 secs)
446 GstDateTime *datetime;
448 gst_date_time_new_from_g_date_time (g_date_time_new_from_unix_utc (secs));
452 static GstDateTimeFields
453 gst_date_time_check_fields (gint * year, gint * month, gint * day,
454 gint * hour, gint * minute, gdouble * seconds)
458 *hour = *minute = *seconds = 0;
459 return GST_DATE_TIME_FIELDS_Y;
460 } else if (*day == -1) {
462 *hour = *minute = *seconds = 0;
463 return GST_DATE_TIME_FIELDS_YM;
464 } else if (*hour == -1) {
465 *hour = *minute = *seconds = 0;
466 return GST_DATE_TIME_FIELDS_YMD;
467 } else if (*seconds == -1) {
469 return GST_DATE_TIME_FIELDS_YMD_HM;
471 return GST_DATE_TIME_FIELDS_YMD_HMS;
475 * gst_date_time_new_local_time:
476 * @year: the gregorian year
477 * @month: the gregorian month, or -1
478 * @day: the day of the gregorian month, or -1
479 * @hour: the hour of the day, or -1
480 * @minute: the minute of the hour, or -1
481 * @seconds: the second of the minute, or -1
483 * Creates a new #GstDateTime using the date and times in the gregorian calendar
484 * in the local timezone.
486 * @year should be from 1 to 9999, @month should be from 1 to 12, @day from
487 * 1 to 31, @hour from 0 to 23, @minutes and @seconds from 0 to 59.
489 * If @month is -1, then the #GstDateTime created will only contain @year,
490 * and all other fields will be considered not set.
492 * If @day is -1, then the #GstDateTime created will only contain @year and
493 * @month and all other fields will be considered not set.
495 * If @hour is -1, then the #GstDateTime created will only contain @year and
496 * @month and @day, and the time fields will be considered not set. In this
497 * case @minute and @seconds should also be -1.
499 * Free-function: gst_date_time_unref
501 * Return value: (transfer full) (nullable): the newly created #GstDateTime
504 gst_date_time_new_local_time (gint year, gint month, gint day, gint hour,
505 gint minute, gdouble seconds)
507 GstDateTimeFields fields;
508 GstDateTime *datetime;
510 g_return_val_if_fail (year > 0 && year <= 9999, NULL);
511 g_return_val_if_fail ((month > 0 && month <= 12) || month == -1, NULL);
512 g_return_val_if_fail ((day > 0 && day <= 31) || day == -1, NULL);
513 g_return_val_if_fail ((hour >= 0 && hour < 24) || hour == -1, NULL);
514 g_return_val_if_fail ((minute >= 0 && minute < 60) || minute == -1, NULL);
515 g_return_val_if_fail ((seconds >= 0 && seconds < 60) || seconds == -1, NULL);
517 fields = gst_date_time_check_fields (&year, &month, &day,
518 &hour, &minute, &seconds);
520 datetime = gst_date_time_new_from_g_date_time (g_date_time_new_local (year,
521 month, day, hour, minute, seconds));
523 datetime->fields = fields;
528 * gst_date_time_new_now_local_time:
530 * Creates a new #GstDateTime representing the current date and time.
532 * Free-function: gst_date_time_unref
534 * Return value: (transfer full): the newly created #GstDateTime which should
535 * be freed with gst_date_time_unref().
538 gst_date_time_new_now_local_time (void)
540 return gst_date_time_new_from_g_date_time (g_date_time_new_now_local ());
544 * gst_date_time_new_now_utc:
546 * Creates a new #GstDateTime that represents the current instant at Universal
549 * Free-function: gst_date_time_unref
551 * Return value: (transfer full): the newly created #GstDateTime which should
552 * be freed with gst_date_time_unref().
555 gst_date_time_new_now_utc (void)
557 return gst_date_time_new_from_g_date_time (g_date_time_new_now_utc ());
561 __gst_date_time_compare (const GstDateTime * dt1, const GstDateTime * dt2)
565 /* we assume here that GST_DATE_TIME_FIELDS_YMD_HMS is the highest
566 * resolution, and ignore microsecond differences on purpose for now */
567 if (dt1->fields != dt2->fields)
568 return GST_VALUE_UNORDERED;
570 /* This will round down to nearest second, which is what we want. We're
571 * not comparing microseconds on purpose here, since we're not
572 * serialising them when doing new_utc_now() + to_string() */
574 g_date_time_to_unix (dt1->datetime) - g_date_time_to_unix (dt2->datetime);
576 return GST_VALUE_LESS_THAN;
578 return GST_VALUE_GREATER_THAN;
580 return GST_VALUE_EQUAL;
585 * @tzoffset: Offset from UTC in hours.
586 * @year: the gregorian year
587 * @month: the gregorian month
588 * @day: the day of the gregorian month
589 * @hour: the hour of the day
590 * @minute: the minute of the hour
591 * @seconds: the second of the minute
593 * Creates a new #GstDateTime using the date and times in the gregorian calendar
594 * in the supplied timezone.
596 * @year should be from 1 to 9999, @month should be from 1 to 12, @day from
597 * 1 to 31, @hour from 0 to 23, @minutes and @seconds from 0 to 59.
599 * Note that @tzoffset is a float and was chosen so for being able to handle
600 * some fractional timezones, while it still keeps the readability of
601 * representing it in hours for most timezones.
603 * If value is -1 then all over value will be ignored. For example
604 * if @month == -1, then #GstDateTime will created only for @year. If
605 * @day == -1, then #GstDateTime will created for @year and @month and
608 * Free-function: gst_date_time_unref
610 * Return value: (transfer full) (nullable): the newly created #GstDateTime
613 gst_date_time_new (gfloat tzoffset, gint year, gint month, gint day, gint hour,
614 gint minute, gdouble seconds)
616 GstDateTimeFields fields;
620 GstDateTime *datetime;
621 gint tzhour, tzminute;
623 g_return_val_if_fail (year > 0 && year <= 9999, NULL);
624 g_return_val_if_fail ((month > 0 && month <= 12) || month == -1, NULL);
625 g_return_val_if_fail ((day > 0 && day <= 31) || day == -1, NULL);
626 g_return_val_if_fail ((hour >= 0 && hour < 24) || hour == -1, NULL);
627 g_return_val_if_fail ((minute >= 0 && minute < 60) || minute == -1, NULL);
628 g_return_val_if_fail ((seconds >= 0 && seconds < 60) || seconds == -1, NULL);
629 g_return_val_if_fail (tzoffset >= -12.0 && tzoffset <= 12.0, NULL);
630 g_return_val_if_fail ((hour >= 0 && minute >= 0) ||
631 (hour == -1 && minute == -1 && seconds == -1 && tzoffset == 0.0), NULL);
633 tzhour = (gint) ABS (tzoffset);
634 tzminute = (gint) ((ABS (tzoffset) - tzhour) * 60);
636 g_snprintf (buf, 6, "%c%02d%02d", tzoffset >= 0 ? '+' : '-', tzhour,
639 tz = g_time_zone_new (buf);
641 fields = gst_date_time_check_fields (&year, &month, &day,
642 &hour, &minute, &seconds);
644 dt = g_date_time_new (tz, year, month, day, hour, minute, seconds);
645 g_time_zone_unref (tz);
647 datetime = gst_date_time_new_from_g_date_time (dt);
648 datetime->fields = fields;
654 __gst_date_time_serialize (GstDateTime * datetime, gboolean serialize_usecs)
660 /* we always have at least the year */
661 s = g_string_new (NULL);
662 g_string_append_printf (s, "%04u", gst_date_time_get_year (datetime));
664 if (datetime->fields == GST_DATE_TIME_FIELDS_Y)
668 g_string_append_printf (s, "-%02u", gst_date_time_get_month (datetime));
670 if (datetime->fields == GST_DATE_TIME_FIELDS_YM)
673 /* add day of month */
674 g_string_append_printf (s, "-%02u", gst_date_time_get_day (datetime));
676 if (datetime->fields == GST_DATE_TIME_FIELDS_YMD)
680 g_string_append_printf (s, "T%02u:%02u", gst_date_time_get_hour (datetime),
681 gst_date_time_get_minute (datetime));
683 if (datetime->fields == GST_DATE_TIME_FIELDS_YMD_HM)
687 g_string_append_printf (s, ":%02u", gst_date_time_get_second (datetime));
689 /* add microseconds */
690 if (serialize_usecs) {
691 msecs = gst_date_time_get_microsecond (datetime);
693 g_string_append_printf (s, ".%06u", msecs);
694 /* trim trailing 0s */
695 while (s->str[s->len - 1] == '0')
696 g_string_truncate (s, s->len - 1);
704 gmt_offset = gst_date_time_get_time_zone_offset (datetime);
705 if (gmt_offset == 0) {
706 g_string_append_c (s, 'Z');
708 guint tzhour, tzminute;
710 tzhour = (guint) ABS (gmt_offset);
711 tzminute = (guint) ((ABS (gmt_offset) - tzhour) * 60);
713 g_string_append_c (s, (gmt_offset >= 0) ? '+' : '-');
714 g_string_append_printf (s, "%02u%02u", tzhour, tzminute);
719 return g_string_free (s, FALSE);
723 * gst_date_time_to_iso8601_string:
724 * @datetime: GstDateTime.
726 * Create a minimal string compatible with ISO-8601. Possible output formats
727 * are (for example): 2012, 2012-06, 2012-06-23, 2012-06-23T23:30Z,
728 * 2012-06-23T23:30+0100, 2012-06-23T23:30:59Z, 2012-06-23T23:30:59+0100
730 * Returns: (nullable): a newly allocated string formatted according
731 * to ISO 8601 and only including the datetime fields that are
732 * valid, or %NULL in case there was an error. The string should
733 * be freed with g_free().
736 gst_date_time_to_iso8601_string (GstDateTime * datetime)
738 g_return_val_if_fail (datetime != NULL, NULL);
740 if (datetime->fields == GST_DATE_TIME_FIELDS_INVALID)
743 return __gst_date_time_serialize (datetime, FALSE);
747 * gst_date_time_new_from_iso8601_string:
748 * @string: ISO 8601-formatted datetime string.
750 * Tries to parse common variants of ISO-8601 datetime strings into a
751 * #GstDateTime. Possible input formats are (for example):
752 * 2012-06-30T22:46:43Z, 2012, 2012-06, 2012-06-30, 2012-06-30T22:46:43-0430,
753 * 2012-06-30T22:46Z, 2012-06-30T22:46-0430, 2012-06-30 22:46,
754 * 2012-06-30 22:46:43, 2012-06-00, 2012-00-00, 2012-00-30, 22:46:43Z, 22:46Z,
755 * 22:46:43-0430, 22:46-0430, 22:46:30, 22:46
756 * If no date is provided, it is assumed to be "today" in the timezone
757 * provided (if any), otherwise UTC.
759 * Free-function: gst_date_time_unref
761 * Returns: (transfer full) (nullable): a newly created #GstDateTime,
765 gst_date_time_new_from_iso8601_string (const gchar * string)
767 gint year = -1, month = -1, day = -1, hour = -1, minute = -1;
768 gint gmt_offset_hour = -99, gmt_offset_min = -99;
769 gdouble second = -1.0;
770 gfloat tzoffset = 0.0;
774 g_return_val_if_fail (string != NULL, NULL);
776 GST_DEBUG ("Parsing '%s' into a datetime", string);
778 len = strlen (string);
780 /* The input string is expected to start either with a year (4 digits) or
781 * with an hour (2 digits). Hour must be followed by minute. In any case,
782 * the string must be at least 4 characters long and start with 2 digits */
783 if (len < 4 || !g_ascii_isdigit (string[0]) || !g_ascii_isdigit (string[1]))
786 if (g_ascii_isdigit (string[2]) && g_ascii_isdigit (string[3])) {
787 ret = sscanf (string, "%04d-%02d-%02d", &year, &month, &day);
792 if (ret == 3 && day <= 0) {
797 if (ret >= 2 && month <= 0) {
802 if (ret >= 1 && (year <= 0 || year > 9999 || month > 12 || day > 31))
805 else if (ret >= 1 && len < 16)
806 /* YMD is 10 chars. XMD + HM will be 16 chars. if it is less,
807 * it make no sense to continue. We will stay with YMD. */
811 /* Exit if there is no expeceted value on this stage */
812 if (!(*string == 'T' || *string == '-' || *string == ' '))
817 /* if hour or minute fails, then we will use only ymd. */
818 hour = g_ascii_strtoull (string, (gchar **) & string, 10);
819 if (hour > 24 || *string != ':')
823 minute = g_ascii_strtoull (string + 1, (gchar **) & string, 10);
828 if (*string == ':') {
829 second = g_ascii_strtoull (string + 1, (gchar **) & string, 10);
830 /* if we fail here, we still can reuse hour and minute. We
831 * will still attempt to parse any timezone information */
836 if (*string == '.' || *string == ',') {
837 const gchar *usec_start = string + 1;
840 usecs = g_ascii_strtoull (string + 1, (gchar **) & string, 10);
841 if (usecs != G_MAXUINT64 && string > usec_start) {
842 digits = (guint) (string - usec_start);
843 second += (gdouble) usecs / pow (10.0, digits);
852 /* reuse some code from gst-plugins-base/gst-libs/gst/tag/gstxmptag.c */
853 gint gmt_offset = -1;
854 gchar *plus_pos = NULL;
855 gchar *neg_pos = NULL;
858 GST_LOG ("Checking for timezone information");
860 /* check if there is timezone info */
861 plus_pos = strrchr (string, '+');
862 neg_pos = strrchr (string, '-');
868 if (pos && strlen (pos) >= 3) {
871 ret_tz = sscanf (pos, "%d:%d", &gmt_offset_hour, &gmt_offset_min);
873 ret_tz = sscanf (pos, "%02d%02d", &gmt_offset_hour, &gmt_offset_min);
875 GST_DEBUG ("Parsing timezone: %s", pos);
878 if (neg_pos != NULL && neg_pos + 1 == pos) {
879 gmt_offset_hour *= -1;
880 gmt_offset_min *= -1;
882 gmt_offset = gmt_offset_hour * 60 + gmt_offset_min;
884 tzoffset = gmt_offset / 60.0;
886 GST_LOG ("Timezone offset: %f (%d minutes)", tzoffset, gmt_offset);
888 GST_WARNING ("Failed to parse timezone information");
893 if (year == -1 || month == -1 || day == -1) {
894 GDateTime *now_utc, *now_in_given_tz;
896 /* No date was supplied: make it today */
897 now_utc = g_date_time_new_now_utc ();
898 if (tzoffset != 0.0) {
899 /* If a timezone offset was supplied, get the date of that timezone */
900 g_assert (gmt_offset_min != -99);
901 g_assert (gmt_offset_hour != -99);
903 g_date_time_add_minutes (now_utc,
904 (60 * gmt_offset_hour) + gmt_offset_min);
905 g_date_time_unref (now_utc);
907 now_in_given_tz = now_utc;
909 g_date_time_get_ymd (now_in_given_tz, &year, &month, &day);
910 g_date_time_unref (now_in_given_tz);
912 return gst_date_time_new (tzoffset, year, month, day, hour, minute, second);
915 /* No date was supplied and time failed to parse */
918 return gst_date_time_new_ymd (year, month, day);
922 gst_date_time_free (GstDateTime * datetime)
924 g_date_time_unref (datetime->datetime);
925 g_slice_free (GstDateTime, datetime);
930 * @datetime: a #GstDateTime
932 * Atomically increments the reference count of @datetime by one.
934 * Return value: (transfer full): the reference @datetime
937 gst_date_time_ref (GstDateTime * datetime)
939 return (GstDateTime *) gst_mini_object_ref (GST_MINI_OBJECT_CAST (datetime));
943 * gst_date_time_unref:
944 * @datetime: (transfer full): a #GstDateTime
946 * Atomically decrements the reference count of @datetime by one. When the
947 * reference count reaches zero, the structure is freed.
950 gst_date_time_unref (GstDateTime * datetime)
952 gst_mini_object_unref (GST_MINI_OBJECT_CAST (datetime));
956 _priv_gst_date_time_initialize (void)
958 _gst_date_time_type = gst_date_time_get_type ();