datetime: Add constructor for timestamps in microseconds

This commit is contained in:
Linus Svensson 2019-11-22 16:04:20 +01:00
parent fa8312472f
commit 47c50b086e
3 changed files with 104 additions and 0 deletions

View 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)

View file

@ -105,6 +105,12 @@ GstDateTime * gst_date_time_new_from_unix_epoch_local_time (gint64 secs) G_GNU
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,

View 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);