feat(rule_engine): add rule sql functions for datetime rfc3339
This commit is contained in:
parent
73e563c883
commit
19a9bab3a4
|
@ -180,6 +180,10 @@
|
|||
%% Date functions
|
||||
-export([ now_rfc3339/0
|
||||
, now_rfc3339/1
|
||||
, unix_ts_to_rfc3339/1
|
||||
, unix_ts_to_rfc3339/2
|
||||
, rfc3339_to_unix_ts/1
|
||||
, rfc3339_to_unix_ts/2
|
||||
, now_timestamp/0
|
||||
, now_timestamp/1
|
||||
]).
|
||||
|
@ -834,9 +838,22 @@ now_rfc3339() ->
|
|||
now_rfc3339(<<"second">>).
|
||||
|
||||
now_rfc3339(Unit) ->
|
||||
unix_ts_to_rfc3339(now_timestamp(Unit), Unit).
|
||||
|
||||
unix_ts_to_rfc3339(Epoch) ->
|
||||
unix_ts_to_rfc3339(Epoch, <<"second">>).
|
||||
|
||||
unix_ts_to_rfc3339(Epoch, Unit) when is_integer(Epoch) ->
|
||||
emqx_rule_utils:bin(
|
||||
calendar:system_time_to_rfc3339(
|
||||
now_timestamp(Unit), [{unit, time_unit(Unit)}])).
|
||||
Epoch, [{unit, time_unit(Unit)}])).
|
||||
|
||||
rfc3339_to_unix_ts(DateTime) ->
|
||||
rfc3339_to_unix_ts(DateTime, <<"second">>).
|
||||
|
||||
rfc3339_to_unix_ts(DateTime, Unit) when is_binary(DateTime) ->
|
||||
calendar:rfc3339_to_system_time(binary_to_list(DateTime),
|
||||
[{unit, time_unit(Unit)}]).
|
||||
|
||||
now_timestamp() ->
|
||||
erlang:system_time(second).
|
||||
|
|
|
@ -706,7 +706,7 @@ t_disable_rule(_Config) ->
|
|||
types=[], params_spec = #{},
|
||||
title = #{en => <<"Simple Action">>},
|
||||
description = #{en => <<"Simple Action">>}}),
|
||||
{ok, #rule{actions = [#action_instance{id = ActInsId0}]}} = emqx_rule_engine:create_rule(
|
||||
{ok, #rule{actions = [#action_instance{}]}} = emqx_rule_engine:create_rule(
|
||||
#{id => <<"simple_rule_2">>,
|
||||
rawsql => <<"select * from \"t/#\"">>,
|
||||
actions => [#{name => 'simple_action_2', args => #{}}]
|
||||
|
|
|
@ -647,6 +647,23 @@ t_now_timestamp_1(_) ->
|
|||
apply_func(now_timestamp, [atom_to_binary(Unit, utf8)])))
|
||||
|| Unit <- [second,millisecond,microsecond,nanosecond]].
|
||||
|
||||
t_unix_ts_to_rfc3339(_) ->
|
||||
[begin
|
||||
BUnit = atom_to_binary(Unit, utf8),
|
||||
Epoch = apply_func(now_timestamp, [BUnit]),
|
||||
DateTime = apply_func(unix_ts_to_rfc3339, [Epoch, BUnit]),
|
||||
?assertEqual(Epoch,
|
||||
calendar:rfc3339_to_system_time(binary_to_list(DateTime), [{unit, Unit}]))
|
||||
end || Unit <- [second,millisecond,microsecond,nanosecond]].
|
||||
|
||||
t_rfc3339_to_unix_ts(_) ->
|
||||
[begin
|
||||
BUnit = atom_to_binary(Unit, utf8),
|
||||
Epoch = apply_func(now_timestamp, [BUnit]),
|
||||
DateTime = apply_func(unix_ts_to_rfc3339, [Epoch, BUnit]),
|
||||
?assertEqual(Epoch, emqx_rule_funcs:rfc3339_to_unix_ts(DateTime, BUnit))
|
||||
end || Unit <- [second,millisecond,microsecond,nanosecond]].
|
||||
|
||||
%%------------------------------------------------------------------------------
|
||||
%% Utility functions
|
||||
%%------------------------------------------------------------------------------
|
||||
|
|
Loading…
Reference in New Issue