feat: add date_to_unix_ts/3 function to the rule engine

Fixes:
https://emqx.atlassian.net/browse/EMQX-9245
This commit is contained in:
Kjell Winblad 2023-04-13 15:20:04 +02:00
parent f3446c48f7
commit d3ccd8a65d
3 changed files with 28 additions and 0 deletions

View File

@ -227,6 +227,7 @@
now_timestamp/1, now_timestamp/1,
format_date/3, format_date/3,
format_date/4, format_date/4,
date_to_unix_ts/3,
date_to_unix_ts/4 date_to_unix_ts/4
]). ]).
@ -1085,6 +1086,14 @@ format_date(TimeUnit, Offset, FormatString, TimeEpoch) ->
) )
). ).
date_to_unix_ts(TimeUnit, FormatString, InputString) ->
emqx_rule_date:parse_date(
time_unit(TimeUnit),
"Z",
emqx_plugin_libs_rule:str(FormatString),
emqx_plugin_libs_rule:str(InputString)
).
date_to_unix_ts(TimeUnit, Offset, FormatString, InputString) -> date_to_unix_ts(TimeUnit, Offset, FormatString, InputString) ->
emqx_rule_date:parse_date( emqx_rule_date:parse_date(
time_unit(TimeUnit), time_unit(TimeUnit),

View File

@ -1003,6 +1003,24 @@ prop_format_date_fun() ->
) )
] ]
) )
),
%% When no offset is specified, the offset should be taken from the formatted time string
ArgsNoOffset = [<<"second">>, <<"%y-%m-%d-%H:%M:%S%Z">>],
ArgsOffset = [<<"second">>, <<"+08:00">>, <<"%y-%m-%d-%H:%M:%S%Z">>],
?FORALL(
S,
erlang:system_time(second),
S ==
apply_func(
date_to_unix_ts,
ArgsNoOffset ++
[
apply_func(
format_date,
ArgsOffset ++ [S]
)
]
)
). ).
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------

View File

@ -0,0 +1 @@
A new function to convert a formatted date to an integer timestamp has been added: date_to_unix_ts/3