Sfoglia il codice sorgente

Merge pull request #10392 from kjellwinblad/kjell/rule_engine/add_missing_datetime_function/EMQX-9245

freat: add date_to_unix_ts/3 function to the rule engine
Kjell Winblad 2 anni fa
parent
commit
0c727fc9b2

+ 4 - 0
apps/emqx_rule_engine/src/emqx_rule_funcs.erl

@@ -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,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) ->
 date_to_unix_ts(TimeUnit, Offset, FormatString, InputString) ->
     emqx_rule_date:parse_date(
     emqx_rule_date:parse_date(
         time_unit(TimeUnit),
         time_unit(TimeUnit),

+ 21 - 3
apps/emqx_rule_engine/test/emqx_rule_funcs_SUITE.erl

@@ -958,7 +958,7 @@ prop_format_date_fun() ->
     Args1 = [<<"second">>, <<"+07:00">>, <<"%m--%d--%y---%H:%M:%S%Z">>],
     Args1 = [<<"second">>, <<"+07:00">>, <<"%m--%d--%y---%H:%M:%S%Z">>],
     ?FORALL(
     ?FORALL(
         S,
         S,
-        erlang:system_time(second),
+        range(0, 4000000000),
         S ==
         S ==
             apply_func(
             apply_func(
                 date_to_unix_ts,
                 date_to_unix_ts,
@@ -974,7 +974,7 @@ prop_format_date_fun() ->
     Args2 = [<<"millisecond">>, <<"+04:00">>, <<"--%m--%d--%y---%H:%M:%S%Z">>],
     Args2 = [<<"millisecond">>, <<"+04:00">>, <<"--%m--%d--%y---%H:%M:%S%Z">>],
     ?FORALL(
     ?FORALL(
         S,
         S,
-        erlang:system_time(millisecond),
+        range(0, 4000000000),
         S ==
         S ==
             apply_func(
             apply_func(
                 date_to_unix_ts,
                 date_to_unix_ts,
@@ -990,7 +990,7 @@ prop_format_date_fun() ->
     Args = [<<"second">>, <<"+08:00">>, <<"%y-%m-%d-%H:%M:%S%Z">>],
     Args = [<<"second">>, <<"+08:00">>, <<"%y-%m-%d-%H:%M:%S%Z">>],
     ?FORALL(
     ?FORALL(
         S,
         S,
-        erlang:system_time(second),
+        range(0, 4000000000),
         S ==
         S ==
             apply_func(
             apply_func(
                 date_to_unix_ts,
                 date_to_unix_ts,
@@ -1002,6 +1002,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]
+                        )
+                    ]
+            )
     ).
     ).
 
 
 %%------------------------------------------------------------------------------
 %%------------------------------------------------------------------------------

+ 1 - 0
changes/ce/feat-10392.en.md

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