X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=gst%2Fgstdatetime.c;h=cea0f708a04a60f969cc74e6b6426e3f20da4414;hb=5688773b91c3ea1094996a573f119230727a0978;hp=e05d22298d332be05f72c6315ed6934930526adb;hpb=1b27e1e792e99242feadf0f668e284f6f45c2787;p=platform%2Fupstream%2Fgstreamer.git diff --git a/gst/gstdatetime.c b/gst/gstdatetime.c index e05d222..cea0f70 100644 --- a/gst/gstdatetime.c +++ b/gst/gstdatetime.c @@ -13,19 +13,21 @@ * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the - * Free Software Foundation, Inc., 59 Temple Place - Suite 330, - * Boston, MA 02111-1307, USA. + * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301, USA. */ #ifdef HAVE_CONFIG_H #include "config.h" #endif -#include "glib-compat-private.h" #include "gst_private.h" +#include "glib-compat-private.h" #include "gstdatetime.h" +#include "gstvalue.h" #include #include +#include /** * SECTION:gstdatetime @@ -38,39 +40,209 @@ * Date information is handled using the proleptic Gregorian calendar. * * Provides basic creation functions and accessor functions to its fields. + */ + +typedef enum +{ + GST_DATE_TIME_FIELDS_INVALID = 0, + GST_DATE_TIME_FIELDS_Y, /* have year */ + GST_DATE_TIME_FIELDS_YM, /* have year and month */ + GST_DATE_TIME_FIELDS_YMD, /* have year, month and day */ + GST_DATE_TIME_FIELDS_YMD_HM, + GST_DATE_TIME_FIELDS_YMD_HMS + /* Note: if we ever add more granularity here, e.g. for microsecs, + * the compare function will need updating */ +} GstDateTimeFields; + +struct _GstDateTime +{ + GstMiniObject mini_object; + + GDateTime *datetime; + + GstDateTimeFields fields; +}; + +GType _gst_date_time_type = 0; +GST_DEFINE_MINI_OBJECT_TYPE (GstDateTime, gst_date_time); + +static void gst_date_time_free (GstDateTime * datetime); + +/** + * gst_date_time_new_from_g_date_time: + * @dt: (transfer full): the #GDateTime. The new #GstDateTime takes ownership. + * + * Creates a new #GstDateTime from a #GDateTime object. + * + * Free-function: gst_date_time_unref + * + * Returns: (transfer full) (nullable): a newly created #GstDateTime, + * or %NULL on error + */ +GstDateTime * +gst_date_time_new_from_g_date_time (GDateTime * dt) +{ + GstDateTime *gst_dt; + + if (!dt) + return NULL; + + gst_dt = g_slice_new (GstDateTime); + + gst_mini_object_init (GST_MINI_OBJECT_CAST (gst_dt), 0, GST_TYPE_DATE_TIME, + NULL, NULL, (GstMiniObjectFreeFunction) gst_date_time_free); + + gst_dt->datetime = dt; + gst_dt->fields = GST_DATE_TIME_FIELDS_YMD_HMS; + return gst_dt; +} + +/** + * gst_date_time_to_g_date_time: + * @datetime: GstDateTime. + * + * Creates a new #GDateTime from a fully defined #GstDateTime object. + * + * Free-function: g_date_time_unref + * + * Returns: (transfer full) (nullable): a newly created #GDateTime, or + * %NULL on error + */ +GDateTime * +gst_date_time_to_g_date_time (GstDateTime * datetime) +{ + g_return_val_if_fail (datetime != NULL, NULL); + + if (datetime->fields != GST_DATE_TIME_FIELDS_YMD_HMS) + return NULL; + + return g_date_time_add (datetime->datetime, 0); +} + +/** + * gst_date_time_has_year: + * @datetime: a #GstDateTime + * + * Returns: %TRUE if @datetime's year field is set (which should always + * be the case), otherwise %FALSE + */ +gboolean +gst_date_time_has_year (const GstDateTime * datetime) +{ + g_return_val_if_fail (datetime != NULL, FALSE); + + return (datetime->fields >= GST_DATE_TIME_FIELDS_Y); +} + +/** + * gst_date_time_has_month: + * @datetime: a #GstDateTime + * + * Returns: %TRUE if @datetime's month field is set, otherwise %FALSE + */ +gboolean +gst_date_time_has_month (const GstDateTime * datetime) +{ + g_return_val_if_fail (datetime != NULL, FALSE); + + return (datetime->fields >= GST_DATE_TIME_FIELDS_YM); +} + +/** + * gst_date_time_has_day: + * @datetime: a #GstDateTime * - * Since: 0.10.31 + * Returns: %TRUE if @datetime's day field is set, otherwise %FALSE */ +gboolean +gst_date_time_has_day (const GstDateTime * datetime) +{ + g_return_val_if_fail (datetime != NULL, FALSE); + + return (datetime->fields >= GST_DATE_TIME_FIELDS_YMD); +} + +/** + * gst_date_time_has_time: + * @datetime: a #GstDateTime + * + * Returns: %TRUE if @datetime's hour and minute fields are set, + * otherwise %FALSE + */ +gboolean +gst_date_time_has_time (const GstDateTime * datetime) +{ + g_return_val_if_fail (datetime != NULL, FALSE); + + return (datetime->fields >= GST_DATE_TIME_FIELDS_YMD_HM); +} + +/** + * gst_date_time_has_second: + * @datetime: a #GstDateTime + * + * Returns: %TRUE if @datetime's second field is set, otherwise %FALSE + */ +gboolean +gst_date_time_has_second (const GstDateTime * datetime) +{ + g_return_val_if_fail (datetime != NULL, FALSE); + + return (datetime->fields >= GST_DATE_TIME_FIELDS_YMD_HMS); +} /** * gst_date_time_get_year: * @datetime: a #GstDateTime * * Returns the year of this #GstDateTime + * Call gst_date_time_has_year() before, to avoid warnings. * * Return value: The year of this #GstDateTime - * Since: 0.10.31 */ +gint +gst_date_time_get_year (const GstDateTime * datetime) +{ + g_return_val_if_fail (datetime != NULL, 0); + + return g_date_time_get_year (datetime->datetime); +} /** * gst_date_time_get_month: * @datetime: a #GstDateTime * * Returns the month of this #GstDateTime. January is 1, February is 2, etc.. + * Call gst_date_time_has_month() before, to avoid warnings. * * Return value: The month of this #GstDateTime - * Since: 0.10.31 */ +gint +gst_date_time_get_month (const GstDateTime * datetime) +{ + g_return_val_if_fail (datetime != NULL, 0); + g_return_val_if_fail (gst_date_time_has_month (datetime), 0); + + return g_date_time_get_month (datetime->datetime); +} /** * gst_date_time_get_day: * @datetime: a #GstDateTime * - * Returns the day of this #GstDateTime. + * Returns the day of the month of this #GstDateTime. + * Call gst_date_time_has_day() before, to avoid warnings. * * Return value: The day of this #GstDateTime - * Since: 0.10.31 */ +gint +gst_date_time_get_day (const GstDateTime * datetime) +{ + g_return_val_if_fail (datetime != NULL, 0); + g_return_val_if_fail (gst_date_time_has_day (datetime), 0); + + return g_date_time_get_day_of_month (datetime->datetime); +} /** * gst_date_time_get_hour: @@ -78,23 +250,18 @@ * * Retrieves the hour of the day represented by @datetime in the gregorian * calendar. The return is in the range of 0 to 23. + * Call gst_date_time_has_time() before, to avoid warnings. * * Return value: the hour of the day - * - * Since: 0.10.31 */ +gint +gst_date_time_get_hour (const GstDateTime * datetime) +{ + g_return_val_if_fail (datetime != NULL, 0); + g_return_val_if_fail (gst_date_time_has_time (datetime), 0); -/** - * gst_date_time_get_microsecond: - * @datetime: a #GstDateTime - * - * Retrieves the fractional part of the seconds in microseconds represented by - * @datetime in the gregorian calendar. - * - * Return value: the microsecond of the second - * - * Since: 0.10.31 - */ + return g_date_time_get_hour (datetime->datetime); +} /** * gst_date_time_get_minute: @@ -102,11 +269,18 @@ * * Retrieves the minute of the hour represented by @datetime in the gregorian * calendar. + * Call gst_date_time_has_time() before, to avoid warnings. * * Return value: the minute of the hour - * - * Since: 0.10.31 */ +gint +gst_date_time_get_minute (const GstDateTime * datetime) +{ + g_return_val_if_fail (datetime != NULL, 0); + g_return_val_if_fail (gst_date_time_has_time (datetime), 0); + + return g_date_time_get_minute (datetime->datetime); +} /** * gst_date_time_get_second: @@ -114,23 +288,36 @@ * * Retrieves the second of the minute represented by @datetime in the gregorian * calendar. + * Call gst_date_time_has_time() before, to avoid warnings. * * Return value: the second represented by @datetime - * - * Since: 0.10.31 */ +gint +gst_date_time_get_second (const GstDateTime * datetime) +{ + g_return_val_if_fail (datetime != NULL, 0); + g_return_val_if_fail (gst_date_time_has_second (datetime), 0); + + return g_date_time_get_second (datetime->datetime); +} /** - * gst_date_time_get_second: + * gst_date_time_get_microsecond: * @datetime: a #GstDateTime * - * Retrieves the second of the minute represented by @datetime in the gregorian - * calendar. - * - * Return value: the second represented by @datetime + * Retrieves the fractional part of the seconds in microseconds represented by + * @datetime in the gregorian calendar. * - * Since: 0.10.31 + * Return value: the microsecond of the second */ +gint +gst_date_time_get_microsecond (const GstDateTime * datetime) +{ + g_return_val_if_fail (datetime != NULL, 0); + g_return_val_if_fail (gst_date_time_has_second (datetime), 0); + + return g_date_time_get_microsecond (datetime->datetime); +} /** * gst_date_time_get_time_zone_offset: @@ -142,658 +329,636 @@ * If @datetime represents UTC time, then the offset is zero. * * Return value: the offset from UTC in hours - * Since: 0.10.31 */ +gfloat +gst_date_time_get_time_zone_offset (const GstDateTime * datetime) +{ + g_return_val_if_fail (datetime != NULL, 0.0); + g_return_val_if_fail (gst_date_time_has_time (datetime), 0.0); -/** - * gst_date_time_new_from_unix_epoch_local_time: - * @secs: seconds from the Unix epoch - * - * Creates a new #GstDateTime using the time since Jan 1, 1970 specified by - * @secs. The #GstDateTime is in the local timezone. - * - * Return value: the newly created #GstDateTime - * - * Since: 0.10.31 - */ + return (g_date_time_get_utc_offset (datetime->datetime) / + G_USEC_PER_SEC) / 3600.0; +} /** - * gst_date_time_new_from_unix_epoch_utc: - * @secs: seconds from the Unix epoch + * gst_date_time_new_y: + * @year: the gregorian year * - * Creates a new #GstDateTime using the time since Jan 1, 1970 specified by - * @secs. The #GstDateTime is in the UTC timezone. + * Creates a new #GstDateTime using the date and times in the gregorian calendar + * in the local timezone. + * + * @year should be from 1 to 9999. * - * Return value: the newly created #GstDateTime + * Free-function: gst_date_time_unref * - * Since: 0.10.31 + * Return value: (transfer full): the newly created #GstDateTime */ +GstDateTime * +gst_date_time_new_y (gint year) +{ + return gst_date_time_new (0.0, year, -1, -1, -1, -1, -1); +} /** - * gst_date_time_new_local_time: + * gst_date_time_new_ym: * @year: the gregorian year * @month: the gregorian month - * @day: the day of the gregorian month - * @hour: the hour of the day - * @minute: the minute of the hour - * @second: the second of the minute - * @microsecond: the microsecond of the second * * Creates a new #GstDateTime using the date and times in the gregorian calendar * in the local timezone. * - * @year should be from 1 to 9999, @month should be from 1 to 12, @day from - * 1 to 31, @hour from 0 to 23, @minutes and @seconds from 0 to 59 and - * @microsecond from 0 to 999999. + * @year should be from 1 to 9999, @month should be from 1 to 12. * - * Return value: the newly created #GstDateTime + * If value is -1 then all over value will be ignored. For example + * if @month == -1, then #GstDateTime will created only for @year. * - * Since: 0.10.31 + * Free-function: gst_date_time_unref + * + * Return value: (transfer full): the newly created #GstDateTime */ +GstDateTime * +gst_date_time_new_ym (gint year, gint month) +{ + return gst_date_time_new (0.0, year, month, -1, -1, -1, -1); +} /** - * gst_date_time_new: - * @tzoffset: Offset from UTC in hours. + * gst_date_time_new_ymd: * @year: the gregorian year * @month: the gregorian month * @day: the day of the gregorian month - * @hour: the hour of the day - * @minute: the minute of the hour - * @second: the second of the minute - * @microsecond: the microsecond of the second * * Creates a new #GstDateTime using the date and times in the gregorian calendar - * in the supplied timezone. + * in the local timezone. * * @year should be from 1 to 9999, @month should be from 1 to 12, @day from - * 1 to 31, @hour from 0 to 23, @minutes and @seconds from 0 to 59 and - * @microsecond from 0 to 999999. + * 1 to 31. * - * Note that @tzoffset is a float and was chosen so for being able to handle - * some fractional timezones, while it still keeps the readability of - * represeting it in hours for most timezones. + * If value is -1 then all over value will be ignored. For example + * if @month == -1, then #GstDateTime will created only for @year. If + * @day == -1, then #GstDateTime will created for @year and @month and + * so on. * - * Return value: the newly created #GstDateTime + * Free-function: gst_date_time_unref * - * Since: 0.10.31 + * Return value: (transfer full): the newly created #GstDateTime */ +GstDateTime * +gst_date_time_new_ymd (gint year, gint month, gint day) +{ + return gst_date_time_new (0.0, year, month, day, -1, -1, -1); +} /** - * gst_date_time_new_now_local_time: + * gst_date_time_new_from_unix_epoch_local_time: + * @secs: seconds from the Unix epoch * - * Creates a new #GstDateTime representing the current date and time. + * Creates a new #GstDateTime using the time since Jan 1, 1970 specified by + * @secs. The #GstDateTime is in the local timezone. * - * Return value: the newly created #GstDateTime which should be freed with - * gst_date_time_unref(). + * Free-function: gst_date_time_unref * - * Since: 0.10.31 + * Return value: (transfer full): the newly created #GstDateTime */ +GstDateTime * +gst_date_time_new_from_unix_epoch_local_time (gint64 secs) +{ + GDateTime *datetime; + + datetime = g_date_time_new_from_unix_local (secs); + return gst_date_time_new_from_g_date_time (datetime); +} /** - * gst_date_time_new_now_utc: + * gst_date_time_new_from_unix_epoch_utc: + * @secs: seconds from the Unix epoch * - * Creates a new #GstDateTime that represents the current instant at Universal - * coordinated time. + * Creates a new #GstDateTime using the time since Jan 1, 1970 specified by + * @secs. The #GstDateTime is in the UTC timezone. * - * Return value: the newly created #GstDateTime which should be freed with - * gst_date_time_unref(). + * Free-function: gst_date_time_unref * - * Since: 0.10.31 + * Return value: (transfer full): the newly created #GstDateTime */ - - -#define GST_DATE_TIME_SEC_PER_DAY (G_GINT64_CONSTANT (86400)) -#define GST_DATE_TIME_USEC_PER_DAY (G_GINT64_CONSTANT (86400000000)) -#define GST_DATE_TIME_USEC_PER_HOUR (G_GINT64_CONSTANT (3600000000)) -#define GST_DATE_TIME_USEC_PER_MINUTE (G_GINT64_CONSTANT (60000000)) -#define GST_DATE_TIME_USEC_PER_SECOND (G_GINT64_CONSTANT (1000000)) -#define GST_DATE_TIME_USEC_PER_MILLISECOND (G_GINT64_CONSTANT (1000)) - -#ifndef GLIB_HAS_GDATETIME - -#define MAX_SUPPORTED_YEAR 9999 -#define GREGORIAN_LEAP(y) (((y%4)==0)&&(!(((y%100)==0)&&((y%400)!=0)))) - -static const guint16 days_in_months[2][13] = { - {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, - {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} -}; - -struct _GstDateTime +GstDateTime * +gst_date_time_new_from_unix_epoch_utc (gint64 secs) { - /* - * As we don't have a datetime math API, we can have fields split here. - * (There is still some math done internally, but nothing really relevant). - * - * If we ever add one, we should go for a days since some epoch counter. - * (Proleptic Gregorian with 0001-01-01 as day 1) - */ - gint16 year; - gint8 month; - gint8 day; - guint64 usec; /* Microsecond timekeeping within Day */ - - gint tzoffset; - - volatile gint ref_count; -}; + GstDateTime *datetime; + datetime = + gst_date_time_new_from_g_date_time (g_date_time_new_from_unix_utc (secs)); + return datetime; +} -/* - * Returns the utc offset in seconds for this time structure - */ -static gint -gmt_offset (struct tm *tm, time_t t) -{ -#if defined (HAVE_TM_GMTOFF) - return tm->tm_gmtoff; -#else - struct tm g; - time_t t2; -#ifdef HAVE_GMTIME_R - gmtime_r (&t, &g); -#else - g = *gmtime (&t); -#endif - t2 = mktime (&g); - return (int) difftime (t, t2); -#endif +static GstDateTimeFields +gst_date_time_check_fields (gint * year, gint * month, gint * day, + gint * hour, gint * minute, gdouble * seconds) +{ + if (*month == -1) { + *month = *day = 1; + *hour = *minute = *seconds = 0; + return GST_DATE_TIME_FIELDS_Y; + } else if (*day == -1) { + *day = 1; + *hour = *minute = *seconds = 0; + return GST_DATE_TIME_FIELDS_YM; + } else if (*hour == -1) { + *hour = *minute = *seconds = 0; + return GST_DATE_TIME_FIELDS_YMD; + } else if (*seconds == -1) { + *seconds = 0; + return GST_DATE_TIME_FIELDS_YMD_HM; + } else + return GST_DATE_TIME_FIELDS_YMD_HMS; } -static void -gst_date_time_set_local_timezone (GstDateTime * dt) +/** + * gst_date_time_new_local_time: + * @year: the gregorian year + * @month: the gregorian month, or -1 + * @day: the day of the gregorian month, or -1 + * @hour: the hour of the day, or -1 + * @minute: the minute of the hour, or -1 + * @seconds: the second of the minute, or -1 + * + * Creates a new #GstDateTime using the date and times in the gregorian calendar + * in the local timezone. + * + * @year should be from 1 to 9999, @month should be from 1 to 12, @day from + * 1 to 31, @hour from 0 to 23, @minutes and @seconds from 0 to 59. + * + * If @month is -1, then the #GstDateTime created will only contain @year, + * and all other fields will be considered not set. + * + * If @day is -1, then the #GstDateTime created will only contain @year and + * @month and all other fields will be considered not set. + * + * If @hour is -1, then the #GstDateTime created will only contain @year and + * @month and @day, and the time fields will be considered not set. In this + * case @minute and @seconds should also be -1. + * + * Free-function: gst_date_time_unref + * + * Return value: (transfer full) (nullable): the newly created #GstDateTime + */ +GstDateTime * +gst_date_time_new_local_time (gint year, gint month, gint day, gint hour, + gint minute, gdouble seconds) { - struct tm tt; - time_t t; - - g_return_if_fail (dt != NULL); + GstDateTimeFields fields; + GstDateTime *datetime; - memset (&tt, 0, sizeof (tt)); + g_return_val_if_fail (year > 0 && year <= 9999, NULL); + g_return_val_if_fail ((month > 0 && month <= 12) || month == -1, NULL); + g_return_val_if_fail ((day > 0 && day <= 31) || day == -1, NULL); + g_return_val_if_fail ((hour >= 0 && hour < 24) || hour == -1, NULL); + g_return_val_if_fail ((minute >= 0 && minute < 60) || minute == -1, NULL); + g_return_val_if_fail ((seconds >= 0 && seconds < 60) || seconds == -1, NULL); - tt.tm_mday = gst_date_time_get_day (dt); - tt.tm_mon = gst_date_time_get_month (dt) - 1; - tt.tm_year = gst_date_time_get_year (dt) - 1900; - tt.tm_hour = gst_date_time_get_hour (dt); - tt.tm_min = gst_date_time_get_minute (dt); - tt.tm_sec = gst_date_time_get_second (dt); + fields = gst_date_time_check_fields (&year, &month, &day, + &hour, &minute, &seconds); - t = mktime (&tt); + datetime = gst_date_time_new_from_g_date_time (g_date_time_new_local (year, + month, day, hour, minute, seconds)); - dt->tzoffset = gmt_offset (&tt, t) / 60; -} - -static GstDateTime * -gst_date_time_alloc (void) -{ - GstDateTime *datetime; - - datetime = g_slice_new0 (GstDateTime); - datetime->ref_count = 1; - - return datetime; -} - -static void -gst_date_time_free (GstDateTime * datetime) -{ - g_slice_free (GstDateTime, datetime); -} - -static GstDateTime * -gst_date_time_new_from_date (gint year, gint month, gint day) -{ - GstDateTime *dt; - - g_return_val_if_fail (year > 0 && year <= 9999, NULL); - g_return_val_if_fail ((month > 0 && month <= 12), NULL); - g_return_val_if_fail ((day > 0 && day <= 31), NULL); - - dt = gst_date_time_alloc (); - - dt->year = year; - dt->month = month; - dt->day = day; - gst_date_time_set_local_timezone (dt); - - return dt; -} - -gint -gst_date_time_get_year (const GstDateTime * datetime) -{ - g_return_val_if_fail (datetime != NULL, 0); - - return datetime->year; -} - -gint -gst_date_time_get_month (const GstDateTime * datetime) -{ - g_return_val_if_fail (datetime != NULL, 0); - - return datetime->month; -} - -gint -gst_date_time_get_day (const GstDateTime * datetime) -{ - g_return_val_if_fail (datetime != NULL, 0); - - return datetime->day; -} - -gint -gst_date_time_get_hour (const GstDateTime * datetime) -{ - g_return_val_if_fail (datetime != NULL, 0); - return (datetime->usec / GST_DATE_TIME_USEC_PER_HOUR); -} - -gint -gst_date_time_get_microsecond (const GstDateTime * datetime) -{ - g_return_val_if_fail (datetime != NULL, 0); - return (datetime->usec % GST_DATE_TIME_USEC_PER_SECOND); -} - -gint -gst_date_time_get_minute (const GstDateTime * datetime) -{ - g_return_val_if_fail (datetime != NULL, 0); - return (datetime->usec % GST_DATE_TIME_USEC_PER_HOUR) / - GST_DATE_TIME_USEC_PER_MINUTE; -} - -gint -gst_date_time_get_second (const GstDateTime * datetime) -{ - g_return_val_if_fail (datetime != NULL, 0); - return (datetime->usec % GST_DATE_TIME_USEC_PER_MINUTE) / - GST_DATE_TIME_USEC_PER_SECOND; -} - -gfloat -gst_date_time_get_time_zone_offset (const GstDateTime * datetime) -{ - g_return_val_if_fail (datetime != NULL, 0); - - return datetime->tzoffset / 60.0f; + datetime->fields = fields; + return datetime; } +/** + * gst_date_time_new_now_local_time: + * + * Creates a new #GstDateTime representing the current date and time. + * + * Free-function: gst_date_time_unref + * + * Return value: (transfer full): the newly created #GstDateTime which should + * be freed with gst_date_time_unref(). + */ GstDateTime * -gst_date_time_new_from_unix_epoch_local_time (gint64 secs) +gst_date_time_new_now_local_time (void) { - GstDateTime *dt; - struct tm tm; - time_t tt; - - memset (&tm, 0, sizeof (tm)); - tt = (time_t) secs; - -#ifdef HAVE_LOCALTIME_R - localtime_r (&tt, &tm); -#else - memcpy (&tm, localtime (&tt), sizeof (struct tm)); -#endif - - dt = gst_date_time_new (0, tm.tm_year + 1900, - tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec); - gst_date_time_set_local_timezone (dt); - return dt; + return gst_date_time_new_from_g_date_time (g_date_time_new_now_local ()); } +/** + * gst_date_time_new_now_utc: + * + * Creates a new #GstDateTime that represents the current instant at Universal + * coordinated time. + * + * Free-function: gst_date_time_unref + * + * Return value: (transfer full): the newly created #GstDateTime which should + * be freed with gst_date_time_unref(). + */ GstDateTime * -gst_date_time_new_from_unix_epoch_utc (gint64 secs) +gst_date_time_new_now_utc (void) { - GstDateTime *dt; - struct tm tm; - time_t tt; - - memset (&tm, 0, sizeof (tm)); - tt = (time_t) secs; - -#ifdef HAVE_GMTIME_R - gmtime_r (&tt, &tm); -#else - memcpy (&tm, gmtime (&tt), sizeof (struct tm)); -#endif - - dt = gst_date_time_new (0, tm.tm_year + 1900, - tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec); - return dt; + return gst_date_time_new_from_g_date_time (g_date_time_new_now_utc ()); } -GstDateTime * -gst_date_time_new_local_time (gint year, gint month, gint day, gint hour, - gint minute, gdouble seconds) -{ - GstDateTime *dt; - - dt = gst_date_time_new (0, year, month, day, hour, minute, seconds); - - gst_date_time_set_local_timezone (dt); - - return dt; +gint +__gst_date_time_compare (const GstDateTime * dt1, const GstDateTime * dt2) +{ + gint64 diff; + + /* we assume here that GST_DATE_TIME_FIELDS_YMD_HMS is the highest + * resolution, and ignore microsecond differences on purpose for now */ + if (dt1->fields != dt2->fields) + return GST_VALUE_UNORDERED; + + /* This will round down to nearest second, which is what we want. We're + * not comparing microseconds on purpose here, since we're not + * serialising them when doing new_utc_now() + to_string() */ + diff = + g_date_time_to_unix (dt1->datetime) - g_date_time_to_unix (dt2->datetime); + if (diff < 0) + return GST_VALUE_LESS_THAN; + else if (diff > 0) + return GST_VALUE_GREATER_THAN; + else + return GST_VALUE_EQUAL; } +/** + * gst_date_time_new: + * @tzoffset: Offset from UTC in hours. + * @year: the gregorian year + * @month: the gregorian month + * @day: the day of the gregorian month + * @hour: the hour of the day + * @minute: the minute of the hour + * @seconds: the second of the minute + * + * Creates a new #GstDateTime using the date and times in the gregorian calendar + * in the supplied timezone. + * + * @year should be from 1 to 9999, @month should be from 1 to 12, @day from + * 1 to 31, @hour from 0 to 23, @minutes and @seconds from 0 to 59. + * + * Note that @tzoffset is a float and was chosen so for being able to handle + * some fractional timezones, while it still keeps the readability of + * representing it in hours for most timezones. + * + * If value is -1 then all over value will be ignored. For example + * if @month == -1, then #GstDateTime will created only for @year. If + * @day == -1, then #GstDateTime will created for @year and @month and + * so on. + * + * Free-function: gst_date_time_unref + * + * Return value: (transfer full) (nullable): the newly created #GstDateTime + */ GstDateTime * gst_date_time_new (gfloat tzoffset, gint year, gint month, gint day, gint hour, gint minute, gdouble seconds) { - GstDateTime *dt; + GstDateTimeFields fields; + gchar buf[6]; + GTimeZone *tz; + GDateTime *dt; + GstDateTime *datetime; + gint tzhour, tzminute; - g_return_val_if_fail (hour >= 0 && hour < 24, NULL); - g_return_val_if_fail (minute >= 0 && minute < 60, NULL); - g_return_val_if_fail (seconds >= 0 && seconds < 60, NULL); + g_return_val_if_fail (year > 0 && year <= 9999, NULL); + g_return_val_if_fail ((month > 0 && month <= 12) || month == -1, NULL); + g_return_val_if_fail ((day > 0 && day <= 31) || day == -1, NULL); + g_return_val_if_fail ((hour >= 0 && hour < 24) || hour == -1, NULL); + g_return_val_if_fail ((minute >= 0 && minute < 60) || minute == -1, NULL); + g_return_val_if_fail ((seconds >= 0 && seconds < 60) || seconds == -1, NULL); g_return_val_if_fail (tzoffset >= -12.0 && tzoffset <= 12.0, NULL); + g_return_val_if_fail ((hour >= 0 && minute >= 0) || + (hour == -1 && minute == -1 && seconds == -1 && tzoffset == 0.0), NULL); - if (!(dt = gst_date_time_new_from_date (year, month, day))) - return NULL; + tzhour = (gint) ABS (tzoffset); + tzminute = (gint) ((ABS (tzoffset) - tzhour) * 60); + + g_snprintf (buf, 6, "%c%02d%02d", tzoffset >= 0 ? '+' : '-', tzhour, + tzminute); - dt->usec = (hour * GST_DATE_TIME_USEC_PER_HOUR) - + (minute * GST_DATE_TIME_USEC_PER_MINUTE) - + (guint64) (floor (seconds * GST_DATE_TIME_USEC_PER_SECOND + 0.5)); + tz = g_time_zone_new (buf); - /* we store in minutes */ - dt->tzoffset = (gint) tzoffset *60.0; + fields = gst_date_time_check_fields (&year, &month, &day, + &hour, &minute, &seconds); - return dt; -} + dt = g_date_time_new (tz, year, month, day, hour, minute, seconds); + g_time_zone_unref (tz); -GstDateTime * -gst_date_time_new_now_local_time (void) -{ - GstDateTime *datetime; - GTimeVal tv; - g_get_current_time (&tv); + datetime = gst_date_time_new_from_g_date_time (dt); + datetime->fields = fields; - datetime = gst_date_time_new_from_unix_epoch_local_time (tv.tv_sec); - datetime->usec += tv.tv_usec; - gst_date_time_set_local_timezone (datetime); return datetime; } -static GstDateTime * -gst_date_time_copy (const GstDateTime * dt) +gchar * +__gst_date_time_serialize (GstDateTime * datetime, gboolean serialize_usecs) { - GstDateTime *copy = gst_date_time_alloc (); + GString *s; + gfloat gmt_offset; + guint msecs; - memcpy (copy, dt, sizeof (GstDateTime)); - copy->ref_count = 1; + /* we always have at least the year */ + s = g_string_new (NULL); + g_string_append_printf (s, "%04u", gst_date_time_get_year (datetime)); - return copy; -} + if (datetime->fields == GST_DATE_TIME_FIELDS_Y) + goto done; -static GstDateTime * -gst_date_time_to_utc (const GstDateTime * dt) -{ - GstDateTime *utc; - gint64 usec; - gint days; - gint leap; + /* add month */ + g_string_append_printf (s, "-%02u", gst_date_time_get_month (datetime)); - g_return_val_if_fail (dt != NULL, NULL); + if (datetime->fields == GST_DATE_TIME_FIELDS_YM) + goto done; - utc = gst_date_time_copy (dt); + /* add day of month */ + g_string_append_printf (s, "-%02u", gst_date_time_get_day (datetime)); - usec = dt->usec - dt->tzoffset * GST_DATE_TIME_USEC_PER_MINUTE; - days = usec / GST_DATE_TIME_USEC_PER_DAY; - if (usec < 0) - days--; - utc->day += days; + if (datetime->fields == GST_DATE_TIME_FIELDS_YMD) + goto done; - leap = GREGORIAN_LEAP (utc->year) ? 1 : 0; + /* add time */ + g_string_append_printf (s, "T%02u:%02u", gst_date_time_get_hour (datetime), + gst_date_time_get_minute (datetime)); - /* check if we should update month/year */ - if (utc->day < 1) { - if (utc->month == 1) { - utc->year--; - utc->month = 12; - } else { - utc->month--; - } - if (GREGORIAN_LEAP (utc->year)) - utc->day = days_in_months[1][utc->month]; - else - utc->day = days_in_months[0][utc->month]; - } else if (utc->day > days_in_months[leap][utc->month]) { - if (utc->month == 12) { - utc->year++; - utc->month = 1; - } else { - utc->month++; - } - utc->day = 1; - } + if (datetime->fields == GST_DATE_TIME_FIELDS_YMD_HM) + goto add_timezone; - if (usec < 0) - utc->usec = - GST_DATE_TIME_USEC_PER_DAY + (usec % GST_DATE_TIME_USEC_PER_DAY); - else - utc->usec = usec % GST_DATE_TIME_USEC_PER_DAY; + /* add seconds */ + g_string_append_printf (s, ":%02u", gst_date_time_get_second (datetime)); - return utc; -} + /* add microseconds */ + if (serialize_usecs) { + msecs = gst_date_time_get_microsecond (datetime); + if (msecs != 0) { + g_string_append_printf (s, ".%06u", msecs); + /* trim trailing 0s */ + while (s->str[s->len - 1] == '0') + g_string_truncate (s, s->len - 1); + } + } -GstDateTime * -gst_date_time_new_now_utc (void) -{ - GstDateTime *now, *utc; + /* add timezone */ - now = gst_date_time_new_now_local_time (); - utc = gst_date_time_to_utc (now); - gst_date_time_unref (now); - return utc; -} +add_timezone: -gint -priv_gst_date_time_compare (gconstpointer dt1, gconstpointer dt2) -{ - GstDateTime *a, *b; - gint res = 0; + gmt_offset = gst_date_time_get_time_zone_offset (datetime); + if (gmt_offset == 0) { + g_string_append_c (s, 'Z'); + } else { + guint tzhour, tzminute; - a = gst_date_time_to_utc (dt1); - b = gst_date_time_to_utc (dt2); + tzhour = (guint) ABS (gmt_offset); + tzminute = (guint) ((ABS (gmt_offset) - tzhour) * 60); -#define GST_DATE_TIME_COMPARE_VALUE(a,b,v) \ - if ((a)->v > (b)->v) { \ - res = 1; \ - goto done; \ - } else if ((a)->v < (b)->v) { \ - res = -1; \ - goto done; \ + g_string_append_c (s, (gmt_offset >= 0) ? '+' : '-'); + g_string_append_printf (s, "%02u%02u", tzhour, tzminute); } - GST_DATE_TIME_COMPARE_VALUE (a, b, year); - GST_DATE_TIME_COMPARE_VALUE (a, b, month); - GST_DATE_TIME_COMPARE_VALUE (a, b, day); - GST_DATE_TIME_COMPARE_VALUE (a, b, usec); - -#undef GST_DATE_TIME_COMPARE_VALUE - done: - gst_date_time_unref (a); - gst_date_time_unref (b); - return res; -} - -#else -struct _GstDateTime -{ - GDateTime *datetime; - - volatile gint ref_count; -}; + return g_string_free (s, FALSE); +} -static GstDateTime * -gst_date_time_new_from_gdatetime (GDateTime * dt) +/** + * gst_date_time_to_iso8601_string: + * @datetime: GstDateTime. + * + * 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: (nullable): a newly allocated string formatted according + * to ISO 8601 and only including the datetime fields that are + * valid, or %NULL in case there was an error. The string should + * be freed with g_free(). + */ +gchar * +gst_date_time_to_iso8601_string (GstDateTime * datetime) { - GstDateTime *gst_dt; + g_return_val_if_fail (datetime != NULL, NULL); - if (!dt) + if (datetime->fields == GST_DATE_TIME_FIELDS_INVALID) return NULL; - gst_dt = g_slice_new (GstDateTime); - gst_dt->datetime = dt; - gst_dt->ref_count = 1; - return gst_dt; -} - -gint -gst_date_time_get_year (const GstDateTime * datetime) -{ - return g_date_time_get_year (datetime->datetime); + return __gst_date_time_serialize (datetime, FALSE); } -gint -gst_date_time_get_month (const GstDateTime * datetime) +/** + * gst_date_time_new_from_iso8601_string: + * @string: ISO 8601-formatted datetime string. + * + * Tries to parse common variants of ISO-8601 datetime strings into a + * #GstDateTime. 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. + * + * Free-function: gst_date_time_unref + * + * Returns: (transfer full) (nullable): a newly created #GstDateTime, + * or %NULL on error + */ +GstDateTime * +gst_date_time_new_from_iso8601_string (const gchar * string) { - return g_date_time_get_month (datetime->datetime); -} + gint year = -1, month = -1, day = -1, hour = -1, minute = -1; + gint gmt_offset_hour = -99, gmt_offset_min = -99; + gdouble second = -1.0; + gfloat tzoffset = 0.0; + guint64 usecs; + gint len, ret; -gint -gst_date_time_get_day (const GstDateTime * datetime) -{ - return g_date_time_get_day_of_month (datetime->datetime); -} + g_return_val_if_fail (string != NULL, NULL); -gint -gst_date_time_get_hour (const GstDateTime * datetime) -{ - return g_date_time_get_hour (datetime->datetime); -} - -gint -gst_date_time_get_minute (const GstDateTime * datetime) -{ - return g_date_time_get_minute (datetime->datetime); -} + GST_DEBUG ("Parsing '%s' into a datetime", string); -gint -gst_date_time_get_second (const GstDateTime * datetime) -{ - return g_date_time_get_second (datetime->datetime); -} + len = strlen (string); -gint -gst_date_time_get_microsecond (const GstDateTime * datetime) -{ - return g_date_time_get_microsecond (datetime->datetime); -} + /* The input string is expected to start either with a year (4 digits) or + * with an hour (2 digits). Hour must be followed by minute. In any case, + * the string must be at least 4 characters long and start with 2 digits */ + if (len < 4 || !g_ascii_isdigit (string[0]) || !g_ascii_isdigit (string[1])) + return NULL; -gfloat -gst_date_time_get_time_zone_offset (const GstDateTime * datetime) -{ - return g_date_time_get_utc_offset (datetime->datetime) / - (G_USEC_PER_SEC * G_GINT64_CONSTANT (3600)); -} + if (g_ascii_isdigit (string[2]) && g_ascii_isdigit (string[3])) { + ret = sscanf (string, "%04d-%02d-%02d", &year, &month, &day); -GstDateTime * -gst_date_time_new_from_unix_epoch_local_time (gint64 secs) -{ - return - gst_date_time_new_from_gdatetime (g_date_time_new_from_unix_local (secs)); -} + if (ret == 0) + return NULL; -GstDateTime * -gst_date_time_new_from_unix_epoch_utc (gint64 secs) -{ - return - gst_date_time_new_from_gdatetime (g_date_time_new_from_unix_utc (secs)); -} + if (ret == 3 && day <= 0) { + ret = 2; + day = -1; + } -GstDateTime * -gst_date_time_new_local_time (gint year, gint month, gint day, gint hour, - gint minute, gdouble seconds) -{ - return gst_date_time_new_from_gdatetime (g_date_time_new_local (year, month, - day, hour, minute, seconds)); -} + if (ret >= 2 && month <= 0) { + ret = 1; + month = day = -1; + } -GstDateTime * -gst_date_time_new_now_local_time (void) -{ - return gst_date_time_new_from_gdatetime (g_date_time_new_now_local ()); -} + if (ret >= 1 && (year <= 0 || year > 9999 || month > 12 || day > 31)) + return NULL; -GstDateTime * -gst_date_time_new_now_utc (void) -{ - return gst_date_time_new_from_gdatetime (g_date_time_new_now_utc ()); -} + else if (ret >= 1 && len < 16) + /* YMD is 10 chars. XMD + HM will be 16 chars. if it is less, + * it make no sense to continue. We will stay with YMD. */ + goto ymd; -gint -priv_gst_date_time_compare (gconstpointer dt1, gconstpointer dt2) -{ - const GstDateTime *datetime1 = dt1; - const GstDateTime *datetime2 = dt2; - return g_date_time_compare (datetime1->datetime, datetime2->datetime); -} + string += 10; + /* Exit if there is no expeceted value on this stage */ + if (!(*string == 'T' || *string == '-' || *string == ' ')) + goto ymd; -GstDateTime * -gst_date_time_new (gfloat tzoffset, gint year, gint month, gint day, gint hour, - gint minute, gdouble seconds) -{ - gchar buf[6]; - GTimeZone *tz; - GDateTime *dt; - gint tzhour, tzminute; - - tzhour = (gint) ABS (tzoffset); - tzminute = (gint) ((ABS (tzoffset) - tzhour) * 60); + string += 1; + } + /* if hour or minute fails, then we will use only ymd. */ + hour = g_ascii_strtoull (string, (gchar **) & string, 10); + if (hour > 24 || *string != ':') + goto ymd; + + /* minute */ + minute = g_ascii_strtoull (string + 1, (gchar **) & string, 10); + if (minute > 59) + goto ymd; + + /* second */ + if (*string == ':') { + second = g_ascii_strtoull (string + 1, (gchar **) & string, 10); + /* if we fail here, we still can reuse hour and minute. We + * will still attempt to parse any timezone information */ + if (second > 59) { + second = -1.0; + } else { + /* microseconds */ + if (*string == '.' || *string == ',') { + const gchar *usec_start = string + 1; + guint digits; + + usecs = g_ascii_strtoull (string + 1, (gchar **) & string, 10); + if (usecs != G_MAXUINT64 && string > usec_start) { + digits = (guint) (string - usec_start); + second += (gdouble) usecs / pow (10.0, digits); + } + } + } + } - g_snprintf (buf, 6, "%c%02d%02d", tzoffset >= 0 ? '+' : '-', tzhour, - tzminute); + if (*string == 'Z') + goto ymd_hms; + else { + /* reuse some code from gst-plugins-base/gst-libs/gst/tag/gstxmptag.c */ + gint gmt_offset = -1; + gchar *plus_pos = NULL; + gchar *neg_pos = NULL; + gchar *pos = NULL; + + GST_LOG ("Checking for timezone information"); + + /* check if there is timezone info */ + plus_pos = strrchr (string, '+'); + neg_pos = strrchr (string, '-'); + if (plus_pos) + pos = plus_pos + 1; + else if (neg_pos) + pos = neg_pos + 1; + + if (pos && strlen (pos) >= 3) { + gint ret_tz; + if (pos[2] == ':') + ret_tz = sscanf (pos, "%d:%d", &gmt_offset_hour, &gmt_offset_min); + else + ret_tz = sscanf (pos, "%02d%02d", &gmt_offset_hour, &gmt_offset_min); + + GST_DEBUG ("Parsing timezone: %s", pos); + + if (ret_tz == 2) { + if (neg_pos != NULL && neg_pos + 1 == pos) { + gmt_offset_hour *= -1; + gmt_offset_min *= -1; + } + gmt_offset = gmt_offset_hour * 60 + gmt_offset_min; + + tzoffset = gmt_offset / 60.0; + + GST_LOG ("Timezone offset: %f (%d minutes)", tzoffset, gmt_offset); + } else + GST_WARNING ("Failed to parse timezone information"); + } + } - tz = g_time_zone_new (buf); - dt = g_date_time_new (tz, year, month, day, hour, minute, seconds); - g_time_zone_unref (tz); - return gst_date_time_new_from_gdatetime (dt); +ymd_hms: + if (year == -1 || month == -1 || day == -1) { + GDateTime *now_utc, *now_in_given_tz; + + /* No date was supplied: make it today */ + now_utc = g_date_time_new_now_utc (); + if (tzoffset != 0.0) { + /* If a timezone offset was supplied, get the date of that timezone */ + g_assert (gmt_offset_min != -99); + g_assert (gmt_offset_hour != -99); + now_in_given_tz = + g_date_time_add_minutes (now_utc, + (60 * gmt_offset_hour) + gmt_offset_min); + g_date_time_unref (now_utc); + } else { + now_in_given_tz = now_utc; + } + g_date_time_get_ymd (now_in_given_tz, &year, &month, &day); + g_date_time_unref (now_in_given_tz); + } + return gst_date_time_new (tzoffset, year, month, day, hour, minute, second); +ymd: + if (year == -1) { + /* No date was supplied and time failed to parse */ + return NULL; + } + return gst_date_time_new_ymd (year, month, day); } static void gst_date_time_free (GstDateTime * datetime) { g_date_time_unref (datetime->datetime); - g_slice_free (GstDateTime, datetime); -} +#ifdef USE_POISONING + memset (datetime, 0xff, sizeof (GstDateTime)); #endif + g_slice_free (GstDateTime, datetime); +} + /** * gst_date_time_ref: * @datetime: a #GstDateTime * * Atomically increments the reference count of @datetime by one. * - * Return value: the reference @datetime - * - * Since: 0.10.31 + * Return value: (transfer full): the reference @datetime */ GstDateTime * gst_date_time_ref (GstDateTime * datetime) { - g_return_val_if_fail (datetime != NULL, NULL); - g_return_val_if_fail (datetime->ref_count > 0, NULL); - g_atomic_int_inc (&datetime->ref_count); - return datetime; + return (GstDateTime *) gst_mini_object_ref (GST_MINI_OBJECT_CAST (datetime)); } /** * gst_date_time_unref: - * @datetime: a #GstDateTime + * @datetime: (transfer full): a #GstDateTime * * Atomically decrements the reference count of @datetime by one. When the * reference count reaches zero, the structure is freed. - * - * Since: 0.10.31 */ void gst_date_time_unref (GstDateTime * datetime) { - g_return_if_fail (datetime != NULL); - g_return_if_fail (datetime->ref_count > 0); + gst_mini_object_unref (GST_MINI_OBJECT_CAST (datetime)); +} - if (g_atomic_int_dec_and_test (&datetime->ref_count)) - gst_date_time_free (datetime); +void +_priv_gst_date_time_initialize (void) +{ + _gst_date_time_type = gst_date_time_get_type (); }