datetime: Add _from_unix_epoch variants
authorThiago Santos <thiago.sousa.santos@collabora.co.uk>
Wed, 17 Nov 2010 12:39:34 +0000 (09:39 -0300)
committerThiago Santos <thiago.sousa.santos@collabora.co.uk>
Wed, 17 Nov 2010 12:58:32 +0000 (09:58 -0300)
Adds 2 variants for the gst_date_time_from_unix_epoch function,
one for UTC and another for local time.

API: gst_date_time_new_from_unix_epoch_utc
API: gst_date_time_new_from_unix_epoch_local_time

Fixes #653031

https://bugzilla.gnome.org/show_bug.cgi?id=635031

configure.ac
gst/gstdatetime.c
gst/gstdatetime.h
tests/check/gst/gstdatetime.c
win32/common/libgstreamer.def

index 12d9721..39f6ab5 100644 (file)
@@ -374,6 +374,7 @@ AC_MSG_RESULT($have_tm_gmtoff)
 dnl *** checks for library functions ***
 
 AC_CHECK_FUNCS([gmtime_r])
+AC_CHECK_FUNCS([localtime_r])
 AC_CHECK_FUNCS([sigaction])
 
 dnl check for fseeko()
index 97ddc60..e05d222 100644 (file)
  */
 
 /**
- * gst_date_time_new_from_unix_epoch:
+ * gst_date_time_new_from_unix_epoch_local_time:
  * @secs: seconds from the Unix epoch
  *
  * Creates a new #GstDateTime using the time since Jan 1, 1970 specified by
  */
 
 /**
+ * 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.
+ *
+ * Return value: the newly created #GstDateTime
+ *
+ * Since: 0.10.31
+ */
+
+/**
  * gst_date_time_new_local_time:
  * @year: the gregorian year
  * @month: the gregorian month
@@ -408,7 +420,7 @@ gst_date_time_get_time_zone_offset (const GstDateTime * datetime)
 }
 
 GstDateTime *
-gst_date_time_new_from_unix_epoch (gint64 secs)
+gst_date_time_new_from_unix_epoch_local_time (gint64 secs)
 {
   GstDateTime *dt;
   struct tm tm;
@@ -430,6 +442,27 @@ gst_date_time_new_from_unix_epoch (gint64 secs)
 }
 
 GstDateTime *
+gst_date_time_new_from_unix_epoch_utc (gint64 secs)
+{
+  GstDateTime *dt;
+  struct tm tm;
+  time_t tt;
+
+  memset (&tm, 0, sizeof (tm));
+  tt = (time_t) secs;
+
+#ifdef HAVE_GMTIME_R
+  gmtime_r (&tt, &tm);
+#else
+  memcpy (&tm, gmtime (&tt), sizeof (struct tm));
+#endif
+
+  dt = gst_date_time_new (0, tm.tm_year + 1900,
+      tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
+  return dt;
+}
+
+GstDateTime *
 gst_date_time_new_local_time (gint year, gint month, gint day, gint hour,
     gint minute, gdouble seconds)
 {
@@ -473,7 +506,7 @@ gst_date_time_new_now_local_time (void)
   GTimeVal tv;
   g_get_current_time (&tv);
 
-  datetime = gst_date_time_new_from_unix_epoch (tv.tv_sec);
+  datetime = gst_date_time_new_from_unix_epoch_local_time (tv.tv_sec);
   datetime->usec += tv.tv_usec;
   gst_date_time_set_local_timezone (datetime);
   return datetime;
@@ -656,13 +689,20 @@ gst_date_time_get_time_zone_offset (const GstDateTime * datetime)
 }
 
 GstDateTime *
-gst_date_time_new_from_unix_epoch (gint64 secs)
+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));
 }
 
 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));
+}
+
+GstDateTime *
 gst_date_time_new_local_time (gint year, gint month, gint day, gint hour,
     gint minute, gdouble seconds)
 {
index 957faae..eeeb020 100644 (file)
@@ -45,7 +45,8 @@ 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_from_unix_epoch_local_time (gint64 secs);
+GstDateTime    *gst_date_time_new_from_unix_epoch_utc  (gint64 secs);
 GstDateTime    *gst_date_time_new_local_time           (gint year, gint month,
                                                         gint day, gint hour,
                                                         gint minute,
index 74e2291..06fc540 100644 (file)
@@ -38,7 +38,11 @@ GST_START_TEST (test_GstDateTime_now)
 
   memset (&tm, 0, sizeof (tm));
   t = time (NULL);
+#ifdef HAVE_LOCALTIME_R
   localtime_r (&t, &tm);
+#else
+  memcpy (&tm, localtime (&t), sizeof (struct tm));
+#endif
   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);
@@ -51,7 +55,7 @@ GST_START_TEST (test_GstDateTime_now)
 
 GST_END_TEST;
 
-GST_START_TEST (test_GstDateTime_new_from_unix_epoch)
+GST_START_TEST (test_GstDateTime_new_from_unix_epoch_local_time)
 {
   GstDateTime *dt;
   struct tm tm;
@@ -59,8 +63,12 @@ GST_START_TEST (test_GstDateTime_new_from_unix_epoch)
 
   memset (&tm, 0, sizeof (tm));
   t = time (NULL);
+#ifdef HAVE_LOCALTIME_R
   localtime_r (&t, &tm);
-  dt = gst_date_time_new_from_unix_epoch (t);
+#else
+  memcpy (&tm, localtime (&t), sizeof (struct tm));
+#endif
+  dt = gst_date_time_new_from_unix_epoch_local_time (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);
@@ -77,7 +85,7 @@ GST_START_TEST (test_GstDateTime_new_from_unix_epoch)
   tm.tm_min = 0;
   tm.tm_sec = 0;
   t = mktime (&tm);
-  dt = gst_date_time_new_from_unix_epoch (t);
+  dt = gst_date_time_new_from_unix_epoch_local_time (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);
@@ -89,6 +97,33 @@ GST_START_TEST (test_GstDateTime_new_from_unix_epoch)
 
 GST_END_TEST;
 
+
+GST_START_TEST (test_GstDateTime_new_from_unix_epoch_utc)
+{
+  GstDateTime *dt;
+  struct tm tm;
+  time_t t;
+
+  memset (&tm, 0, sizeof (tm));
+  t = time (NULL);
+#ifdef HAVE_GMTIME_R
+  gmtime_r (&t, &tm);
+#else
+  memcpy (&tm, gmtime (&t), sizeof (struct tm));
+#endif
+  dt = gst_date_time_new_from_unix_epoch_utc (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);
+  assert_equals_int (gst_date_time_get_time_zone_offset (dt), 0);
+  gst_date_time_unref (dt);
+}
+
+GST_END_TEST;
+
 GST_START_TEST (test_GstDateTime_get_dmy)
 {
   GstDateTime *dt;
@@ -96,8 +131,12 @@ GST_START_TEST (test_GstDateTime_get_dmy)
   struct tm tt;
 
   t = time (NULL);
+#ifdef HAVE_LOCALTIME_R
   localtime_r (&t, &tt);
-  dt = gst_date_time_new_from_unix_epoch (t);
+#else
+  memcpy (&tt, localtime (&t), sizeof (struct tm));
+#endif
+  dt = gst_date_time_new_from_unix_epoch_local_time (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);
@@ -191,7 +230,11 @@ GST_START_TEST (test_GstDateTime_utc_now)
   struct tm tm;
 
   t = time (NULL);
+#ifdef HAVE_GMTIME_R
   gmtime_r (&t, &tm);
+#else
+  memcpy (&tm, gmtime (&t), sizeof (struct tm));
+#endif
   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));
@@ -213,7 +256,11 @@ GST_START_TEST (test_GstDateTime_get_utc_offset)
 
   t = time (NULL);
   memset (&tm, 0, sizeof (tm));
+#ifdef HAVE_LOCALTIME_R
   localtime_r (&t, &tm);
+#else
+  memcpy (&tm, localtime (&t), sizeof (struct tm));
+#endif
 
   dt = gst_date_time_new_now_local_time ();
   ts = gst_date_time_get_time_zone_offset (dt);
@@ -236,7 +283,8 @@ gst_date_time_suite (void)
   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_from_unix_epoch_local_time);
+  tcase_add_test (tc_chain, test_GstDateTime_new_from_unix_epoch_utc);
   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);
index b20e602..44d83d5 100644 (file)
@@ -239,7 +239,8 @@ EXPORTS
        gst_date_time_get_type
        gst_date_time_get_year
        gst_date_time_new
-       gst_date_time_new_from_unix_epoch
+       gst_date_time_new_from_unix_epoch_local_time
+       gst_date_time_new_from_unix_epoch_utc
        gst_date_time_new_local_time
        gst_date_time_new_now_local_time
        gst_date_time_new_now_utc