datetime: Add constructor for timestamps in microseconds
authorLinus Svensson <linussn@axis.com>
Fri, 22 Nov 2019 15:04:20 +0000 (16:04 +0100)
committerLinus Svensson <linussn@axis.com>
Mon, 25 Nov 2019 12:31:11 +0000 (13:31 +0100)
gst/gstdatetime.c
gst/gstdatetime.h
tests/check/gst/gstdatetime.c

index e906f19..8d363de 100644 (file)
@@ -449,6 +449,54 @@ gst_date_time_new_from_unix_epoch_utc (gint64 secs)
   return datetime;
 }
 
+/**
+ * gst_date_time_new_from_unix_epoch_local_time_usecs:
+ * @usecs: microseconds from the Unix epoch
+ *
+ * Creates a new #GstDateTime using the time since Jan 1, 1970 specified by
+ * @usecs. The #GstDateTime is in the local timezone.
+ *
+ * Returns: (transfer full): a newly created #GstDateTime
+ *
+ * Since: 1.18
+ */
+GstDateTime *
+gst_date_time_new_from_unix_epoch_local_time_usecs (gint64 usecs)
+{
+  GDateTime *dt, *datetime;
+  gint64 secs = usecs / G_USEC_PER_SEC;
+  gint64 usec_part = usecs % G_USEC_PER_SEC;
+
+  dt = g_date_time_new_from_unix_local (secs);
+  datetime = g_date_time_add_seconds (dt, (gdouble) usec_part / G_USEC_PER_SEC);
+  g_date_time_unref (dt);
+  return gst_date_time_new_from_g_date_time (datetime);
+}
+
+/**
+ * gst_date_time_new_from_unix_epoch_utc_usecs:
+ * @usecs: microseconds from the Unix epoch
+ *
+ * Creates a new #GstDateTime using the time since Jan 1, 1970 specified by
+ * @usecs. The #GstDateTime is in UTC.
+ *
+ * Returns: (transfer full): a newly created #GstDateTime
+ *
+ * Since: 1.18
+ */
+GstDateTime *
+gst_date_time_new_from_unix_epoch_utc_usecs (gint64 usecs)
+{
+  GDateTime *dt, *datetime;
+  gint64 secs = usecs / G_USEC_PER_SEC;
+  gint64 usec_part = usecs % G_USEC_PER_SEC;
+
+  dt = g_date_time_new_from_unix_utc (secs);
+  datetime = g_date_time_add_seconds (dt, (gdouble) usec_part / G_USEC_PER_SEC);
+  g_date_time_unref (dt);
+  return gst_date_time_new_from_g_date_time (datetime);
+}
+
 static GstDateTimeFields
 gst_date_time_check_fields (gint * year, gint * month, gint * day,
     gint * hour, gint * minute, gdouble * seconds)
index 8a056ce..ede2b44 100644 (file)
@@ -106,6 +106,12 @@ GST_API
 GstDateTime *   gst_date_time_new_from_unix_epoch_utc   (gint64 secs) G_GNUC_MALLOC;
 
 GST_API
+GstDateTime *   gst_date_time_new_from_unix_epoch_local_time_usecs (gint64 usecs) G_GNUC_MALLOC;
+
+GST_API
+GstDateTime *   gst_date_time_new_from_unix_epoch_utc_usecs (gint64 usecs) G_GNUC_MALLOC;
+
+GST_API
 GstDateTime *   gst_date_time_new_local_time            (gint year,
                                                          gint month,
                                                          gint day,
index 2932f2c..bd9fa89 100644 (file)
@@ -131,6 +131,53 @@ GST_START_TEST (test_GstDateTime_new_from_unix_epoch_utc)
 
 GST_END_TEST;
 
+GST_START_TEST (test_GstDateTime_new_from_unix_epoch_local_time_usecs)
+{
+  GstDateTime *dt;
+  struct tm tm;
+  /* 2007-01-01T20:00:00.500000Z */
+  gint64 time = G_GINT64_CONSTANT (1167681600500000);
+  time_t t = (time_t) (time / G_USEC_PER_SEC);
+
+#ifdef HAVE_LOCALTIME_R
+  localtime_r (&t, &tm);
+#else
+  memcpy (&tm, localtime (&t), sizeof (struct tm));
+#endif
+
+  dt = gst_date_time_new_from_unix_epoch_local_time_usecs (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);
+  assert_equals_int (gst_date_time_get_microsecond (dt), 500000);
+  gst_date_time_unref (dt);
+}
+
+GST_END_TEST;
+
+GST_START_TEST (test_GstDateTime_new_from_unix_epoch_utc_usecs)
+{
+  GstDateTime *dt;
+  /* 2007-01-01T20:00:00.500000Z */
+  gint64 time = G_GINT64_CONSTANT (1167681600500000);
+
+  dt = gst_date_time_new_from_unix_epoch_utc_usecs (time);
+  assert_equals_int (gst_date_time_get_year (dt), 2007);
+  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), 20);
+  assert_equals_int (gst_date_time_get_minute (dt), 0);
+  assert_equals_int (gst_date_time_get_second (dt), 0);
+  assert_equals_int (gst_date_time_get_microsecond (dt), 500000);
+  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;
@@ -776,6 +823,9 @@ gst_date_time_suite (void)
   tcase_add_test (tc_chain, test_GstDateTime_get_utc_offset);
   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_from_unix_epoch_local_time_usecs);
+  tcase_add_test (tc_chain, test_GstDateTime_new_from_unix_epoch_utc_usecs);
   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);