gstdatetime: Adds GstDateTime
authorThiago Santos <thiago.sousa.santos@collabora.co.uk>
Fri, 16 Jul 2010 17:09:12 +0000 (14:09 -0300)
committerThiago Santos <thiago.sousa.santos@collabora.co.uk>
Mon, 26 Jul 2010 14:57:49 +0000 (11:57 -0300)
Adds GstDateTime to represent dates + time + timezone
information.

Tests included.

API: GstDateTime
API: gst_date_time_get_day
API: gst_date_time_get_month
API: gst_date_time_get_year
API: gst_date_time_get_hour
API: gst_date_time_get_microsecond
API: gst_date_time_get_minute
API: gst_date_time_get_second
API: gst_date_time_get_time_zone_offset
API: gst_date_time_new
API: gst_date_time_new_local_time
API: gst_date_time_new_from_unix_epoch
API: gst_date_time_new_now_local_time
API: gst_date_time_new_now_utc
API: gst_date_time_ref
API: gst_date_time_unref

Fixes #594504

configure.ac
docs/gst/gstreamer-sections.txt
gst/Makefile.am
gst/gst.h
gst/gstdatetime.c [new file with mode: 0644]
gst/gstdatetime.h [new file with mode: 0644]
tests/check/Makefile.am
tests/check/gst/.gitignore
tests/check/gst/gstdatetime.c [new file with mode: 0644]
win32/common/libgstreamer.def

index 85294d0..f85d350 100644 (file)
@@ -342,8 +342,25 @@ if test x$gst_cv_uint128_t = xyes; then
   AC_DEFINE(HAVE_UINT128_T, 1, [Have __uint128_t type])
 fi
 
+dnl *** checking for tm_gmtoff ***
+AC_MSG_CHECKING([for tm_gmtoff])
+AC_RUN_IFELSE([AC_LANG_SOURCE([[
+       #include <time.h>
+       int main(void) {
+         struct tm t;
+         t.tm_gmtoff = 0;
+         exit(0);
+       }]])],
+  [have_tm_gmtoff=yes
+   AC_DEFINE(HAVE_TM_GMTOFF,1,[Have tm_gmtoff field in struct tm])],
+  [have_tm_gmtoff=no],
+  [have_tm_gmtoff="no (cross compiling)"])
+AC_MSG_RESULT($have_tm_gmtoff)
+
+
 dnl *** checks for library functions ***
 
+AC_CHECK_FUNCS([gmtime_r])
 AC_CHECK_FUNCS([sigaction])
 
 dnl check for fseeko()
index b5cbad2..8c78664 100644 (file)
@@ -2585,6 +2585,27 @@ gst_search_mode_get_type
 </SECTION>
 
 <SECTION>
+<FILE>gstdatetime</FILE>
+<TITLE>GstDateTime</TITLE>
+GstDateTime
+gst_date_time_get_day
+gst_date_time_get_month
+gst_date_time_get_hour
+gst_date_time_get_microsecond
+gst_date_time_get_minute
+gst_date_time_get_time_zone_offset
+gst_date_time_get_second
+gst_date_time_get_year
+gst_date_time_new
+gst_date_time_new_from_unix_epoch
+gst_date_time_new_local_time
+gst_date_time_new_now_local_time
+gst_date_time_new_now_utc
+gst_date_time_ref
+gst_date_time_unref
+</SECTION>
+
+<SECTION>
 <FILE>gstvalue</FILE>
 <TITLE>GstValue</TITLE>
 
index 7ea62e4..b85dd75 100644 (file)
@@ -61,6 +61,7 @@ libgstreamer_@GST_MAJORMINOR@_la_SOURCES = \
        gstcaps.c               \
        gstchildproxy.c         \
        gstclock.c              \
+       gstdatetime.c           \
        gstdebugutils.c         \
        gstelement.c            \
        gstelementfactory.c     \
@@ -152,6 +153,7 @@ gst_headers =                       \
        gstchildproxy.h         \
        gstclock.h              \
        gstcompat.h             \
+       gstdatetime.h           \
        gstdebugutils.h         \
        gstelement.h            \
        gstelementfactory.h     \
