Erlang Central

Dates and Time

Revision as of 00:13, 4 September 2006 by Bfulgham (Talk | contribs)

Introduction

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.

Erlang also provides a variety of helpful date-specific functions. For example, the built-in-function date provides the current date. So, for instance printing the current year is as simple as:

1> {Year,_,_} = erlang:date().
{2004,8,27}
2> Year.
2004