Skip to content

Instantly share code, notes, and snippets.

@seriyps
Last active April 29, 2022 08:38
Show Gist options
  • Select an option

  • Save seriyps/db52ff310021ee243d1e4b7fe48a3442 to your computer and use it in GitHub Desktop.

Select an option

Save seriyps/db52ff310021ee243d1e4b7fe48a3442 to your computer and use it in GitHub Desktop.

Revisions

  1. seriyps revised this gist Apr 29, 2020. 1 changed file with 1 addition and 3 deletions.
    4 changes: 1 addition & 3 deletions uptime.erl
    Original file line number Diff line number Diff line change
    @@ -2,9 +2,7 @@

    %% @doc uptime in native time units
    uptime() ->
    Now = erlang:system_time(),
    StartTime = erlang:time_offset() + erlang:system_info(start_time),
    Now - StartTime.
    erlang:monotonic_time() - erlang:system_info(start_time).

    %% @doc uptime in specified time units
    uptime(Unit) ->
  2. seriyps created this gist Jun 24, 2017.
    23 changes: 23 additions & 0 deletions uptime.erl
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    -export([uptime/0, uptime/1, uptime_string/0]).

    %% @doc uptime in native time units
    uptime() ->
    Now = erlang:system_time(),
    StartTime = erlang:time_offset() + erlang:system_info(start_time),
    Now - StartTime.

    %% @doc uptime in specified time units
    uptime(Unit) ->
    erlang:convert_time_unit(uptime(), native, Unit).

    %% @doc uptime as binary formatted string
    uptime_string() ->
    {D, {H, M, S}} = calendar:seconds_to_daystime(uptime(seconds)),
    list_to_binary(
    io_lib:format("~pd, ~p:~p:~p", [D, H, M, S])).

    uptime2() ->
    {StartTime, _} = erlang:statistics(wall_clock),
    {D, {H, M, S}} = calendar:seconds_to_daystime(StartTime div 1000),
    list_to_binary(
    io_lib:format("~pd, ~p:~p:~p", [D, H, M, S])).