2010-07-16 17:09:12 +00:00
|
|
|
/* GStreamer
|
|
|
|
* Copyright (C) 2010 Thiago Santos <thiago.sousa.santos@collabora.co.uk>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
2012-11-03 20:44:48 +00:00
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
2010-07-16 17:09:12 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "gst_private.h"
|
2013-10-15 09:44:05 +00:00
|
|
|
#include "glib-compat-private.h"
|
2010-07-16 17:09:12 +00:00
|
|
|
#include "gstdatetime.h"
|
2012-06-27 12:16:07 +00:00
|
|
|
#include "gstvalue.h"
|
2010-09-27 22:35:08 +00:00
|
|
|
#include <glib.h>
|
2010-10-12 18:13:48 +00:00
|
|
|
#include <math.h>
|
2012-06-18 06:06:49 +00:00
|
|
|
#include <stdio.h>
|
2010-09-27 22:35:08 +00:00
|
|
|
|
2010-07-16 17:09:12 +00:00
|
|
|
/**
|
2010-09-13 08:17:34 +00:00
|
|
|
* SECTION:gstdatetime
|
2010-07-16 17:09:12 +00:00
|
|
|
* @title: GstDateTime
|
|
|
|
* @short_description: A date, time and timezone structure
|
|
|
|
*
|
|
|
|
* Struct to store date, time and timezone information altogether.
|
|
|
|
* #GstDateTime is refcounted and immutable.
|
|
|
|
*
|
|
|
|
* Date information is handled using the proleptic Gregorian calendar.
|
|
|
|
*
|
|
|
|
* Provides basic creation functions and accessor functions to its fields.
|
|
|
|
*/
|
|
|
|
|
2012-06-12 20:35:42 +00:00
|
|
|
typedef enum
|
|
|
|
{
|
2012-06-12 22:52:02 +00:00
|
|
|
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
|
2012-06-27 12:16:07 +00:00
|
|
|
/* Note: if we ever add more granularity here, e.g. for microsecs,
|
|
|
|
* the compare function will need updating */
|
2012-06-12 22:52:02 +00:00
|
|
|
} GstDateTimeFields;
|
2012-06-12 20:35:42 +00:00
|
|
|
|
2012-06-09 17:05:14 +00:00
|
|
|
struct _GstDateTime
|
|
|
|
{
|
2014-06-03 22:38:28 +00:00
|
|
|
GstMiniObject mini_object;
|
|
|
|
|
2012-06-09 17:05:14 +00:00
|
|
|
GDateTime *datetime;
|
|
|
|
|
2012-06-12 22:52:02 +00:00
|
|
|
GstDateTimeFields fields;
|
2012-06-09 17:05:14 +00:00
|
|
|
};
|
|
|
|
|
2014-06-19 06:05:03 +00:00
|
|
|
GType _gst_date_time_type = 0;
|
2014-06-03 22:38:28 +00:00
|
|
|
GST_DEFINE_MINI_OBJECT_TYPE (GstDateTime, gst_date_time);
|
|
|
|
|
|
|
|
static void gst_date_time_free (GstDateTime * datetime);
|
|
|
|
|
2012-06-30 01:52:47 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*
|
2014-06-11 22:21:34 +00:00
|
|
|
* Returns: (transfer full) (nullable): a newly created #GstDateTime,
|
|
|
|
* or %NULL on error
|
2012-06-30 01:52:47 +00:00
|
|
|
*/
|
|
|
|
GstDateTime *
|
|
|
|
gst_date_time_new_from_g_date_time (GDateTime * dt)
|
2012-06-09 17:05:14 +00:00
|
|
|
{
|
|
|
|
GstDateTime *gst_dt;
|
|
|
|
|
|
|
|
if (!dt)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
gst_dt = g_slice_new (GstDateTime);
|
2014-06-03 22:38:28 +00:00
|
|
|
|
|
|
|
gst_mini_object_init (GST_MINI_OBJECT_CAST (gst_dt), 0, GST_TYPE_DATE_TIME,
|
|
|
|
NULL, NULL, (GstMiniObjectFreeFunction) gst_date_time_free);
|
|
|
|
|
2012-06-09 17:05:14 +00:00
|
|
|
gst_dt->datetime = dt;
|
2012-06-12 22:52:02 +00:00
|
|
|
gst_dt->fields = GST_DATE_TIME_FIELDS_YMD_HMS;
|
2012-06-09 17:05:14 +00:00
|
|
|
return gst_dt;
|
|
|
|
}
|
|
|
|
|
2012-06-30 01:52:47 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*
|
2014-06-11 22:21:34 +00:00
|
|
|
* Returns: (transfer full) (nullable): a newly created #GDateTime, or
|
|
|
|
* %NULL on error
|
2012-06-30 01:52:47 +00:00
|
|
|
*/
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2012-06-12 20:35:42 +00:00
|
|
|
/**
|
|
|
|
* gst_date_time_has_year:
|
|
|
|
* @datetime: a #GstDateTime
|
|
|
|
*
|
2014-05-29 21:54:34 +00:00
|
|
|
* Returns: %TRUE if @datetime<!-- -->'s year field is set (which should always
|
|
|
|
* be the case), otherwise %FALSE
|
2012-06-12 20:35:42 +00:00
|
|
|
*/
|
|
|
|
gboolean
|
|
|
|
gst_date_time_has_year (const GstDateTime * datetime)
|
|
|
|
{
|
2012-06-12 22:52:02 +00:00
|
|
|
g_return_val_if_fail (datetime != NULL, FALSE);
|
|
|
|
|
|
|
|
return (datetime->fields >= GST_DATE_TIME_FIELDS_Y);
|
2012-06-12 20:35:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_date_time_has_month:
|
|
|
|
* @datetime: a #GstDateTime
|
|
|
|
*
|
2014-05-29 21:54:34 +00:00
|
|
|
* Returns: %TRUE if @datetime<!-- -->'s month field is set, otherwise %FALSE
|
2012-06-12 20:35:42 +00:00
|
|
|
*/
|
|
|
|
gboolean
|
|
|
|
gst_date_time_has_month (const GstDateTime * datetime)
|
|
|
|
{
|
2012-06-12 22:52:02 +00:00
|
|
|
g_return_val_if_fail (datetime != NULL, FALSE);
|
|
|
|
|
|
|
|
return (datetime->fields >= GST_DATE_TIME_FIELDS_YM);
|
2012-06-12 20:35:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_date_time_has_day:
|
|
|
|
* @datetime: a #GstDateTime
|
|
|
|
*
|
2014-05-29 21:54:34 +00:00
|
|
|
* Returns: %TRUE if @datetime<!-- -->'s day field is set, otherwise %FALSE
|
2012-06-12 20:35:42 +00:00
|
|
|
*/
|
|
|
|
gboolean
|
|
|
|
gst_date_time_has_day (const GstDateTime * datetime)
|
|
|
|
{
|
2012-06-12 22:52:02 +00:00
|
|
|
g_return_val_if_fail (datetime != NULL, FALSE);
|
2012-06-12 20:35:42 +00:00
|
|
|
|
2012-06-12 22:52:02 +00:00
|
|
|
return (datetime->fields >= GST_DATE_TIME_FIELDS_YMD);
|
2012-06-12 20:35:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-06-12 22:52:02 +00:00
|
|
|
* gst_date_time_has_time:
|
2012-06-12 20:35:42 +00:00
|
|
|
* @datetime: a #GstDateTime
|
|
|
|
*
|
2014-05-29 21:54:34 +00:00
|
|
|
* Returns: %TRUE if @datetime<!-- -->'s hour and minute fields are set,
|
|
|
|
* otherwise %FALSE
|
2012-06-12 20:35:42 +00:00
|
|
|
*/
|
|
|
|
gboolean
|
2012-06-12 22:52:02 +00:00
|
|
|
gst_date_time_has_time (const GstDateTime * datetime)
|
2012-06-12 20:35:42 +00:00
|
|
|
{
|
2012-06-12 22:52:02 +00:00
|
|
|
g_return_val_if_fail (datetime != NULL, FALSE);
|
|
|
|
|
|
|
|
return (datetime->fields >= GST_DATE_TIME_FIELDS_YMD_HM);
|
2012-06-12 20:35:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_date_time_has_second:
|
|
|
|
* @datetime: a #GstDateTime
|
|
|
|
*
|
2014-05-29 21:54:34 +00:00
|
|
|
* Returns: %TRUE if @datetime<!-- -->'s second field is set, otherwise %FALSE
|
2012-06-12 20:35:42 +00:00
|
|
|
*/
|
|
|
|
gboolean
|
|
|
|
gst_date_time_has_second (const GstDateTime * datetime)
|
|
|
|
{
|
2012-06-12 22:52:02 +00:00
|
|
|
g_return_val_if_fail (datetime != NULL, FALSE);
|
|
|
|
|
|
|
|
return (datetime->fields >= GST_DATE_TIME_FIELDS_YMD_HMS);
|
2012-06-12 20:35:42 +00:00
|
|
|
}
|
|
|
|
|
2010-10-11 19:15:29 +00:00
|
|
|
/**
|
|
|
|
* gst_date_time_get_year:
|
|
|
|
* @datetime: a #GstDateTime
|
|
|
|
*
|
|
|
|
* Returns the year of this #GstDateTime
|
2018-05-21 09:37:00 +00:00
|
|
|
* Call gst_date_time_has_year() before, to avoid warnings.
|
2010-10-11 19:15:29 +00:00
|
|
|
*
|
|
|
|
* Return value: The year of this #GstDateTime
|
|
|
|
*/
|
2012-06-09 17:05:14 +00:00
|
|
|
gint
|
|
|
|
gst_date_time_get_year (const GstDateTime * datetime)
|
|
|
|
{
|
2012-06-12 22:52:02 +00:00
|
|
|
g_return_val_if_fail (datetime != NULL, 0);
|
|
|
|
|
2012-06-09 17:05:14 +00:00
|
|
|
return g_date_time_get_year (datetime->datetime);
|
|
|
|
}
|
2010-10-11 19:15:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_date_time_get_month:
|
|
|
|
* @datetime: a #GstDateTime
|
|
|
|
*
|
|
|
|
* Returns the month of this #GstDateTime. January is 1, February is 2, etc..
|
2018-05-21 09:37:00 +00:00
|
|
|
* Call gst_date_time_has_month() before, to avoid warnings.
|
2010-10-11 19:15:29 +00:00
|
|
|
*
|
|
|
|
* Return value: The month of this #GstDateTime
|
|
|
|
*/
|
2012-06-09 17:05:14 +00:00
|
|
|
gint
|
|
|
|
gst_date_time_get_month (const GstDateTime * datetime)
|
|
|
|
{
|
2012-06-12 22:52:02 +00:00
|
|
|
g_return_val_if_fail (datetime != NULL, 0);
|
2012-06-12 20:35:42 +00:00
|
|
|
g_return_val_if_fail (gst_date_time_has_month (datetime), 0);
|
2012-06-12 22:52:02 +00:00
|
|
|
|
2012-06-09 17:05:14 +00:00
|
|
|
return g_date_time_get_month (datetime->datetime);
|
|
|
|
}
|
2010-10-11 19:15:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_date_time_get_day:
|
|
|
|
* @datetime: a #GstDateTime
|
|
|
|
*
|
2012-06-30 01:52:47 +00:00
|
|
|
* Returns the day of the month of this #GstDateTime.
|
2018-05-21 09:37:00 +00:00
|
|
|
* Call gst_date_time_has_day() before, to avoid warnings.
|
2010-10-11 19:15:29 +00:00
|
|
|
*
|
|
|
|
* Return value: The day of this #GstDateTime
|
|
|
|
*/
|
2012-06-09 17:05:14 +00:00
|
|
|
gint
|
|
|
|
gst_date_time_get_day (const GstDateTime * datetime)
|
|
|
|
{
|
2012-06-12 22:52:02 +00:00
|
|
|
g_return_val_if_fail (datetime != NULL, 0);
|
2012-06-12 20:35:42 +00:00
|
|
|
g_return_val_if_fail (gst_date_time_has_day (datetime), 0);
|
2012-06-12 22:52:02 +00:00
|
|
|
|
2012-06-09 17:05:14 +00:00
|
|
|
return g_date_time_get_day_of_month (datetime->datetime);
|
|
|
|
}
|
2010-10-11 19:15:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_date_time_get_hour:
|
|
|
|
* @datetime: a #GstDateTime
|
|
|
|
*
|
|
|
|
* Retrieves the hour of the day represented by @datetime in the gregorian
|
|
|
|
* calendar. The return is in the range of 0 to 23.
|
2018-05-21 09:37:00 +00:00
|
|
|
* Call gst_date_time_has_time() before, to avoid warnings.
|
2010-10-11 19:15:29 +00:00
|
|
|
*
|
|
|
|
* Return value: the hour of the day
|
|
|
|
*/
|
2012-06-09 17:05:14 +00:00
|
|
|
gint
|
|
|
|
gst_date_time_get_hour (const GstDateTime * datetime)
|
|
|
|
{
|
2012-06-12 22:52:02 +00:00
|
|
|
g_return_val_if_fail (datetime != NULL, 0);
|
|
|
|
g_return_val_if_fail (gst_date_time_has_time (datetime), 0);
|
|
|
|
|
2012-06-09 17:05:14 +00:00
|
|
|
return g_date_time_get_hour (datetime->datetime);
|
|
|
|
}
|
2010-10-11 19:15:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_date_time_get_minute:
|
|
|
|
* @datetime: a #GstDateTime
|
|
|
|
*
|
|
|
|
* Retrieves the minute of the hour represented by @datetime in the gregorian
|
|
|
|
* calendar.
|
2018-05-21 09:37:00 +00:00
|
|
|
* Call gst_date_time_has_time() before, to avoid warnings.
|
2010-10-11 19:15:29 +00:00
|
|
|
*
|
|
|
|
* Return value: the minute of the hour
|
|
|
|
*/
|
2012-06-09 17:05:14 +00:00
|
|
|
gint
|
|
|
|
gst_date_time_get_minute (const GstDateTime * datetime)
|
|
|
|
{
|
2012-06-12 22:52:02 +00:00
|
|
|
g_return_val_if_fail (datetime != NULL, 0);
|
|
|
|
g_return_val_if_fail (gst_date_time_has_time (datetime), 0);
|
|
|
|
|
2012-06-09 17:05:14 +00:00
|
|
|
return g_date_time_get_minute (datetime->datetime);
|
|
|
|
}
|
2010-10-11 19:15:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_date_time_get_second:
|
|
|
|
* @datetime: a #GstDateTime
|
|
|
|
*
|
|
|
|
* Retrieves the second of the minute represented by @datetime in the gregorian
|
|
|
|
* calendar.
|
2018-05-21 09:37:00 +00:00
|
|
|
* Call gst_date_time_has_time() before, to avoid warnings.
|
2010-10-11 19:15:29 +00:00
|
|
|
*
|
|
|
|
* Return value: the second represented by @datetime
|
|
|
|
*/
|
2012-06-09 17:05:14 +00:00
|
|
|
gint
|
|
|
|
gst_date_time_get_second (const GstDateTime * datetime)
|
|
|
|
{
|
2012-06-12 22:52:02 +00:00
|
|
|
g_return_val_if_fail (datetime != NULL, 0);
|
2012-06-12 20:35:42 +00:00
|
|
|
g_return_val_if_fail (gst_date_time_has_second (datetime), 0);
|
2012-06-12 22:52:02 +00:00
|
|
|
|
2012-06-09 17:05:14 +00:00
|
|
|
return g_date_time_get_second (datetime->datetime);
|
|
|
|
}
|
2010-10-11 19:15:29 +00:00
|
|
|
|
|
|
|
/**
|
2012-06-09 17:05:14 +00:00
|
|
|
* gst_date_time_get_microsecond:
|
2010-10-11 19:15:29 +00:00
|
|
|
* @datetime: a #GstDateTime
|
|
|
|
*
|
2012-06-09 17:05:14 +00:00
|
|
|
* Retrieves the fractional part of the seconds in microseconds represented by
|
|
|
|
* @datetime in the gregorian calendar.
|
2010-10-11 19:15:29 +00:00
|
|
|
*
|
2012-06-09 17:05:14 +00:00
|
|
|
* Return value: the microsecond of the second
|
2010-10-11 19:15:29 +00:00
|
|
|
*/
|
2012-06-09 17:05:14 +00:00
|
|
|
gint
|
|
|
|
gst_date_time_get_microsecond (const GstDateTime * datetime)
|
|
|
|
{
|
2012-06-12 22:52:02 +00:00
|
|
|
g_return_val_if_fail (datetime != NULL, 0);
|
|
|
|
g_return_val_if_fail (gst_date_time_has_second (datetime), 0);
|
|
|
|
|
2012-06-09 17:05:14 +00:00
|
|
|
return g_date_time_get_microsecond (datetime->datetime);
|
|
|
|
}
|
2010-10-11 19:15:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_date_time_get_time_zone_offset:
|
|
|
|
* @datetime: a #GstDateTime
|
|
|
|
*
|
|
|
|
* Retrieves the offset from UTC in hours that the timezone specified
|
|
|
|
* by @datetime represents. Timezones ahead (to the east) of UTC have positive
|
|
|
|
* values, timezones before (to the west) of UTC have negative values.
|
|
|
|
* If @datetime represents UTC time, then the offset is zero.
|
|
|
|
*
|
|
|
|
* Return value: the offset from UTC in hours
|
|
|
|
*/
|
2012-06-09 17:05:14 +00:00
|
|
|
gfloat
|
|
|
|
gst_date_time_get_time_zone_offset (const GstDateTime * datetime)
|
|
|
|
{
|
2012-06-12 22:52:02 +00:00
|
|
|
g_return_val_if_fail (datetime != NULL, 0.0);
|
|
|
|
g_return_val_if_fail (gst_date_time_has_time (datetime), 0.0);
|
|
|
|
|
2012-06-09 17:05:14 +00:00
|
|
|
return (g_date_time_get_utc_offset (datetime->datetime) /
|
|
|
|
G_USEC_PER_SEC) / 3600.0;
|
|
|
|
}
|
2010-10-11 19:15:29 +00:00
|
|
|
|
2012-06-12 20:35:42 +00:00
|
|
|
/**
|
|
|
|
* gst_date_time_new_y:
|
|
|
|
* @year: the gregorian year
|
|
|
|
*
|
|
|
|
* Creates a new #GstDateTime using the date and times in the gregorian calendar
|
|
|
|
* in the local timezone.
|
|
|
|
*
|
|
|
|
* @year should be from 1 to 9999.
|
|
|
|
*
|
|
|
|
* Free-function: gst_date_time_unref
|
|
|
|
*
|
|
|
|
* Return value: (transfer full): the newly created #GstDateTime
|
|
|
|
*/
|
|
|
|
GstDateTime *
|
|
|
|
gst_date_time_new_y (gint year)
|
|
|
|
{
|
2012-06-12 22:52:02 +00:00
|
|
|
return gst_date_time_new (0.0, year, -1, -1, -1, -1, -1);
|
2012-06-12 20:35:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_date_time_new_ym:
|
|
|
|
* @year: the gregorian year
|
|
|
|
* @month: the gregorian month
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* If value is -1 then all over value will be ignored. For example
|
|
|
|
* if @month == -1, then #GstDateTime will created only for @year.
|
|
|
|
*
|
|
|
|
* Free-function: gst_date_time_unref
|
|
|
|
*
|
|
|
|
* Return value: (transfer full): the newly created #GstDateTime
|
|
|
|
*/
|
|
|
|
GstDateTime *
|
|
|
|
gst_date_time_new_ym (gint year, gint month)
|
|
|
|
{
|
2012-06-12 22:52:02 +00:00
|
|
|
return gst_date_time_new (0.0, year, month, -1, -1, -1, -1);
|
2012-06-12 20:35:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_date_time_new_ymd:
|
|
|
|
* @year: the gregorian year
|
|
|
|
* @month: the gregorian month
|
|
|
|
* @day: the day of the gregorian month
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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): the newly created #GstDateTime
|
|
|
|
*/
|
|
|
|
GstDateTime *
|
|
|
|
gst_date_time_new_ymd (gint year, gint month, gint day)
|
|
|
|
{
|
2012-06-12 22:52:02 +00:00
|
|
|
return gst_date_time_new (0.0, year, month, day, -1, -1, -1);
|
2012-06-12 20:35:42 +00:00
|
|
|
}
|
|
|
|
|
2010-10-11 19:15:29 +00:00
|
|
|
/**
|
2010-11-17 12:39:34 +00:00
|
|
|
* gst_date_time_new_from_unix_epoch_local_time:
|
2010-10-11 19:15:29 +00:00
|
|
|
* @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.
|
|
|
|
*
|
2010-12-07 18:35:04 +00:00
|
|
|
* Free-function: gst_date_time_unref
|
|
|
|
*
|
|
|
|
* Return value: (transfer full): the newly created #GstDateTime
|
2010-10-11 19:15:29 +00:00
|
|
|
*/
|
2012-06-09 17:05:14 +00:00
|
|
|
GstDateTime *
|
|
|
|
gst_date_time_new_from_unix_epoch_local_time (gint64 secs)
|
|
|
|
{
|
2012-06-30 01:52:47 +00:00
|
|
|
GDateTime *datetime;
|
|
|
|
|
|
|
|
datetime = g_date_time_new_from_unix_local (secs);
|
|
|
|
return gst_date_time_new_from_g_date_time (datetime);
|
2012-06-09 17:05:14 +00:00
|
|
|
}
|
2010-10-11 19:15:29 +00:00
|
|
|
|
2010-11-17 12:39:34 +00:00
|
|
|
/**
|
|
|
|
* gst_date_time_new_from_unix_epoch_utc:
|
|
|
|
* @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 UTC timezone.
|
|
|
|
*
|
2010-12-07 18:35:04 +00:00
|
|
|
* Free-function: gst_date_time_unref
|
|
|
|
*
|
|
|
|
* Return value: (transfer full): the newly created #GstDateTime
|
2010-11-17 12:39:34 +00:00
|
|
|
*/
|
2012-06-09 17:05:14 +00:00
|
|
|
GstDateTime *
|
|
|
|
gst_date_time_new_from_unix_epoch_utc (gint64 secs)
|
|
|
|
{
|
2012-06-12 20:35:42 +00:00
|
|
|
GstDateTime *datetime;
|
|
|
|
datetime =
|
2012-06-30 01:52:47 +00:00
|
|
|
gst_date_time_new_from_g_date_time (g_date_time_new_from_unix_utc (secs));
|
2012-06-12 20:35:42 +00:00
|
|
|
return datetime;
|
|
|
|
}
|
|
|
|
|
2012-06-12 22:52:02 +00:00
|
|
|
static GstDateTimeFields
|
2012-06-12 20:35:42 +00:00
|
|
|
gst_date_time_check_fields (gint * year, gint * month, gint * day,
|
|
|
|
gint * hour, gint * minute, gdouble * seconds)
|
|
|
|
{
|
|
|
|
if (*month == -1) {
|
2012-06-12 22:52:02 +00:00
|
|
|
*month = *day = 1;
|
|
|
|
*hour = *minute = *seconds = 0;
|
|
|
|
return GST_DATE_TIME_FIELDS_Y;
|
2012-06-12 20:35:42 +00:00
|
|
|
} else if (*day == -1) {
|
2012-06-12 22:52:02 +00:00
|
|
|
*day = 1;
|
|
|
|
*hour = *minute = *seconds = 0;
|
|
|
|
return GST_DATE_TIME_FIELDS_YM;
|
2012-06-12 20:35:42 +00:00
|
|
|
} else if (*hour == -1) {
|
2012-06-12 22:52:02 +00:00
|
|
|
*hour = *minute = *seconds = 0;
|
|
|
|
return GST_DATE_TIME_FIELDS_YMD;
|
2012-06-12 20:35:42 +00:00
|
|
|
} else if (*seconds == -1) {
|
2012-06-12 22:52:02 +00:00
|
|
|
*seconds = 0;
|
|
|
|
return GST_DATE_TIME_FIELDS_YMD_HM;
|
2012-06-12 20:35:42 +00:00
|
|
|
} else
|
2012-06-12 22:52:02 +00:00
|
|
|
return GST_DATE_TIME_FIELDS_YMD_HMS;
|
2012-06-09 17:05:14 +00:00
|
|
|
}
|
2010-11-17 12:39:34 +00:00
|
|
|
|
2010-10-11 19:15:29 +00:00
|
|
|
/**
|
|
|
|
* gst_date_time_new_local_time:
|
|
|
|
* @year: the gregorian year
|
2012-06-12 22:52:02 +00:00
|
|
|
* @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
|
2010-10-11 19:15:29 +00:00
|
|
|
*
|
|
|
|
* 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
|
2010-12-17 14:48:34 +00:00
|
|
|
* 1 to 31, @hour from 0 to 23, @minutes and @seconds from 0 to 59.
|
2010-10-11 19:15:29 +00:00
|
|
|
*
|
2012-06-12 22:52:02 +00:00
|
|
|
* 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.
|
2012-06-12 20:35:42 +00:00
|
|
|
*
|
2010-12-07 18:35:04 +00:00
|
|
|
* Free-function: gst_date_time_unref
|
|
|
|
*
|
2017-10-22 12:35:30 +00:00
|
|
|
* Return value: (transfer full) (nullable): the newly created #GstDateTime
|
2010-10-11 19:15:29 +00:00
|
|
|
*/
|
2012-06-09 17:05:14 +00:00
|
|
|
GstDateTime *
|
|
|
|
gst_date_time_new_local_time (gint year, gint month, gint day, gint hour,
|
|
|
|
gint minute, gdouble seconds)
|
|
|
|
{
|
2012-06-12 22:52:02 +00:00
|
|
|
GstDateTimeFields fields;
|
2012-06-12 20:35:42 +00:00
|
|
|
GstDateTime *datetime;
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
fields = gst_date_time_check_fields (&year, &month, &day,
|
|
|
|
&hour, &minute, &seconds);
|
|
|
|
|
2012-06-30 01:52:47 +00:00
|
|
|
datetime = gst_date_time_new_from_g_date_time (g_date_time_new_local (year,
|
2012-06-12 20:35:42 +00:00
|
|
|
month, day, hour, minute, seconds));
|
|
|
|
|
|
|
|
datetime->fields = fields;
|
|
|
|
return datetime;
|
2012-06-09 17:05:14 +00:00
|
|
|
}
|
2010-10-11 19:15:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_date_time_new_now_local_time:
|
|
|
|
*
|
|
|
|
* Creates a new #GstDateTime representing the current date and time.
|
|
|
|
*
|
2010-12-07 18:35:04 +00:00
|
|
|
* Free-function: gst_date_time_unref
|
|
|
|
*
|
|
|
|
* Return value: (transfer full): the newly created #GstDateTime which should
|
|
|
|
* be freed with gst_date_time_unref().
|
2010-10-11 19:15:29 +00:00
|
|
|
*/
|
2012-06-09 17:05:14 +00:00
|
|
|
GstDateTime *
|
|
|
|
gst_date_time_new_now_local_time (void)
|
|
|
|
{
|
2012-06-30 01:52:47 +00:00
|
|
|
return gst_date_time_new_from_g_date_time (g_date_time_new_now_local ());
|
2012-06-09 17:05:14 +00:00
|
|
|
}
|
2010-10-11 19:15:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_date_time_new_now_utc:
|
|
|
|
*
|
|
|
|
* Creates a new #GstDateTime that represents the current instant at Universal
|
|
|
|
* coordinated time.
|
|
|
|
*
|
2010-12-07 18:35:04 +00:00
|
|
|
* Free-function: gst_date_time_unref
|
|
|
|
*
|
|
|
|
* Return value: (transfer full): the newly created #GstDateTime which should
|
|
|
|
* be freed with gst_date_time_unref().
|
2010-10-11 19:15:29 +00:00
|
|
|
*/
|
2010-09-27 22:35:08 +00:00
|
|
|
GstDateTime *
|
|
|
|
gst_date_time_new_now_utc (void)
|
|
|
|
{
|
2012-06-30 01:52:47 +00:00
|
|
|
return gst_date_time_new_from_g_date_time (g_date_time_new_now_utc ());
|
2010-09-27 22:35:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gint
|
2012-06-27 12:16:07 +00:00
|
|
|
__gst_date_time_compare (const GstDateTime * dt1, const GstDateTime * dt2)
|
2010-09-27 22:35:08 +00:00
|
|
|
{
|
2012-06-27 12:16:07 +00:00
|
|
|
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;
|
2010-09-27 22:35:08 +00:00
|
|
|
}
|
|
|
|
|
2012-06-09 17:05:14 +00:00
|
|
|
/**
|
|
|
|
* 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
|
2013-12-07 14:38:19 +00:00
|
|
|
* representing it in hours for most timezones.
|
2012-06-09 17:05:14 +00:00
|
|
|
*
|
2012-06-12 20:35:42 +00:00
|
|
|
* 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.
|
|
|
|
*
|
2012-06-09 17:05:14 +00:00
|
|
|
* Free-function: gst_date_time_unref
|
|
|
|
*
|
2017-10-22 12:35:30 +00:00
|
|
|
* Return value: (transfer full) (nullable): the newly created #GstDateTime
|
2012-06-09 17:05:14 +00:00
|
|
|
*/
|
2010-09-27 22:35:08 +00:00
|
|
|
GstDateTime *
|
2010-10-12 18:13:48 +00:00
|
|
|
gst_date_time_new (gfloat tzoffset, gint year, gint month, gint day, gint hour,
|
|
|
|
gint minute, gdouble seconds)
|
2010-09-27 22:35:08 +00:00
|
|
|
{
|
2012-06-12 22:52:02 +00:00
|
|
|
GstDateTimeFields fields;
|
2010-09-27 22:35:08 +00:00
|
|
|
gchar buf[6];
|
|
|
|
GTimeZone *tz;
|
|
|
|
GDateTime *dt;
|
2012-06-12 20:35:42 +00:00
|
|
|
GstDateTime *datetime;
|
2010-09-27 22:35:08 +00:00
|
|
|
gint tzhour, tzminute;
|
2012-06-12 20:35:42 +00:00
|
|
|
|
|
|
|
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);
|
2012-06-12 22:52:02 +00:00
|
|
|
g_return_val_if_fail ((hour >= 0 && minute >= 0) ||
|
|
|
|
(hour == -1 && minute == -1 && seconds == -1 && tzoffset == 0.0), NULL);
|
2010-09-27 22:35:08 +00:00
|
|
|
|
|
|
|
tzhour = (gint) ABS (tzoffset);
|
|
|
|
tzminute = (gint) ((ABS (tzoffset) - tzhour) * 60);
|
|
|
|
|
|
|
|
g_snprintf (buf, 6, "%c%02d%02d", tzoffset >= 0 ? '+' : '-', tzhour,
|
|
|
|
tzminute);
|
|
|
|
|
|
|
|
tz = g_time_zone_new (buf);
|
2010-12-02 19:28:43 +00:00
|
|
|
|
2012-06-12 20:35:42 +00:00
|
|
|
fields = gst_date_time_check_fields (&year, &month, &day,
|
|
|
|
&hour, &minute, &seconds);
|
|
|
|
|
2010-10-12 18:13:48 +00:00
|
|
|
dt = g_date_time_new (tz, year, month, day, hour, minute, seconds);
|
2010-09-27 22:35:08 +00:00
|
|
|
g_time_zone_unref (tz);
|
2012-06-12 20:35:42 +00:00
|
|
|
|
2012-06-30 01:52:47 +00:00
|
|
|
datetime = gst_date_time_new_from_g_date_time (dt);
|
2012-06-12 20:35:42 +00:00
|
|
|
datetime->fields = fields;
|
|
|
|
|
|
|
|
return datetime;
|
2010-09-27 22:35:08 +00:00
|
|
|
}
|
|
|
|
|
2012-07-07 21:40:12 +00:00
|
|
|
gchar *
|
|
|
|
__gst_date_time_serialize (GstDateTime * datetime, gboolean serialize_usecs)
|
|
|
|
{
|
|
|
|
GString *s;
|
|
|
|
gfloat gmt_offset;
|
|
|
|
guint msecs;
|
|
|
|
|
|
|
|
/* we always have at least the year */
|
|
|
|
s = g_string_new (NULL);
|
|
|
|
g_string_append_printf (s, "%04u", gst_date_time_get_year (datetime));
|
|
|
|
|
|
|
|
if (datetime->fields == GST_DATE_TIME_FIELDS_Y)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
/* add month */
|
|
|
|
g_string_append_printf (s, "-%02u", gst_date_time_get_month (datetime));
|
|
|
|
|
|
|
|
if (datetime->fields == GST_DATE_TIME_FIELDS_YM)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
/* add day of month */
|
|
|
|
g_string_append_printf (s, "-%02u", gst_date_time_get_day (datetime));
|
|
|
|
|
|
|
|
if (datetime->fields == GST_DATE_TIME_FIELDS_YMD)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
/* add time */
|
|
|
|
g_string_append_printf (s, "T%02u:%02u", gst_date_time_get_hour (datetime),
|
|
|
|
gst_date_time_get_minute (datetime));
|
|
|
|
|
|
|
|
if (datetime->fields == GST_DATE_TIME_FIELDS_YMD_HM)
|
|
|
|
goto add_timezone;
|
|
|
|
|
|
|
|
/* add seconds */
|
|
|
|
g_string_append_printf (s, ":%02u", gst_date_time_get_second (datetime));
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* add timezone */
|
|
|
|
|
|
|
|
add_timezone:
|
|
|
|
|
|
|
|
gmt_offset = gst_date_time_get_time_zone_offset (datetime);
|
|
|
|
if (gmt_offset == 0) {
|
|
|
|
g_string_append_c (s, 'Z');
|
|
|
|
} else {
|
|
|
|
guint tzhour, tzminute;
|
|
|
|
|
|
|
|
tzhour = (guint) ABS (gmt_offset);
|
|
|
|
tzminute = (guint) ((ABS (gmt_offset) - tzhour) * 60);
|
|
|
|
|
|
|
|
g_string_append_c (s, (gmt_offset >= 0) ? '+' : '-');
|
|
|
|
g_string_append_printf (s, "%02u%02u", tzhour, tzminute);
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
|
|
|
|
return g_string_free (s, FALSE);
|
|
|
|
}
|
2012-06-18 06:06:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*
|
2014-06-11 22:21:34 +00:00
|
|
|
* 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().
|
2012-06-18 06:06:49 +00:00
|
|
|
*/
|
|
|
|
gchar *
|
|
|
|
gst_date_time_to_iso8601_string (GstDateTime * datetime)
|
|
|
|
{
|
2012-07-07 21:40:12 +00:00
|
|
|
g_return_val_if_fail (datetime != NULL, NULL);
|
2012-06-18 06:06:49 +00:00
|
|
|
|
2012-07-07 21:40:12 +00:00
|
|
|
if (datetime->fields == GST_DATE_TIME_FIELDS_INVALID)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return __gst_date_time_serialize (datetime, FALSE);
|
2012-06-18 06:06:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2015-08-10 12:31:37 +00:00
|
|
|
* #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.
|
2012-06-18 06:06:49 +00:00
|
|
|
*
|
|
|
|
* Free-function: gst_date_time_unref
|
|
|
|
*
|
2014-06-11 22:21:34 +00:00
|
|
|
* Returns: (transfer full) (nullable): a newly created #GstDateTime,
|
|
|
|
* or %NULL on error
|
2012-06-18 06:06:49 +00:00
|
|
|
*/
|
|
|
|
GstDateTime *
|
|
|
|
gst_date_time_new_from_iso8601_string (const gchar * string)
|
|
|
|
{
|
2012-07-07 18:43:50 +00:00
|
|
|
gint year = -1, month = -1, day = -1, hour = -1, minute = -1;
|
2015-08-10 12:31:37 +00:00
|
|
|
gint gmt_offset_hour = -99, gmt_offset_min = -99;
|
2012-07-07 18:43:50 +00:00
|
|
|
gdouble second = -1.0;
|
2012-06-18 06:06:49 +00:00
|
|
|
gfloat tzoffset = 0.0;
|
2012-07-07 18:43:50 +00:00
|
|
|
guint64 usecs;
|
2012-06-18 06:06:49 +00:00
|
|
|
gint len, ret;
|
|
|
|
|
2012-07-15 23:24:46 +00:00
|
|
|
g_return_val_if_fail (string != NULL, NULL);
|
|
|
|
|
|
|
|
GST_DEBUG ("Parsing '%s' into a datetime", string);
|
2012-06-18 06:06:49 +00:00
|
|
|
|
2012-07-15 23:24:46 +00:00
|
|
|
len = strlen (string);
|
2012-06-18 06:06:49 +00:00
|
|
|
|
2015-08-10 12:31:37 +00:00
|
|
|
/* 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]))
|
2012-07-15 23:24:46 +00:00
|
|
|
return NULL;
|
2012-06-18 06:06:49 +00:00
|
|
|
|
2015-08-10 12:31:37 +00:00
|
|
|
if (g_ascii_isdigit (string[2]) && g_ascii_isdigit (string[3])) {
|
|
|
|
ret = sscanf (string, "%04d-%02d-%02d", &year, &month, &day);
|
2012-06-18 06:06:49 +00:00
|
|
|
|
2015-08-10 12:31:37 +00:00
|
|
|
if (ret == 0)
|
|
|
|
return NULL;
|
2012-06-27 22:00:08 +00:00
|
|
|
|
2015-08-10 12:31:37 +00:00
|
|
|
if (ret == 3 && day <= 0) {
|
|
|
|
ret = 2;
|
|
|
|
day = -1;
|
|
|
|
}
|
2012-06-27 22:00:08 +00:00
|
|
|
|
2015-08-10 12:31:37 +00:00
|
|
|
if (ret >= 2 && month <= 0) {
|
|
|
|
ret = 1;
|
|
|
|
month = day = -1;
|
|
|
|
}
|
2012-06-27 22:00:08 +00:00
|
|
|
|
2016-04-21 10:49:32 +00:00
|
|
|
if (ret >= 1 && (year <= 0 || year > 9999 || month > 12 || day > 31))
|
2015-08-10 12:31:37 +00:00
|
|
|
return NULL;
|
2012-06-27 22:00:08 +00:00
|
|
|
|
2015-08-10 12:31:37 +00:00
|
|
|
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;
|
2012-06-18 06:06:49 +00:00
|
|
|
|
2015-08-10 12:31:37 +00:00
|
|
|
string += 10;
|
|
|
|
/* Exit if there is no expeceted value on this stage */
|
|
|
|
if (!(*string == 'T' || *string == '-' || *string == ' '))
|
|
|
|
goto ymd;
|
2012-06-18 06:06:49 +00:00
|
|
|
|
2015-08-10 12:31:37 +00:00
|
|
|
string += 1;
|
|
|
|
}
|
|
|
|
/* if hour or minute fails, then we will use only ymd. */
|
|
|
|
hour = g_ascii_strtoull (string, (gchar **) & string, 10);
|
2012-06-18 06:06:49 +00:00
|
|
|
if (hour > 24 || *string != ':')
|
|
|
|
goto ymd;
|
|
|
|
|
2012-07-07 15:01:41 +00:00
|
|
|
/* minute */
|
2012-06-18 06:06:49 +00:00
|
|
|
minute = g_ascii_strtoull (string + 1, (gchar **) & string, 10);
|
|
|
|
if (minute > 59)
|
|
|
|
goto ymd;
|
|
|
|
|
2012-07-07 15:01:41 +00:00
|
|
|
/* second */
|
2012-06-18 06:06:49 +00:00
|
|
|
if (*string == ':') {
|
|
|
|
second = g_ascii_strtoull (string + 1, (gchar **) & string, 10);
|
|
|
|
/* if we fail here, we still can reuse hour and minute. We
|
2012-07-07 15:01:41 +00:00
|
|
|
* will still attempt to parse any timezone information */
|
|
|
|
if (second > 59) {
|
|
|
|
second = -1.0;
|
2012-07-07 18:43:50 +00:00
|
|
|
} 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);
|
|
|
|
}
|
|
|
|
}
|
2012-07-07 15:01:41 +00:00
|
|
|
}
|
2012-06-18 06:06:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (*string == 'Z')
|
|
|
|
goto ymd_hms;
|
|
|
|
else {
|
|
|
|
/* reuse some code from gst-plugins-base/gst-libs/gst/tag/gstxmptag.c */
|
2015-08-10 12:31:37 +00:00
|
|
|
gint gmt_offset = -1;
|
2012-06-18 06:06:49 +00:00
|
|
|
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;
|
|
|
|
|
2017-01-15 11:52:44 +00:00
|
|
|
if (pos && strlen (pos) >= 3) {
|
2012-06-18 06:06:49 +00:00
|
|
|
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) {
|
2015-08-10 12:31:37 +00:00
|
|
|
if (neg_pos != NULL && neg_pos + 1 == pos) {
|
|
|
|
gmt_offset_hour *= -1;
|
|
|
|
gmt_offset_min *= -1;
|
|
|
|
}
|
2012-06-18 06:06:49 +00:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ymd_hms:
|
2015-08-10 12:31:37 +00:00
|
|
|
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);
|
|
|
|
}
|
2012-06-18 06:06:49 +00:00
|
|
|
return gst_date_time_new (tzoffset, year, month, day, hour, minute, second);
|
|
|
|
ymd:
|
2015-08-10 12:31:37 +00:00
|
|
|
if (year == -1) {
|
|
|
|
/* No date was supplied and time failed to parse */
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-06-18 06:06:49 +00:00
|
|
|
return gst_date_time_new_ymd (year, month, day);
|
|
|
|
}
|
|
|
|
|
2010-09-27 22:35:08 +00:00
|
|
|
static void
|
|
|
|
gst_date_time_free (GstDateTime * datetime)
|
|
|
|
{
|
|
|
|
g_date_time_unref (datetime->datetime);
|
2018-08-03 10:16:21 +00:00
|
|
|
|
|
|
|
#ifdef USE_POISONING
|
|
|
|
memset (datetime, 0xff, sizeof (GstDateTime));
|
|
|
|
#endif
|
|
|
|
|
2010-09-27 22:35:08 +00:00
|
|
|
g_slice_free (GstDateTime, datetime);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_date_time_ref:
|
|
|
|
* @datetime: a #GstDateTime
|
|
|
|
*
|
|
|
|
* Atomically increments the reference count of @datetime by one.
|
|
|
|
*
|
2010-12-07 18:35:04 +00:00
|
|
|
* Return value: (transfer full): the reference @datetime
|
2010-09-27 22:35:08 +00:00
|
|
|
*/
|
|
|
|
GstDateTime *
|
|
|
|
gst_date_time_ref (GstDateTime * datetime)
|
|
|
|
{
|
2014-06-03 22:38:28 +00:00
|
|
|
return (GstDateTime *) gst_mini_object_ref (GST_MINI_OBJECT_CAST (datetime));
|
2010-09-27 22:35:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_date_time_unref:
|
2010-12-07 18:35:04 +00:00
|
|
|
* @datetime: (transfer full): a #GstDateTime
|
2010-09-27 22:35:08 +00:00
|
|
|
*
|
|
|
|
* Atomically decrements the reference count of @datetime by one. When the
|
|
|
|
* reference count reaches zero, the structure is freed.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
gst_date_time_unref (GstDateTime * datetime)
|
|
|
|
{
|
2014-06-03 22:38:28 +00:00
|
|
|
gst_mini_object_unref (GST_MINI_OBJECT_CAST (datetime));
|
2010-09-27 22:35:08 +00:00
|
|
|
}
|
2014-06-19 06:05:03 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
_priv_gst_date_time_initialize (void)
|
|
|
|
{
|
|
|
|
_gst_date_time_type = gst_date_time_get_type ();
|
|
|
|
}
|