Returns the current OS system time
in the same format as erlang:timestamp/0
. The tuple can be used together with function calendar:now_to_universal_time/1
or calendar:now_to_local_time/1
to get calendar time. Using the calendar time, together with the MicroSecs
part of the return tuple from this function, allows you to log time stamps in high resolution and consistent with the time in the rest of the OS.
Example of code formatting a string in format "DD Mon YYYY HH:MM:SS.mmmmmm", where DD is the day of month, Mon is the textual month name, YYYY is the year, HH:MM:SS is the time, and mmmmmm is the microseconds in six positions:
-module(print_time).
-export([format_utc_timestamp/0]).
format_utc_timestamp() ->
TS = {_,_,Micro} = os:timestamp(),
{{Year,Month,Day},{Hour,Minute,Second}} =
calendar:now_to_universal_time(TS),
Mstr = element(Month,{"Jan","Feb","Mar","Apr","May","Jun","Jul",
"Aug","Sep","Oct","Nov","Dec"}),
io_lib:format("~2w ~s ~4w ~2w:~2..0w:~2..0w.~6..0w",
[Day,Mstr,Year,Hour,Minute,Second,Micro]).
This module can be used as follows:
1> io:format("~s~n",[print_time:format_utc_timestamp()]).
29 Apr 2009 9:55:30.051711
OS system time can also be retrieved by system_time/0
and system_time/1
.