Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

freat: add date_to_unix_ts/3 function to the rule engine #10392

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions apps/emqx_rule_engine/src/emqx_rule_funcs.erl
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@
now_timestamp/1,
format_date/3,
format_date/4,
date_to_unix_ts/3,
date_to_unix_ts/4
]).

Expand Down Expand Up @@ -1085,6 +1086,9 @@ format_date(TimeUnit, Offset, FormatString, TimeEpoch) ->
)
).

date_to_unix_ts(TimeUnit, FormatString, InputString) ->
date_to_unix_ts(TimeUnit, "Z", FormatString, InputString).

date_to_unix_ts(TimeUnit, Offset, FormatString, InputString) ->
emqx_rule_date:parse_date(
time_unit(TimeUnit),
Expand Down
24 changes: 21 additions & 3 deletions apps/emqx_rule_engine/test/emqx_rule_funcs_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -959,7 +959,7 @@ prop_format_date_fun() ->
Args1 = [<<"second">>, <<"+07:00">>, <<"%m--%d--%y---%H:%M:%S%Z">>],
?FORALL(
S,
erlang:system_time(second),
range(0, 4000000000),
S ==
apply_func(
date_to_unix_ts,
Expand All @@ -975,7 +975,7 @@ prop_format_date_fun() ->
Args2 = [<<"millisecond">>, <<"+04:00">>, <<"--%m--%d--%y---%H:%M:%S%Z">>],
?FORALL(
S,
erlang:system_time(millisecond),
range(0, 4000000000),
S ==
apply_func(
date_to_unix_ts,
Expand All @@ -991,7 +991,7 @@ prop_format_date_fun() ->
Args = [<<"second">>, <<"+08:00">>, <<"%y-%m-%d-%H:%M:%S%Z">>],
?FORALL(
S,
erlang:system_time(second),
range(0, 4000000000),
S ==
apply_func(
date_to_unix_ts,
Expand All @@ -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,
range(0, 4000000000),
S ==
apply_func(
date_to_unix_ts,
ArgsNoOffset ++
[
apply_func(
format_date,
ArgsOffset ++ [S]
)
]
)
).

%%------------------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions changes/ce/feat-10392.en.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A new function to convert a formatted date to an integer timestamp has been added: date_to_unix_ts/3