Skip to content

Commit

Permalink
$(format-date): add support for UnixTime based DATETIME values
Browse files Browse the repository at this point in the history
This adds support for timezones. Unfortunetely strftime() does not support
fractions of a second, so that's not yet supported.

Signed-off-by: Balazs Scheidler <bazsi77@gmail.com>
  • Loading branch information
bazsi committed Feb 10, 2023
1 parent b5d8324 commit 031b2bd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
5 changes: 5 additions & 0 deletions modules/timestamp/tests/test_format_date.c
Expand Up @@ -81,6 +81,11 @@ Test(format_date, test_format_date_with_timestamp_argument_formats_it_using_strf
assert_template_format("$(format-date %Y-%m-%dT%H:%M:%S 1667500613)", "2022-11-03T19:36:53");
}

Test(format_date, test_format_date_with_timestamp_argument_using_fractions_and_timezone_works)
{
assert_template_format("$(format-date %Y-%m-%dT%H:%M:%S 1667500613.613+05:00)", "2022-11-03T23:36:53");
}

Test(format_date, test_format_date_with_time_zone_option_overrrides_timezone)
{
assert_template_format("$(format-date --time-zone PST8PDT %Y-%m-%dT%H:%M:%S 1667500613)", "2022-11-03T11:36:53");
Expand Down
11 changes: 6 additions & 5 deletions modules/timestamp/tf-format-date.c
Expand Up @@ -117,11 +117,12 @@ tf_format_date_call(LogTemplateFunction *self, gpointer s, const LogTemplateInvo
if (state->super.argc != 0)
{
const gchar *ts = args->argv[0]->str;
if (!type_cast_to_datetime_msec(ts, &msec, NULL))
msec = 0;
ut.ut_sec = msec / 1000;
ut.ut_usec = (msec % 1000) * 1000;
ut.ut_gmtoff = -1;
if (!type_cast_to_datetime_unixtime(ts, &ut, NULL))
{
ut.ut_sec = 0;
ut.ut_usec = 0;
ut.ut_gmtoff = -1;
}
}
else
{
Expand Down

0 comments on commit 031b2bd

Please sign in to comment.