Erlang Central

Convert Epoch Seconds to DMYHMS

From ErlangCentral Wiki


You have a date and time in Erlang Epoch seconds (i.e., Gregorian calendar year 0 seconds), and you want to calculate the individual DMYHMS values from it.


In recipe TimeToday we did exactly this but only for the current date. How about if we wanted to get the date, and time (DMYHMS) associated to a particular number of seconds? Once more we would use gregorian_seconds_to_datetime.

1> Seconds = 1083022458.
2> DateTime = calendar:gregorian_seconds_to_datetime(Seconds).
3> {{Year, Month, Day}, {Hour, Min, Sec}} = DateTime.
4> io:fwrite("Today's Date is ~2B/~2B/~4..0B ~2B:~2.10.0B:~2.10.0B\n",
              [Month, Day, Year, Hour, Min, Sec]).
Today's Date is  4/26/0034 23:34:18

In recipe TimeToday we discussed how to convert a date structure to a string. Convert in the opposite direction is also possible, and useful. Unfortunately, there is not a lot of built-in Erlang plumbing to do so. However, if we are very sure of the format we can easily extract the data we need using Erlang's ever-helpful io and io_lib modules:

5> Some_Date_String = "2004-04-26T18:26:18-0500".
6> {ok, [YYY,MMM,DD,HH,MM,SS,ZZ],_} = 
6> io_lib:fread("~4d-~2d-~2dT~2d:~2d:~2d-~4d", Some_Date_String).

Symmetric conversion

Author:Thomas Arts

The above conversion writes directly to the console, but sometimes, one would like an approach in which functions return the converted string and given a string, seconds are returned:

epoch_to_mdyhms(Seconds) -> DateString
mdyhms_to_epoch(DateString) -> Seconds

When creating such a solution, symmetry is an important property to strive after. Intuitively, one expects the following property to hold for conversions

prop_epoch_mdyhms_identity() ->
            mdyhms_to_epoch(epoch_to_mdyhms(Seconds)) == Seconds).

seconds() ->

However, that property clearly is violated in the code above, which we can observe by manually looking at the output produced.

A symmetric and testable solution is given by:

epoch_to_mdyhms(Seconds) -> 
    {{Year, Month, Day}, {Hour, Min, Sec}} =
	io_lib:fwrite("~2B/~2B/~4..0B ~2B:~2.10.0B:~2.10.0B",
                      [Month, Day, Year, Hour, Min, Sec])).

mdyhms_to_epoch(DateString) ->
	{ok,[Month, Day, Year, Hour, Min, Sec],_} = 
	   io_lib:fread("~d/~d/~d ~d:~d:~d", DateString),