mirror of
https://gitlab.freedesktop.org/gstreamer/gstreamer.git
synced 2025-03-30 12:49:40 +00:00
datetime: Use GDateTime if available
Use GDateTime internally on GstDateTime if glib already provides it. https://bugzilla.gnome.org/show_bug.cgi?id=628408
This commit is contained in:
parent
6d883ed95c
commit
c7e5bc1e5d
3 changed files with 185 additions and 40 deletions
|
@ -8,7 +8,6 @@
|
||||||
#ifndef __GLIB_COMPAT_PRIVATE_H__
|
#ifndef __GLIB_COMPAT_PRIVATE_H__
|
||||||
#define __GLIB_COMPAT_PRIVATE_H__
|
#define __GLIB_COMPAT_PRIVATE_H__
|
||||||
|
|
||||||
#include "gst_private.h" /* for g_warning */
|
|
||||||
#include <glib.h>
|
#include <glib.h>
|
||||||
|
|
||||||
G_BEGIN_DECLS
|
G_BEGIN_DECLS
|
||||||
|
@ -17,6 +16,10 @@ G_BEGIN_DECLS
|
||||||
typedef struct stat GStatBuf;
|
typedef struct stat GStatBuf;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if GLIB_CHECK_VERSION(2,26,0)
|
||||||
|
#define GLIB_HAS_GDATETIME
|
||||||
|
#endif
|
||||||
|
|
||||||
/* copies */
|
/* copies */
|
||||||
|
|
||||||
/* adaptations */
|
/* adaptations */
|
||||||
|
|
|
@ -21,8 +21,12 @@
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include "glib-compat-private.h"
|
||||||
#include "gst_private.h"
|
#include "gst_private.h"
|
||||||
#include "gstdatetime.h"
|
#include "gstdatetime.h"
|
||||||
|
#include <glib.h>
|
||||||
|
|
||||||
|
#ifndef GLIB_HAS_GDATETIME
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* SECTION:gstdatetime
|
* SECTION:gstdatetime
|
||||||
|
@ -437,44 +441,6 @@ gst_date_time_new_now_local_time (void)
|
||||||
return datetime;
|
return 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
|
|
||||||
*/
|
|
||||||
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:
|
|
||||||
* @datetime: 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);
|
|
||||||
|
|
||||||
if (g_atomic_int_dec_and_test (&datetime->ref_count))
|
|
||||||
gst_date_time_free (datetime);
|
|
||||||
}
|
|
||||||
|
|
||||||
static GstDateTime *
|
static GstDateTime *
|
||||||
gst_date_time_copy (const GstDateTime * dt)
|
gst_date_time_copy (const GstDateTime * dt)
|
||||||
{
|
{
|
||||||
|
@ -589,3 +555,179 @@ done:
|
||||||
gst_date_time_unref (b);
|
gst_date_time_unref (b);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
gint
|
||||||
|
gst_date_time_get_year (const GstDateTime * datetime)
|
||||||
|
{
|
||||||
|
return g_date_time_get_year (datetime->datetime);
|
||||||
|
}
|
||||||
|
|
||||||
|
gint
|
||||||
|
gst_date_time_get_month (const GstDateTime * datetime)
|
||||||
|
{
|
||||||
|
return g_date_time_get_month (datetime->datetime);
|
||||||
|
}
|
||||||
|
|
||||||
|
gint
|
||||||
|
gst_date_time_get_day (const GstDateTime * datetime)
|
||||||
|
{
|
||||||
|
return g_date_time_get_day_of_month (datetime->datetime);
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
gint
|
||||||
|
gst_date_time_get_second (const GstDateTime * datetime)
|
||||||
|
{
|
||||||
|
return g_date_time_get_second (datetime->datetime);
|
||||||
|
}
|
||||||
|
|
||||||
|
gint
|
||||||
|
gst_date_time_get_microsecond (const GstDateTime * datetime)
|
||||||
|
{
|
||||||
|
return g_date_time_get_microsecond (datetime->datetime);
|
||||||
|
}
|
||||||
|
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
|
GstDateTime *
|
||||||
|
gst_date_time_new_from_unix_epoch (gint64 secs)
|
||||||
|
{
|
||||||
|
return
|
||||||
|
gst_date_time_new_from_gdatetime (g_date_time_new_from_unix_local (secs));
|
||||||
|
}
|
||||||
|
|
||||||
|
GstDateTime *
|
||||||
|
gst_date_time_new_local_time (gint year, gint month, gint day, gint hour,
|
||||||
|
gint minute, gint second, gint microsecond)
|
||||||
|
{
|
||||||
|
return gst_date_time_new_from_gdatetime (g_date_time_new_local (year, month,
|
||||||
|
day, hour, minute, second + (microsecond / 1000000.0)));
|
||||||
|
}
|
||||||
|
|
||||||
|
GstDateTime *
|
||||||
|
gst_date_time_new_now_local_time (void)
|
||||||
|
{
|
||||||
|
return gst_date_time_new_from_gdatetime (g_date_time_new_now_local ());
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
GstDateTime *
|
||||||
|
gst_date_time_new (gint year, gint month, gint day, gint hour, gint minute,
|
||||||
|
gint second, gint microsecond, gfloat tzoffset)
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
dt = g_date_time_new (tz, year, month, day, hour, minute,
|
||||||
|
second + (microsecond / 1000000.0));
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
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:
|
||||||
|
* @datetime: 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);
|
||||||
|
|
||||||
|
if (g_atomic_int_dec_and_test (&datetime->ref_count))
|
||||||
|
gst_date_time_free (datetime);
|
||||||
|
}
|
||||||
|
|
|
@ -28,7 +28,7 @@ G_BEGIN_DECLS
|
||||||
/**
|
/**
|
||||||
* GstDateTime:
|
* GstDateTime:
|
||||||
*
|
*
|
||||||
* Opaque, immutable, refcounted struct that store date, time and timezone
|
* Opaque, immutable, refcounted struct that stores date, time and timezone
|
||||||
* information. It currently supports ranges from 0001-01-01 to
|
* information. It currently supports ranges from 0001-01-01 to
|
||||||
* 9999-12-31 in the Gregorian proleptic calendar.
|
* 9999-12-31 in the Gregorian proleptic calendar.
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in a new issue