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
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2010-09-27 22:35:08 +00:00
|
|
|
#include "glib-compat-private.h"
|
2010-07-16 17:09:12 +00:00
|
|
|
#include "gst_private.h"
|
|
|
|
#include "gstdatetime.h"
|
2010-09-27 22:35:08 +00:00
|
|
|
#include <glib.h>
|
2010-10-12 18:13:48 +00:00
|
|
|
#include <math.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.
|
|
|
|
*
|
|
|
|
* Since: 0.10.31
|
|
|
|
*/
|
|
|
|
|
2012-06-09 17:05:14 +00:00
|
|
|
#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))
|
|
|
|
|
|
|
|
struct _GstDateTime
|
|
|
|
{
|
|
|
|
GDateTime *datetime;
|
|
|
|
|
|
|
|
volatile gint ref_count;
|
|
|
|
};
|
|
|
|
|
|
|
|
static GstDateTime *
|
|
|
|
gst_date_time_new_from_gdatetime (GDateTime * dt)
|
|
|
|
{
|
|
|
|
GstDateTime *gst_dt;
|
|
|
|
|
|
|
|
if (!dt)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
gst_dt = g_slice_new (GstDateTime);
|
|
|
|
gst_dt->datetime = dt;
|
|
|
|
gst_dt->ref_count = 1;
|
|
|
|
return gst_dt;
|
|
|
|
}
|
|
|
|
|
2010-10-11 19:15:29 +00:00
|
|
|
/**
|
|
|
|
* gst_date_time_get_year:
|
|
|
|
* @datetime: a #GstDateTime
|
|
|
|
*
|
|
|
|
* Returns the year of this #GstDateTime
|
|
|
|
*
|
|
|
|
* Return value: The year of this #GstDateTime
|
|
|
|
* Since: 0.10.31
|
|
|
|
*/
|
2012-06-09 17:05:14 +00:00
|
|
|
gint
|
|
|
|
gst_date_time_get_year (const GstDateTime * datetime)
|
|
|
|
{
|
|
|
|
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..
|
|
|
|
*
|
|
|
|
* Return value: The month of this #GstDateTime
|
|
|
|
* Since: 0.10.31
|
|
|
|
*/
|
2012-06-09 17:05:14 +00:00
|
|
|
gint
|
|
|
|
gst_date_time_get_month (const GstDateTime * datetime)
|
|
|
|
{
|
|
|
|
return g_date_time_get_month (datetime->datetime);
|
|
|
|
}
|
2010-10-11 19:15:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_date_time_get_day:
|
|
|
|
* @datetime: a #GstDateTime
|
|
|
|
*
|
|
|
|
* Returns the day of this #GstDateTime.
|
|
|
|
*
|
|
|
|
* Return value: The day of this #GstDateTime
|
|
|
|
* Since: 0.10.31
|
|
|
|
*/
|
2012-06-09 17:05:14 +00:00
|
|
|
gint
|
|
|
|
gst_date_time_get_day (const GstDateTime * datetime)
|
|
|
|
{
|
|
|
|
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.
|
|
|
|
*
|
|
|
|
* Return value: the hour of the day
|
|
|
|
*
|
|
|
|
* Since: 0.10.31
|
|
|
|
*/
|
2012-06-09 17:05:14 +00:00
|
|
|
gint
|
|
|
|
gst_date_time_get_hour (const GstDateTime * datetime)
|
|
|
|
{
|
|
|
|
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.
|
|
|
|
*
|
|
|
|
* Return value: the minute of the hour
|
|
|
|
*
|
|
|
|
* Since: 0.10.31
|
|
|
|
*/
|
2012-06-09 17:05:14 +00:00
|
|
|
gint
|
|
|
|
gst_date_time_get_minute (const GstDateTime * datetime)
|
|
|
|
{
|
|
|
|
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.
|
|
|
|
*
|
|
|
|
* Return value: the second represented by @datetime
|
|
|
|
*
|
|
|
|
* Since: 0.10.31
|
|
|
|
*/
|
2012-06-09 17:05:14 +00:00
|
|
|
gint
|
|
|
|
gst_date_time_get_second (const GstDateTime * datetime)
|
|
|
|
{
|
|
|
|
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
|
|
|
*
|
|
|
|
* Since: 0.10.31
|
|
|
|
*/
|
2012-06-09 17:05:14 +00:00
|
|
|
gint
|
|
|
|
gst_date_time_get_microsecond (const GstDateTime * datetime)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
* Since: 0.10.31
|
|
|
|
*/
|
2012-06-09 17:05:14 +00:00
|
|
|
gfloat
|
|
|
|
gst_date_time_get_time_zone_offset (const GstDateTime * datetime)
|
|
|
|
{
|
|
|
|
return (g_date_time_get_utc_offset (datetime->datetime) /
|
|
|
|
G_USEC_PER_SEC) / 3600.0;
|
|
|
|
}
|
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
|
|
|
*
|
|
|
|
* Since: 0.10.31
|
|
|
|
*/
|
2012-06-09 17:05:14 +00:00
|
|
|
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));
|
|
|
|
}
|
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
|
|
|
*
|
|
|
|
* Since: 0.10.31
|
|
|
|
*/
|
2012-06-09 17:05:14 +00:00
|
|
|
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));
|
|
|
|
}
|
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
|
|
|
|
* @month: the gregorian month
|
|
|
|
* @day: the day of the gregorian month
|
|
|
|
* @hour: the hour of the day
|
|
|
|
* @minute: the minute of the hour
|
2010-12-17 14:48:34 +00:00
|
|
|
* @seconds: the second of the minute
|
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
|
|
|
*
|
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
|
|
|
*
|
|
|
|
* Since: 0.10.31
|
|
|
|
*/
|
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)
|
|
|
|
{
|
|
|
|
return gst_date_time_new_from_gdatetime (g_date_time_new_local (year, month,
|
|
|
|
day, hour, minute, seconds));
|
|
|
|
}
|
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
|
|
|
*
|
|
|
|
* Since: 0.10.31
|
|
|
|
*/
|
2012-06-09 17:05:14 +00:00
|
|
|
GstDateTime *
|
|
|
|
gst_date_time_new_now_local_time (void)
|
|
|
|
{
|
|
|
|
return gst_date_time_new_from_gdatetime (g_date_time_new_now_local ());
|
|
|
|
}
|
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
|
|
|
*
|
|
|
|
* Since: 0.10.31
|
|
|
|
*/
|
2010-09-27 22:35:08 +00:00
|
|
|
GstDateTime *
|
|
|
|
gst_date_time_new_now_utc (void)
|
|
|
|
{
|
|
|
|
return gst_date_time_new_from_gdatetime (g_date_time_new_now_utc ());
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
* represeting it in hours for most timezones.
|
|
|
|
*
|
|
|
|
* Free-function: gst_date_time_unref
|
|
|
|
*
|
|
|
|
* Return value: (transfer full): the newly created #GstDateTime
|
|
|
|
*
|
|
|
|
* Since: 0.10.31
|
|
|
|
*/
|
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
|
|
|
{
|
|
|
|
gchar buf[6];
|
|
|
|
GTimeZone *tz;
|
|
|
|
GDateTime *dt;
|
|
|
|
gint tzhour, tzminute;
|
|
|
|
|
|
|
|
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
|
|
|
|
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);
|
|
|
|
return gst_date_time_new_from_gdatetime (dt);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_date_time_free (GstDateTime * datetime)
|
|
|
|
{
|
|
|
|
g_date_time_unref (datetime->datetime);
|
|
|
|
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
|
|
|
*
|
|
|
|
* Since: 0.10.31
|
|
|
|
*/
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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);
|
|
|
|
|
|
|
|
if (g_atomic_int_dec_and_test (&datetime->ref_count))
|
|
|
|
gst_date_time_free (datetime);
|
|
|
|
}
|