diff --git a/apps/emqx/src/emqx_datetime.erl b/apps/emqx/src/emqx_datetime.erl index 2ee403f14..a33ba537f 100644 --- a/apps/emqx/src/emqx_datetime.erl +++ b/apps/emqx/src/emqx_datetime.erl @@ -20,7 +20,8 @@ %% API -export([ to_epoch_millisecond/1, - to_epoch_second/1 + to_epoch_second/1, + human_readable_duration_string/1 ]). -export([ epoch_to_rfc3339/1, @@ -61,6 +62,15 @@ epoch_to_rfc3339(TimeStamp) -> epoch_to_rfc3339(TimeStamp, Unit) when is_integer(TimeStamp) -> list_to_binary(calendar:system_time_to_rfc3339(TimeStamp, [{unit, Unit}])). +-spec human_readable_duration_string(integer()) -> string(). +human_readable_duration_string(Milliseconds) -> + Seconds = Milliseconds div 1000, + {D, {H, M, S}} = calendar:seconds_to_daystime(Seconds), + L0 = [{D, " days"}, {H, " hours"}, {M, " minutes"}, {S, " seconds"}], + L1 = lists:dropwhile(fun({K, _}) -> K =:= 0 end, L0), + L2 = lists:map(fun({Time, Unit}) -> [integer_to_list(Time), Unit] end, L1), + lists:flatten(lists:join(", ", L2)). + -ifdef(TEST). -include_lib("eunit/include/eunit.hrl"). -compile(nowarn_export_all). diff --git a/apps/emqx_management/src/emqx_mgmt_cli.erl b/apps/emqx_management/src/emqx_mgmt_cli.erl index 8e6b303bb..ab88c4a09 100644 --- a/apps/emqx_management/src/emqx_mgmt_cli.erl +++ b/apps/emqx_management/src/emqx_mgmt_cli.erl @@ -82,7 +82,9 @@ status(_) -> broker([]) -> Funs = [sysdescr, version, datetime], [emqx_ctl:print("~-10s: ~ts~n", [Fun, emqx_sys:Fun()]) || Fun <- Funs], - emqx_ctl:print("~-10s: ~p~n", [uptime, emqx_sys:uptime()]); + emqx_ctl:print("~-10s: ~ts~n", [ + uptime, emqx_datetime:human_readable_duration_string(emqx_sys:uptime()) + ]); broker(["stats"]) -> [ emqx_ctl:print("~-30s: ~w~n", [Stat, Val])