index 4a92948..bbf2a27 100644 (file)
--- a/gst/gst.h
+++ b/gst/gst.h
@@ -37,6 +37,7 @@
 #include <gst/gstcaps.h>
 #include <gst/gstchildproxy.h>
 #include <gst/gstclock.h>
+#include <gst/gstdatetime.h>
 #include <gst/gstdebugutils.h>
 #include <gst/gstelement.h>
 #include <gst/gsterror.h>
diff --git a/gst/gstdatetime.c b/gst/gstdatetime.c
new file mode 100644 (file)
index 0000000..bd015bc
--- /dev/null
@@ -0,0 +1,559 @@
+/* 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
+
+#include "gst_private.h"
+#include "gstdatetime.h"
+
+/**
+ * SECTION:gst-date-time
+ * @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
+ */
+
+#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))
+
+#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
+{
+  /*
+   * As we don't have a 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;
+};
+
+/*
+ * 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 void
+gst_date_time_set_local_timezone (GstDateTime * dt)
+{
+  struct tm tt;
+  time_t t;
+
+  g_return_if_fail (dt != NULL);
+
+  memset (&tt, 0, sizeof (tt));
+
+  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);
+
+  t = mktime (&tt);
+
+  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;
+}
+
+/**
+ * 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
+ */
+gint
+gst_date_time_get_year (const GstDateTime * datetime)
+{
+  g_return_val_if_fail (datetime != NULL, 0);
+
+  return datetime->year;
+}
+
+/**
+ * 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
+ */
+gint
+gst_date_time_get_month (const GstDateTime * datetime)
+{
+  g_return_val_if_fail (datetime != NULL, 0);
+
+  return datetime->month;
+}
+
+/**
+ * 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
+ */
+gint
+gst_date_time_get_day (const GstDateTime * datetime)
+{
+  g_return_val_if_fail (datetime != NULL, 0);
+
+  return datetime->day;
+}
+
+/**
+ * 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
+ */
+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);
+}
+
+/**
+ * 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
+ */
+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);
+}
+
+/**
+ * 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
+ */
+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;
+}
+
+/**
+ * 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
+ */
+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;
+}
+
+/**
+ * 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
+ */
+gfloat
+gst_date_time_get_time_zone_offset (const GstDateTime * datetime)
+{
+  g_return_val_if_fail (datetime != NULL, 0);
+
+  return datetime->tzoffset / 60.0f;
+}
+
+/**
+ * gst_date_time_new_from_unix_epoch:
+ * @t: seconds from the Unix epoch
+ *
+ * Creates a new #GstDateTime using the time since Jan 1, 1970 specified by @t.
+ * The #GstDateTime is in the local timezone.
+ *
+ * Return value: the newly created #GstDateTime
+ *
+ * Since: 0.10.31
+ */
+GstDateTime *
+gst_date_time_new_from_unix_epoch (gint64 t)
+{
+  GstDateTime *dt;
+  struct tm tm;
+  time_t tt;
+
+  memset (&tm, 0, sizeof (tm));
+  tt = (time_t) t;
+#ifdef HAVE_LOCALTIME_R
+  localtime_r (&tt, &tm);
+#else
+  localtime (&tt, &tm);
+#endif
+
+  dt = gst_date_time_new (tm.tm_year + 1900,
+      tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, 0, 0);
+  gst_date_time_set_local_timezone (dt);
+  return dt;
+}
+
+/**
+ * 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
+ * @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.
+ *
+ * Return value: the newly created #GstDateTime
+ *
+ * Since: 0.10.31
+ */
+GstDateTime *
+gst_date_time_new_local_time (gint year, gint month, gint day, gint hour,
+    gint minute, gint second, gint microsecond)
+{
+  GstDateTime *dt;
+
+  dt = gst_date_time_new (year, month, day, hour, minute, second, microsecond,
+      0);
+
+  gst_date_time_set_local_timezone (dt);
+
+  return dt;
+}
+
+/**
+ * gst_date_time_new:
+ * @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
+ * @tzoffset: Offset from UTC in hours.
+ *
+ * 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 and
+ * @microsecond from 0 to 999999.
+ *
+ * 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.
+ *
+ * Return value: the newly created #GstDateTime
+ *
+ * Since: 0.10.31
+ */
+GstDateTime *
+gst_date_time_new (gint year, gint month, gint day, gint hour,
+    gint minute, gint second, gint microsecond, gfloat tzoffset)
+{
+  GstDateTime *dt;
+
+  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 (second >= 0 && second < 60, NULL);
+  g_return_val_if_fail (microsecond >= 0 && microsecond < 1000000, NULL);
+
+  if (!(dt = gst_date_time_new_from_date (year, month, day)))
+    return NULL;
+
+  dt->usec = (hour * GST_DATE_TIME_USEC_PER_HOUR)
+      + (minute * GST_DATE_TIME_USEC_PER_MINUTE)
+      + (second * GST_DATE_TIME_USEC_PER_SECOND)
+      + microsecond;
+  dt->tzoffset = (gint) (60 * tzoffset);
+
+  return dt;
+}
+
+/**
+ * gst_date_time_new_now_local_time:
+ *
+ * Creates a new #GstDateTime representing the current date and time.
+ *
+ * Return value: the newly created #GstDateTime which should be freed with
+ *   gst_date_time_unref().
+ *
+ * Since: 0.10.31
+ */
+GstDateTime *
+gst_date_time_new_now_local_time (void)
+{
+  GstDateTime *datetime;
+  GTimeVal tv;
+  g_get_current_time (&tv);
+
+  datetime = gst_date_time_new_from_unix_epoch (tv.tv_sec);
+  datetime->usec += tv.tv_usec;
+  gst_date_time_set_local_timezone (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 *
+gst_date_time_copy (const GstDateTime * dt)
+{
+  GstDateTime *copy = gst_date_time_alloc ();
+
+  memcpy (copy, dt, sizeof (GstDateTime));
+  copy->ref_count = 1;
+
+  return copy;
+}
+
+static GstDateTime *
+gst_date_time_to_utc (const GstDateTime * dt)
+{
+  GstDateTime *utc;
+  gint64 usec;
+  gint days;
+  gint leap;
+
+  g_return_val_if_fail (dt != NULL, NULL);
+
+  utc = gst_date_time_copy (dt);
+
+  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;
+
+  leap = GREGORIAN_LEAP (utc->year) ? 1 : 0;
+
+  /* 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 (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;
+
+  return utc;
+}
+
+/**
+ * gst_date_time_new_now_utc:
+ *
+ * Creates a new #GstDateTime that represents the current instant at Universal
+ * coordinated time.
+ *
+ * Return value: the newly created #GstDateTime which should be freed with
+ *   gst_date_time_unref().
+ *
+ * Since: 0.10.31
+ */
+GstDateTime *
+gst_date_time_new_now_utc (void)
+{
+  GstDateTime *now, *utc;
+
+  now = gst_date_time_new_now_local_time ();
+  utc = gst_date_time_to_utc (now);
+  gst_date_time_unref (now);
+  return utc;
+}
diff --git a/gst/gstdatetime.h b/gst/gstdatetime.h
new file mode 100644 (file)
index 0000000..26b65d4
--- /dev/null
@@ -0,0 +1,67 @@
+/* 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.
+ */
+
+#ifndef __GST_DATE_TIME_H__
+#define __GST_DATE_TIME_H__
+
+#include <time.h>
+#include <glib.h>
+
+G_BEGIN_DECLS
+
+/**
+ * GstDateTime:
+ *
+ * Opaque, immutable, refcounted struct that store date, time and timezone
+ * information. It currently supports ranges from 0001-01-01 to
+ * 9999-12-31 in the Gregorian proleptic calendar.
+ *
+ * Use the acessor functions to get the stored values.
+ */
+typedef struct _GstDateTime GstDateTime;
+
+gint   gst_date_time_get_year          (const GstDateTime * datetime);
+gint   gst_date_time_get_month         (const GstDateTime * datetime);
+gint   gst_date_time_get_day           (const GstDateTime * datetime);
+gint   gst_date_time_get_hour          (const GstDateTime * datetime);
+gint   gst_date_time_get_minute        (const GstDateTime * datetime);
+gint   gst_date_time_get_second        (const GstDateTime * datetime);
+gint   gst_date_time_get_microsecond   (const GstDateTime * datetime);
+gfloat gst_date_time_get_time_zone_offset (const GstDateTime * datetime);
+
+GstDateTime    *gst_date_time_new_from_unix_epoch      (gint64 secs);
+GstDateTime    *gst_date_time_new_local_time           (gint year, gint month,
+                                                        gint day, gint hour,
+                                                        gint minute,
+                                                        gint second,
+                                                        gint microsecond);
+GstDateTime    *gst_date_time_new                      (gint year, gint month,
+                                                        gint day, gint hour,
+                                                        gint minute,
+                                                        gint second,
+                                                        gint microsecond,
+                                                        gfloat tzoffset);
+GstDateTime    *gst_date_time_new_now_local_time (void);
+GstDateTime    *gst_date_time_new_now_utc      (void);
+
+GstDateTime    *gst_date_time_ref              (GstDateTime * datetime);
+void           gst_date_time_unref             (GstDateTime * datetime);
+
+G_END_DECLS
+#endif /* __GST_DATE_TIME_H__ */
index 7fed996..544d361 100644 (file)
@@ -77,6 +77,7 @@ check_PROGRAMS =                              \
        gst/gstbufferlist                       \
        gst/gstbus                              \
        gst/gstcaps                             \
+       gst/gstdatetime                         \
        gst/gstinfo                             \
        gst/gstiterator                         \
        gst/gstmessage                          \
index fdfa6cb..f48cd2d 100644 (file)
@@ -9,6 +9,7 @@ gstcaps
 gstchildproxy
 gstclock
 gstdata
+gstdatetime
 gstelement
 gstevent
 gstghostpad
diff --git a/tests/check/gst/gstdatetime.c b/tests/check/gst/gstdatetime.c
new file mode 100644 (file)
index 0000000..59738a3
--- /dev/null
@@ -0,0 +1,244 @@
+/* GStreamer
+ * Copyright (C) 2010 Thiago Santos <thiago.sousa.santos@collabora.co.uk>
+ * Copyright (C) 2010 Christian Hergert <chris@dronelabs.com>
+ *
+ * gstdatetime.c: Unit tests for GstDateTime
+ *
+ * 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.
+ */
+
+#include <string.h>
+#include <time.h>
+#include <gst/check/gstcheck.h>
+
+#define ASSERT_TIME(dt,H,M,S) G_STMT_START { \
+  assert_equals_int ((H), gst_date_time_get_hour ((dt))); \
+  assert_equals_int ((M), gst_date_time_get_minute ((dt))); \
+  assert_equals_int ((S), gst_date_time_get_second ((dt))); \
+} G_STMT_END
+
+GST_START_TEST (test_GstDateTime_now)
+{
+  GstDateTime *dt;
+  time_t t;
+  struct tm tm;
+
+  memset (&tm, 0, sizeof (tm));
+  t = time (NULL);
+  localtime_r (&t, &tm);
+  dt = gst_date_time_new_now_local_time ();
+  assert_equals_int (gst_date_time_get_year (dt), 1900 + tm.tm_year);
+  assert_equals_int (gst_date_time_get_month (dt), 1 + tm.tm_mon);
+  assert_equals_int (gst_date_time_get_day (dt), tm.tm_mday);
+  assert_equals_int (gst_date_time_get_hour (dt), tm.tm_hour);
+  assert_equals_int (gst_date_time_get_minute (dt), tm.tm_min);
+  assert_equals_int (gst_date_time_get_second (dt), tm.tm_sec);
+  gst_date_time_unref (dt);
+}
+
+GST_END_TEST;
+
+GST_START_TEST (test_GstDateTime_new_from_unix_epoch)
+{
+  GstDateTime *dt;
+  struct tm tm;
+  time_t t;
+
+  memset (&tm, 0, sizeof (tm));
+  t = time (NULL);
+  localtime_r (&t, &tm);
+  dt = gst_date_time_new_from_unix_epoch (t);
+  assert_equals_int (gst_date_time_get_year (dt), 1900 + tm.tm_year);
+  assert_equals_int (gst_date_time_get_month (dt), 1 + tm.tm_mon);
+  assert_equals_int (gst_date_time_get_day (dt), tm.tm_mday);
+  assert_equals_int (gst_date_time_get_hour (dt), tm.tm_hour);
+  assert_equals_int (gst_date_time_get_minute (dt), tm.tm_min);
+  assert_equals_int (gst_date_time_get_second (dt), tm.tm_sec);
+  gst_date_time_unref (dt);
+
+  memset (&tm, 0, sizeof (tm));
+  tm.tm_year = 70;
+  tm.tm_mday = 1;
+  tm.tm_mon = 0;
+  tm.tm_hour = 0;
+  tm.tm_min = 0;
+  tm.tm_sec = 0;
+  t = mktime (&tm);
+  dt = gst_date_time_new_from_unix_epoch (t);
+  assert_equals_int (gst_date_time_get_year (dt), 1970);
+  assert_equals_int (gst_date_time_get_month (dt), 1);
+  assert_equals_int (gst_date_time_get_day (dt), 1);
+  assert_equals_int (gst_date_time_get_hour (dt), 0);
+  assert_equals_int (gst_date_time_get_minute (dt), 0);
+  assert_equals_int (gst_date_time_get_second (dt), 0);
+  gst_date_time_unref (dt);
+}
+
+GST_END_TEST;
+
+GST_START_TEST (test_GstDateTime_get_dmy)
+{
+  GstDateTime *dt;
+  time_t t;
+  struct tm tt;
+
+  t = time (NULL);
+  localtime_r (&t, &tt);
+  dt = gst_date_time_new_from_unix_epoch (t);
+  assert_equals_int (gst_date_time_get_year (dt), tt.tm_year + 1900);
+  assert_equals_int (gst_date_time_get_month (dt), tt.tm_mon + 1);
+  assert_equals_int (gst_date_time_get_day (dt), tt.tm_mday);
+}
+
+GST_END_TEST;
+
+GST_START_TEST (test_GstDateTime_get_hour)
+{
+  GstDateTime *dt;
+
+  dt = gst_date_time_new (2009, 10, 19, 15, 13, 11, 0, 0);
+  assert_equals_int (15, gst_date_time_get_hour (dt));
+  gst_date_time_unref (dt);
+
+  dt = gst_date_time_new (100, 10, 19, 1, 0, 0, 0, 0);
+  assert_equals_int (1, gst_date_time_get_hour (dt));
+  gst_date_time_unref (dt);
+
+  dt = gst_date_time_new (100, 10, 19, 0, 0, 0, 0, 0);
+  assert_equals_int (0, gst_date_time_get_hour (dt));
+  gst_date_time_unref (dt);
+
+  dt = gst_date_time_new (100, 10, 1, 23, 59, 59, 0, 0);
+  assert_equals_int (23, gst_date_time_get_hour (dt));
+  gst_date_time_unref (dt);
+}
+
+GST_END_TEST;
+
+GST_START_TEST (test_GstDateTime_get_microsecond)
+{
+  GTimeVal tv;
+  GstDateTime *dt;
+
+  g_get_current_time (&tv);
+  dt = gst_date_time_new (2010, 7, 15, 11, 12, 13, tv.tv_usec, 0);
+  assert_equals_int (tv.tv_usec, gst_date_time_get_microsecond (dt));
+  gst_date_time_unref (dt);
+}
+
+GST_END_TEST;
+
+GST_START_TEST (test_GstDateTime_get_minute)
+{
+  GstDateTime *dt;
+
+  dt = gst_date_time_new (2009, 12, 1, 1, 31, 0, 0, 0);
+  assert_equals_int (31, gst_date_time_get_minute (dt));
+  gst_date_time_unref (dt);
+}
+
+GST_END_TEST;
+
+GST_START_TEST (test_GstDateTime_get_second)
+{
+  GstDateTime *dt;
+
+  dt = gst_date_time_new (2009, 12, 1, 1, 31, 44, 0, 0);
+  assert_equals_int (44, gst_date_time_get_second (dt));
+  gst_date_time_unref (dt);
+}
+
+GST_END_TEST;
+
+GST_START_TEST (test_GstDateTime_new_full)
+{
+  GstDateTime *dt;
+
+  dt = gst_date_time_new (2009, 12, 11, 12, 11, 10, 1234, 0);
+  assert_equals_int (2009, gst_date_time_get_year (dt));
+  assert_equals_int (12, gst_date_time_get_month (dt));
+  assert_equals_int (11, gst_date_time_get_day (dt));
+  assert_equals_int (12, gst_date_time_get_hour (dt));
+  assert_equals_int (11, gst_date_time_get_minute (dt));
+  assert_equals_int (10, gst_date_time_get_second (dt));
+  assert_equals_int (1234, gst_date_time_get_microsecond (dt));
+  assert_equals_int (0, gst_date_time_get_time_zone_offset (dt));
+  gst_date_time_unref (dt);
+}
+
+GST_END_TEST;
+
+GST_START_TEST (test_GstDateTime_utc_now)
+{
+  GstDateTime *dt;
+  time_t t;
+  struct tm tm;
+
+  t = time (NULL);
+  gmtime_r (&t, &tm);
+  dt = gst_date_time_new_now_utc ();
+  assert_equals_int (tm.tm_year + 1900, gst_date_time_get_year (dt));
+  assert_equals_int (tm.tm_mon + 1, gst_date_time_get_month (dt));
+  assert_equals_int (tm.tm_mday, gst_date_time_get_day (dt));
+  assert_equals_int (tm.tm_hour, gst_date_time_get_hour (dt));
+  assert_equals_int (tm.tm_min, gst_date_time_get_minute (dt));
+  assert_equals_int (tm.tm_sec, gst_date_time_get_second (dt));
+  gst_date_time_unref (dt);
+}
+
+GST_END_TEST;
+
+GST_START_TEST (test_GstDateTime_get_utc_offset)
+{
+  GstDateTime *dt;
+  gfloat ts;
+  struct tm tm;
+  time_t t;
+
+  t = time (NULL);
+  memset (&tm, 0, sizeof (tm));
+  localtime_r (&t, &tm);
+
+  dt = gst_date_time_new_now_local_time ();
+  ts = gst_date_time_get_time_zone_offset (dt);
+  assert_equals_int (ts, tm.tm_gmtoff / 3600.0);
+  gst_date_time_unref (dt);
+}
+
+GST_END_TEST;
+
+static Suite *
+gst_date_time_suite (void)
+{
+  Suite *s = suite_create ("GstDateTime");
+  TCase *tc_chain = tcase_create ("general");
+
+  suite_add_tcase (s, tc_chain);
+  tcase_add_test (tc_chain, test_GstDateTime_get_dmy);
+  tcase_add_test (tc_chain, test_GstDateTime_get_hour);
+  tcase_add_test (tc_chain, test_GstDateTime_get_microsecond);
+  tcase_add_test (tc_chain, test_GstDateTime_get_minute);
+  tcase_add_test (tc_chain, test_GstDateTime_get_second);
+  tcase_add_test (tc_chain, test_GstDateTime_get_utc_offset);
+  tcase_add_test (tc_chain, test_GstDateTime_new_from_unix_epoch);
+  tcase_add_test (tc_chain, test_GstDateTime_new_full);
+  tcase_add_test (tc_chain, test_GstDateTime_now);
+  tcase_add_test (tc_chain, test_GstDateTime_utc_now);
+
+  return s;
+}
+
+GST_CHECK_MAIN (gst_date_time);
index d16775b..79dcf48 100644 (file)
@@ -228,6 +228,21 @@ EXPORTS
        gst_core_error_get_type
        gst_core_error_quark
        gst_date_get_type
+       gst_date_time_get_day
+       gst_date_time_get_hour
+       gst_date_time_get_microsecond
+       gst_date_time_get_minute
+       gst_date_time_get_month
+       gst_date_time_get_second
+       gst_date_time_get_time_zone_offset
+       gst_date_time_get_year
+       gst_date_time_new
+       gst_date_time_new_from_unix_epoch
+       gst_date_time_new_local_time
+       gst_date_time_new_now_local_time
+       gst_date_time_new_now_utc
+       gst_date_time_ref
+       gst_date_time_unref
        gst_debug_add_log_function
        gst_debug_category_free
        gst_debug_category_get_color