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., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
24 #include "glib-compat-private.h"
25 #include "gst_private.h"
26 #include "gstdatetime.h"
33 * @short_description: A date, time and timezone structure
35 * Struct to store date, time and timezone information altogether.
36 * #GstDateTime is refcounted and immutable.
38 * Date information is handled using the proleptic Gregorian calendar.
40 * Provides basic creation functions and accessor functions to its fields.
45 #define GST_DATE_TIME_SEC_PER_DAY (G_GINT64_CONSTANT (86400))
46 #define GST_DATE_TIME_USEC_PER_DAY (G_GINT64_CONSTANT (86400000000))
47 #define GST_DATE_TIME_USEC_PER_HOUR (G_GINT64_CONSTANT (3600000000))
48 #define GST_DATE_TIME_USEC_PER_MINUTE (G_GINT64_CONSTANT (60000000))
49 #define GST_DATE_TIME_USEC_PER_SECOND (G_GINT64_CONSTANT (1000000))
50 #define GST_DATE_TIME_USEC_PER_MILLISECOND (G_GINT64_CONSTANT (1000))
56 HAS_YM, /* has year and month */
57 HAS_YMD, /* has year mont and day */
67 DateTimeFields fields;
68 volatile gint ref_count;
72 gst_date_time_new_from_gdatetime (GDateTime * dt)
79 gst_dt = g_slice_new (GstDateTime);
80 gst_dt->datetime = dt;
81 gst_dt->ref_count = 1;
86 * gst_date_time_has_year:
87 * @datetime: a #GstDateTime
89 * Returns the TRUE if year exist
94 gst_date_time_has_year (const GstDateTime * datetime)
96 return (datetime->fields >= HAS_Y) ? 1 : 0;
100 * gst_date_time_has_month:
101 * @datetime: a #GstDateTime
103 * Returns the TRUE if month exist
108 gst_date_time_has_month (const GstDateTime * datetime)
110 return (datetime->fields >= HAS_YM) ? 1 : 0;
114 * gst_date_time_has_day:
115 * @datetime: a #GstDateTime
117 * Returns the TRUE if day exist
122 gst_date_time_has_day (const GstDateTime * datetime)
124 return (datetime->fields >= HAS_YMD) ? 1 : 0;
128 * gst_date_time_has_hour:
129 * @datetime: a #GstDateTime
131 * Returns the TRUE if hour exist
136 gst_date_time_has_hour (const GstDateTime * datetime)
138 return (datetime->fields >= HAS_YMD_H) ? 1 : 0;
142 * gst_date_time_has_minute:
143 * @datetime: a #GstDateTime
145 * Returns the TRUE if minute exist
150 gst_date_time_has_minute (const GstDateTime * datetime)
152 return (datetime->fields >= HAS_YMD_HM) ? 1 : 0;
156 * gst_date_time_has_second:
157 * @datetime: a #GstDateTime
159 * Returns the TRUE if second exist
164 gst_date_time_has_second (const GstDateTime * datetime)
166 return (datetime->fields >= HAS_YMD_HMS) ? 1 : 0;
170 * gst_date_time_get_year:
171 * @datetime: a #GstDateTime
173 * Returns the year of this #GstDateTime
174 * Call gst_date_time_has_year before, to avoid warnings.
176 * Return value: The year of this #GstDateTime
180 gst_date_time_get_year (const GstDateTime * datetime)
182 g_return_val_if_fail (gst_date_time_has_year (datetime), 0);
183 return g_date_time_get_year (datetime->datetime);
187 * gst_date_time_get_month:
188 * @datetime: a #GstDateTime
190 * Returns the month of this #GstDateTime. January is 1, February is 2, etc..
191 * Call gst_date_time_has_month before, to avoid warnings.
193 * Return value: The month of this #GstDateTime
197 gst_date_time_get_month (const GstDateTime * datetime)
199 g_return_val_if_fail (gst_date_time_has_month (datetime), 0);
200 return g_date_time_get_month (datetime->datetime);
204 * gst_date_time_get_day:
205 * @datetime: a #GstDateTime
207 * Returns the day of this #GstDateTime.
208 * Call gst_date_time_has_day before, to avoid warnings.
210 * Return value: The day of this #GstDateTime
214 gst_date_time_get_day (const GstDateTime * datetime)
216 g_return_val_if_fail (gst_date_time_has_day (datetime), 0);
217 return g_date_time_get_day_of_month (datetime->datetime);
221 * gst_date_time_get_hour:
222 * @datetime: a #GstDateTime
224 * Retrieves the hour of the day represented by @datetime in the gregorian
225 * calendar. The return is in the range of 0 to 23.
226 * Call gst_date_time_has_haur before, to avoid warnings.
228 * Return value: the hour of the day
233 gst_date_time_get_hour (const GstDateTime * datetime)
235 g_return_val_if_fail (gst_date_time_has_hour (datetime), 0);
236 return g_date_time_get_hour (datetime->datetime);
240 * gst_date_time_get_minute:
241 * @datetime: a #GstDateTime
243 * Retrieves the minute of the hour represented by @datetime in the gregorian
245 * Call gst_date_time_has_minute before, to avoid warnings.
247 * Return value: the minute of the hour
252 gst_date_time_get_minute (const GstDateTime * datetime)
254 g_return_val_if_fail (gst_date_time_has_minute (datetime), 0);
255 return g_date_time_get_minute (datetime->datetime);
259 * gst_date_time_get_second:
260 * @datetime: a #GstDateTime
262 * Retrieves the second of the minute represented by @datetime in the gregorian
264 * Call gst_date_time_has_second before, to avoid warnings.
266 * Return value: the second represented by @datetime
271 gst_date_time_get_second (const GstDateTime * datetime)
273 g_return_val_if_fail (gst_date_time_has_second (datetime), 0);
274 return g_date_time_get_second (datetime->datetime);
278 * gst_date_time_get_microsecond:
279 * @datetime: a #GstDateTime
281 * Retrieves the fractional part of the seconds in microseconds represented by
282 * @datetime in the gregorian calendar.
284 * Return value: the microsecond of the second
289 gst_date_time_get_microsecond (const GstDateTime * datetime)
291 return g_date_time_get_microsecond (datetime->datetime);
295 * gst_date_time_get_time_zone_offset:
296 * @datetime: a #GstDateTime
298 * Retrieves the offset from UTC in hours that the timezone specified
299 * by @datetime represents. Timezones ahead (to the east) of UTC have positive
300 * values, timezones before (to the west) of UTC have negative values.
301 * If @datetime represents UTC time, then the offset is zero.
303 * Return value: the offset from UTC in hours
307 gst_date_time_get_time_zone_offset (const GstDateTime * datetime)
309 return (g_date_time_get_utc_offset (datetime->datetime) /
310 G_USEC_PER_SEC) / 3600.0;
314 * gst_date_time_new_y:
315 * @year: the gregorian year
317 * Creates a new #GstDateTime using the date and times in the gregorian calendar
318 * in the local timezone.
320 * @year should be from 1 to 9999.
322 * Free-function: gst_date_time_unref
324 * Return value: (transfer full): the newly created #GstDateTime
329 gst_date_time_new_y (gint year)
331 GstDateTime *datetime;
332 datetime = gst_date_time_new_local_time (year, 1, 1, 1, 1, 1);
333 datetime->fields = HAS_Y;
338 * gst_date_time_new_ym:
339 * @year: the gregorian year
340 * @month: the gregorian month
342 * Creates a new #GstDateTime using the date and times in the gregorian calendar
343 * in the local timezone.
345 * @year should be from 1 to 9999, @month should be from 1 to 12.
347 * If value is -1 then all over value will be ignored. For example
348 * if @month == -1, then #GstDateTime will created only for @year.
350 * Free-function: gst_date_time_unref
352 * Return value: (transfer full): the newly created #GstDateTime
357 gst_date_time_new_ym (gint year, gint month)
359 GstDateTime *datetime;
360 datetime = gst_date_time_new_local_time (year, month, 1, 1, 1, 1);
361 datetime->fields = HAS_YM;
366 * gst_date_time_new_ymd:
367 * @year: the gregorian year
368 * @month: the gregorian month
369 * @day: the day of the gregorian month
371 * Creates a new #GstDateTime using the date and times in the gregorian calendar
372 * in the local timezone.
374 * @year should be from 1 to 9999, @month should be from 1 to 12, @day from
377 * If value is -1 then all over value will be ignored. For example
378 * if @month == -1, then #GstDateTime will created only for @year. If
379 * @day == -1, then #GstDateTime will created for @year and @month and
382 * Free-function: gst_date_time_unref
384 * Return value: (transfer full): the newly created #GstDateTime
389 gst_date_time_new_ymd (gint year, gint month, gint day)
391 GstDateTime *datetime;
392 datetime = gst_date_time_new_local_time (year, month, day, 1, 1, 1);
393 datetime->fields = HAS_YMD;
398 * gst_date_time_new_ymd_h:
399 * @year: the gregorian year
400 * @month: the gregorian month
401 * @day: the day of the gregorian month
402 * @hour: the hour of the day
404 * Creates a new #GstDateTime using the date and times in the gregorian calendar
405 * in the local timezone.
407 * @year should be from 1 to 9999, @month should be from 1 to 12, @day from
408 * 1 to 31, @hour from 0 to 23.
410 * If value is -1 then all over value will be ignored. For example
411 * if @month == -1, then #GstDateTime will created only for @year. If
412 * @day == -1, then #GstDateTime will created for @year and @month and
415 * Free-function: gst_date_time_unref
417 * Return value: (transfer full): the newly created #GstDateTime
422 gst_date_time_new_ymd_h (gint year, gint month, gint day, gint hour)
424 GstDateTime *datetime;
425 datetime = gst_date_time_new_local_time (year, month, day, hour, 1, 1);
426 datetime->fields = HAS_YMD_H;
431 * gst_date_time_new_ymd_hm:
432 * @year: the gregorian year
433 * @month: the gregorian month
434 * @day: the day of the gregorian month
435 * @hour: the hour of the day
436 * @minute: the minute of the hour
438 * Creates a new #GstDateTime using the date and times in the gregorian calendar
439 * in the local timezone.
441 * @year should be from 1 to 9999, @month should be from 1 to 12, @day from
442 * 1 to 31, @hour from 0 to 23, @minutes from 0 to 59.
444 * If value is -1 then all over value will be ignored. For example
445 * if @month == -1, then #GstDateTime will created only for @year. If
446 * @day == -1, then #GstDateTime will created for @year and @month and
449 * Free-function: gst_date_time_unref
451 * Return value: (transfer full): the newly created #GstDateTime
456 gst_date_time_new_ymd_hm (gint year, gint month, gint day, gint hour,
459 GstDateTime *datetime;
460 datetime = gst_date_time_new_local_time (year, month, day, hour, minute, 1);
461 datetime->fields = HAS_YMD_HM;
466 * gst_date_time_new_from_unix_epoch_local_time:
467 * @secs: seconds from the Unix epoch
469 * Creates a new #GstDateTime using the time since Jan 1, 1970 specified by
470 * @secs. The #GstDateTime is in the local timezone.
472 * Free-function: gst_date_time_unref
474 * Return value: (transfer full): the newly created #GstDateTime
479 gst_date_time_new_from_unix_epoch_local_time (gint64 secs)
481 GstDateTime *datetime;
483 gst_date_time_new_from_gdatetime (g_date_time_new_from_unix_local (secs));
484 datetime->fields = HAS_YMD_HMS;
489 * gst_date_time_new_from_unix_epoch_utc:
490 * @secs: seconds from the Unix epoch
492 * Creates a new #GstDateTime using the time since Jan 1, 1970 specified by
493 * @secs. The #GstDateTime is in the UTC timezone.
495 * Free-function: gst_date_time_unref
497 * Return value: (transfer full): the newly created #GstDateTime
502 gst_date_time_new_from_unix_epoch_utc (gint64 secs)
504 GstDateTime *datetime;
506 gst_date_time_new_from_gdatetime (g_date_time_new_from_unix_utc (secs));
507 datetime->fields = HAS_YMD_HMS;
511 static DateTimeFields
512 gst_date_time_check_fields (gint * year, gint * month, gint * day,
513 gint * hour, gint * minute, gdouble * seconds)
516 *month = *day = *hour = *minute = *seconds = 1;
518 } else if (*day == -1) {
519 *day = *hour = *minute = *seconds = 1;
521 } else if (*hour == -1) {
522 *hour = *minute = *seconds = 1;
524 } else if (*minute == -1) {
525 *minute = *seconds = 1;
527 } else if (*seconds == -1) {
535 * gst_date_time_new_local_time:
536 * @year: the gregorian year
537 * @month: the gregorian month
538 * @day: the day of the gregorian month
539 * @hour: the hour of the day
540 * @minute: the minute of the hour
541 * @seconds: the second of the minute
543 * Creates a new #GstDateTime using the date and times in the gregorian calendar
544 * in the local timezone.
546 * @year should be from 1 to 9999, @month should be from 1 to 12, @day from
547 * 1 to 31, @hour from 0 to 23, @minutes and @seconds from 0 to 59.
549 * If value is -1 then all over value will be ignored. For example
550 * if @month == -1, then #GstDateTime will created only for @year. If
551 * @day == -1, then #GstDateTime will created for @year and @month and
554 * Free-function: gst_date_time_unref
556 * Return value: (transfer full): the newly created #GstDateTime
561 gst_date_time_new_local_time (gint year, gint month, gint day, gint hour,
562 gint minute, gdouble seconds)
564 GstDateTime *datetime;
565 DateTimeFields fields;
567 g_return_val_if_fail (year > 0 && year <= 9999, NULL);
568 g_return_val_if_fail ((month > 0 && month <= 12) || month == -1, NULL);
569 g_return_val_if_fail ((day > 0 && day <= 31) || day == -1, NULL);
570 g_return_val_if_fail ((hour >= 0 && hour < 24) || hour == -1, NULL);
571 g_return_val_if_fail ((minute >= 0 && minute < 60) || minute == -1, NULL);
572 g_return_val_if_fail ((seconds >= 0 && seconds < 60) || seconds == -1, NULL);
574 fields = gst_date_time_check_fields (&year, &month, &day,
575 &hour, &minute, &seconds);
577 datetime = gst_date_time_new_from_gdatetime (g_date_time_new_local (year,
578 month, day, hour, minute, seconds));
580 datetime->fields = fields;
585 * gst_date_time_new_now_local_time:
587 * Creates a new #GstDateTime representing the current date and time.
589 * Free-function: gst_date_time_unref
591 * Return value: (transfer full): the newly created #GstDateTime which should
592 * be freed with gst_date_time_unref().
597 gst_date_time_new_now_local_time (void)
599 GstDateTime *datetime;
601 datetime = gst_date_time_new_from_gdatetime (g_date_time_new_now_local ());
602 datetime->fields = HAS_YMD_HMS;
607 * gst_date_time_new_now_utc:
609 * Creates a new #GstDateTime that represents the current instant at Universal
612 * Free-function: gst_date_time_unref
614 * Return value: (transfer full): the newly created #GstDateTime which should
615 * be freed with gst_date_time_unref().
620 gst_date_time_new_now_utc (void)
622 GstDateTime *datetime;
624 datetime = gst_date_time_new_from_gdatetime (g_date_time_new_now_utc ());
625 datetime->fields = HAS_YMD_HMS;
630 priv_gst_date_time_compare (gconstpointer dt1, gconstpointer dt2)
632 const GstDateTime *datetime1 = dt1;
633 const GstDateTime *datetime2 = dt2;
634 return g_date_time_compare (datetime1->datetime, datetime2->datetime);
639 * @tzoffset: Offset from UTC in hours.
640 * @year: the gregorian year
641 * @month: the gregorian month
642 * @day: the day of the gregorian month
643 * @hour: the hour of the day
644 * @minute: the minute of the hour
645 * @seconds: the second of the minute
647 * Creates a new #GstDateTime using the date and times in the gregorian calendar
648 * in the supplied timezone.
650 * @year should be from 1 to 9999, @month should be from 1 to 12, @day from
651 * 1 to 31, @hour from 0 to 23, @minutes and @seconds from 0 to 59.
653 * Note that @tzoffset is a float and was chosen so for being able to handle
654 * some fractional timezones, while it still keeps the readability of
655 * represeting it in hours for most timezones.
657 * If value is -1 then all over value will be ignored. For example
658 * if @month == -1, then #GstDateTime will created only for @year. If
659 * @day == -1, then #GstDateTime will created for @year and @month and
662 * Free-function: gst_date_time_unref
664 * Return value: (transfer full): the newly created #GstDateTime
669 gst_date_time_new (gfloat tzoffset, gint year, gint month, gint day, gint hour,
670 gint minute, gdouble seconds)
675 GstDateTime *datetime;
676 gint tzhour, tzminute;
677 DateTimeFields fields;
679 g_return_val_if_fail (year > 0 && year <= 9999, NULL);
680 g_return_val_if_fail ((month > 0 && month <= 12) || month == -1, NULL);
681 g_return_val_if_fail ((day > 0 && day <= 31) || day == -1, NULL);
682 g_return_val_if_fail ((hour >= 0 && hour < 24) || hour == -1, NULL);
683 g_return_val_if_fail ((minute >= 0 && minute < 60) || minute == -1, NULL);
684 g_return_val_if_fail ((seconds >= 0 && seconds < 60) || seconds == -1, NULL);
685 g_return_val_if_fail (tzoffset >= -12.0 && tzoffset <= 12.0, NULL);
687 tzhour = (gint) ABS (tzoffset);
688 tzminute = (gint) ((ABS (tzoffset) - tzhour) * 60);
690 g_snprintf (buf, 6, "%c%02d%02d", tzoffset >= 0 ? '+' : '-', tzhour,
693 tz = g_time_zone_new (buf);
695 fields = gst_date_time_check_fields (&year, &month, &day,
696 &hour, &minute, &seconds);
698 dt = g_date_time_new (tz, year, month, day, hour, minute, seconds);
699 g_time_zone_unref (tz);
701 datetime = gst_date_time_new_from_gdatetime (dt);
702 datetime->fields = fields;
708 gst_date_time_free (GstDateTime * datetime)
710 g_date_time_unref (datetime->datetime);
711 g_slice_free (GstDateTime, datetime);
716 * @datetime: a #GstDateTime
718 * Atomically increments the reference count of @datetime by one.
720 * Return value: (transfer full): the reference @datetime
725 gst_date_time_ref (GstDateTime * datetime)
727 g_return_val_if_fail (datetime != NULL, NULL);
728 g_return_val_if_fail (datetime->ref_count > 0, NULL);
729 g_atomic_int_inc (&datetime->ref_count);
734 * gst_date_time_unref:
735 * @datetime: (transfer full): a #GstDateTime
737 * Atomically decrements the reference count of @datetime by one. When the
738 * reference count reaches zero, the structure is freed.
743 gst_date_time_unref (GstDateTime * datetime)
745 g_return_if_fail (datetime != NULL);
746 g_return_if_fail (datetime->ref_count > 0);
748 if (g_atomic_int_dec_and_test (&datetime->ref_count))
749 gst_date_time_free (datetime);