Skip to content

Commit

Permalink
filterx/object-datetime: add separate str()
Browse files Browse the repository at this point in the history
str() is a UNIX timestamp since epoch, including fractions of a digits,
but no timezone.

repr() remains an ISO date so that logs are easier to read.

Signed-off-by: Balazs Scheidler <[email protected]>
  • Loading branch information
bazsi committed Feb 16, 2025
1 parent 558ed14 commit ae9a16f
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 6 deletions.
17 changes: 17 additions & 0 deletions lib/filterx/object-datetime.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,13 @@ datetime_repr(const UnixTime *ut, GString *repr)
return TRUE;
}

gboolean
datetime_str(const UnixTime *ut, GString *repr)
{
_convert_unix_time_to_string(ut, repr, FALSE);
return TRUE;
}

static gboolean
_repr(FilterXObject *s, GString *repr)
{
Expand All @@ -205,6 +212,15 @@ _repr(FilterXObject *s, GString *repr)
return datetime_repr(&self->ut, repr);
}

static gboolean
_str(FilterXObject *s, GString *repr)
{
FilterXDateTime *self = (FilterXDateTime *) s;

_convert_unix_time_to_string(&self->ut, repr, FALSE);
return TRUE;
}

static FilterXObject *
_add(FilterXObject *s, FilterXObject *object)
{
Expand Down Expand Up @@ -563,5 +579,6 @@ FILTERX_DEFINE_TYPE(datetime, FILTERX_TYPE_NAME(object),
.map_to_json = _map_to_json,
.marshal = _marshal,
.repr = _repr,
.str = _str,
.add = _add,
);
1 change: 1 addition & 0 deletions lib/filterx/object-datetime.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,6 @@ FilterXExpr *filterx_function_strptime_new(FilterXFunctionArgs *args, GError **e
FilterXExpr *filterx_function_strftime_new(FilterXFunctionArgs *args, GError **error);

gboolean datetime_repr(const UnixTime *ut, GString *repr);
gboolean datetime_str(const UnixTime *ut, GString *repr);

#endif
20 changes: 20 additions & 0 deletions lib/filterx/object-message-value.c
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,25 @@ _repr(FilterXObject *s, GString *repr)
return FALSE;
}

static gboolean
_str(FilterXObject *s, GString *repr)
{
FilterXMessageValue *self = (FilterXMessageValue *) s;
switch (self->type)
{
case LM_VT_DATETIME:
{
UnixTime ut = UNIX_TIME_INIT;
if (!type_cast_to_datetime_unixtime(self->repr, self->repr_len, &ut, NULL))
return FALSE;
return datetime_str(&ut, repr);
}
default:
_repr(s, repr);
return TRUE;
}
}

FILTERX_DEFINE_TYPE(message_value, FILTERX_TYPE_NAME(object),
.free_fn = _free,
.truthy = _truthy,
Expand All @@ -442,4 +461,5 @@ FILTERX_DEFINE_TYPE(message_value, FILTERX_TYPE_NAME(object),
.len = _len,
.map_to_json = _map_to_json,
.repr = _repr,
.str = _str,
);
16 changes: 16 additions & 0 deletions lib/filterx/tests/test_object_datetime.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,22 @@ Test(filterx_datetime, test_filterx_object_datetime_maps_to_the_right_json_value
filterx_object_unref(fobj);
}

Test(filterx_datetime, test_filterx_object_datetime_str_yields_unix_time)
{
UnixTime ut = { .ut_sec = 1701350398, .ut_usec = 123000, .ut_gmtoff = 3600 };
FilterXObject *fobj = filterx_datetime_new(&ut);
assert_object_str_equals(fobj, "1701350398.123000");
filterx_object_unref(fobj);
}

Test(filterx_datetime, test_filterx_object_datetime_repr_yields_isotime)
{
UnixTime ut = { .ut_sec = 1701350398, .ut_usec = 123000, .ut_gmtoff = 3600 };
FilterXObject *fobj = filterx_datetime_new(&ut);
assert_object_repr_equals(fobj, "2023-11-30T14:19:58.123+01:00");
filterx_object_unref(fobj);
}

Test(filterx_datetime, test_filterx_datetime_typecast_null_args)
{
FilterXObject *obj = filterx_typecast_datetime(NULL, NULL, 0);
Expand Down
6 changes: 4 additions & 2 deletions lib/filterx/tests/test_object_message.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,12 @@ Test(filterx_message, test_filterx_message_type_datetime_repr)
FilterXObject *fobj = filterx_message_value_new(val, -1, LM_VT_DATETIME);
GString *repr = scratch_buffers_alloc();
g_string_assign(repr, "foo");
cr_assert(filterx_object_str(fobj, repr));
cr_assert_str_eq(repr->str, "1713520972.000000");
cr_assert(filterx_object_repr(fobj, repr));
cr_assert_str_eq("2024-04-19T12:02:52.000+02:00", repr->str);
cr_assert_str_eq(repr->str, "2024-04-19T12:02:52.000+02:00");
cr_assert(filterx_object_repr_append(fobj, repr));
cr_assert_str_eq("2024-04-19T12:02:52.000+02:002024-04-19T12:02:52.000+02:00", repr->str);
cr_assert_str_eq(repr->str, "2024-04-19T12:02:52.000+02:002024-04-19T12:02:52.000+02:00");
filterx_object_unref(fobj);
}

Expand Down
8 changes: 4 additions & 4 deletions tests/light/functional_tests/filterx/test_filterx.py
Original file line number Diff line number Diff line change
Expand Up @@ -1878,8 +1878,8 @@ def test_add_operator_for_base_types(config, syslog_ng):
$MSG = {};
$MSG.string = ${values.str} + "bar" + "baz";
$MSG.bytes = string(bytes("\xCA") + bytes("\xFE"));
$MSG.datetime_integer = string(strptime("2000-01-01T00:00:00Z", "%Y-%m-%dT%H:%M:%S%z") + 3600000000);
$MSG.datetime_double = string(strptime("2000-01-01T00:00:00Z", "%Y-%m-%dT%H:%M:%S%z") + 3600.0);
$MSG.datetime_integer = repr(strptime("2000-01-01T00:00:00Z", "%Y-%m-%dT%H:%M:%S%z") + 3600000000);
$MSG.datetime_double = repr(strptime("2000-01-01T00:00:00Z", "%Y-%m-%dT%H:%M:%S%z") + 3600.0);
$MSG.integer_integer = 3 + 4 + 5;
$MSG.integer_double = 3 + 0.5;
$MSG.double_integer = 3.5 + 2;
Expand Down Expand Up @@ -1987,11 +1987,11 @@ def test_plus_equal_grammar_rules(config, syslog_ng):
d = strptime("2000-01-01T00:00:00Z", "%Y-%m-%dT%H:%M:%S%z");
d += 3600000000;
$MSG.var_datetime_integer = string(d);
$MSG.var_datetime_integer = repr(d);
e = strptime("2000-01-01T00:00:00Z", "%Y-%m-%dT%H:%M:%S%z");
e += 3600.000000;
$MSG.var_datetime_double = string(e);
$MSG.var_datetime_double = repr(e);
$MSG.attr = "tik";
$MSG.attr += "tak";
Expand Down

0 comments on commit ae9a16f

Please sign in to comment.