Erlang provides basic, but important ways of manipulating time and
dates.
erlang:localtime() built-in function returns the current
time as a tuple of tuples:
{ {YEAR,MONTH,DAY},{HOUR,MIN,SECONDS} }.
The
erlang:now() built-in function returns the current time as a
tuple:
{MegaSecs,Secs,Microsecs}. These tuples are based on a platform-specific starting date, or
epoch.
Erlang also provides support for date and time manipulation under
the
Gregorian calendar.
The Gregorian calendar in this module is extended back to year 0. For a given date, the
gregorian days is the number of days up to and including the date specified. Similarly, the
gregorian seconds for a given date and time, is the the number of seconds up to and including the specified date and time
The value of
erlang:now() increases as time passes (increasing
by 1 for each second that passes). Since people are not used to
working with seconds (or
MegaSeconds? for that matter), Erlang
provides several convenience functions to convert
now formatted
time to date-time values:
now_to_local_time, which returns the
local date and time converted from the return value from
erlang:now();
now_to_universal_time, which returns UTC time
for an
erlang:now() value; and the
now_to_datetime function,
which returns a
DateTime formatted value.
So, for instance printing the current year is as simple as:
1> {{Year,_,_},_} = calendar:now_to_datetime(erlang:now()).
{{2004,8,27},{23,33,40}}
2> Year.
2004 |
We will discuss these and other
calendar functions in the following recipes.
--
FranciscoSolsona - 28 Apr 2004
--
BrentAFulgham - 27 Aug 2004