Erlang Central

Difference between revisions of "Dates and Time"

From ErlangCentral Wiki

(quite a bit of clean-up, particularly as regards the 'calendar' module; removed reference to calendar:now_to_datetime/1 -- how does it differ from calendar:now_to_local_time/1 ?)
Line 1: Line 1:
 
== Introduction ==
 
== 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:  
+
Erlang provides basic, but important ways of manipulating time and dates. erlang:localtime/0 returns the current time as a tuple of tuples:  
 
<code>
 
<code>
{{Year,Month,Day},{Hour,Min,Seconds}}.
+
1> {Date={Year,Month,Day},Time={Hour,Minutes,Seconds}} = erlang:localtime().
 +
{{2006,9,25},{4,34,29}}
 +
2> b().
 +
Date = {2006,9,25}
 +
Day = 25
 +
Hour = 4
 +
Minutes = 34
 +
Seconds = 29
 +
Time = {4,35,50}
 +
Year = 2006
 
</code>
 
</code>
  
The erlang:now built-in function returns the current time as a tuple:  
+
The erlang:now/0 built-in function returns the current time as a tuple:  
 
<code>
 
<code>
{MegaSecs,Secs,Microsecs}.
+
3> Now={Megaseconds,Seconds,Microseconds} = erlang:now().
 +
{1159,177044,563601}
 
</code>
 
</code>
  
 
These tuples are based on a platform-specific starting date, or epoch.  
 
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.  
+
Erlang also provides support for Gregorian date and time manipulation by way of the 'calendar' module. 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.  
+
Since people are not used to working with seconds (or MegaSeconds, for that matter), Erlang provides several convenience functions to convert erlang:now/0 times to date-time values, for example: calendar:now_to_local_time/1, which returns the local date and time converted from the return value from erlang:now/0; and calendar:now_to_universal_time/1, which returns the same for UTC.
  
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:
 
 
<code>
 
<code>
1> {Year,_,_} = erlang:date().
+
4> calendar:now_to_local_time(Now).
{2004,8,27}
+
{{2006,9,25},{4,37,24}}
2> Year.
+
5> calendar:now_to_universal_time(Now).
2004
+
{{2006,9,25},{5,37,24}}
 +
</code>
 +
 
 +
Erlang also provides erlang:date/0, and erlang:time/0, which return the two respective sub-tuples of erlang:localtime/0.
 +
<code>
 +
6> [erlang:localtime(), erlang:date(), erlang:time()].
 +
[{{2006,9,25},{4,49,0}},
 +
{2006,9,25}
 +
{4,49,0}]
 
</code>
 
</code>
  
 
[[Category:CookBook]][[Category:DateTimeRecipes]]
 
[[Category:CookBook]][[Category:DateTimeRecipes]]

Revision as of 09:43, 25 September 2006

Introduction

Erlang provides basic, but important ways of manipulating time and dates. erlang:localtime/0 returns the current time as a tuple of tuples:

1> {Date={Year,Month,Day},Time={Hour,Minutes,Seconds}} = erlang:localtime().
{{2006,9,25},{4,34,29}}
2> b().
Date = {2006,9,25}
Day = 25
Hour = 4
Minutes = 34
Seconds = 29
Time = {4,35,50}
Year = 2006

The erlang:now/0 built-in function returns the current time as a tuple:

3> Now={Megaseconds,Seconds,Microseconds} = erlang:now().
{1159,177044,563601}

These tuples are based on a platform-specific starting date, or epoch.

Erlang also provides support for Gregorian date and time manipulation by way of the 'calendar' module. 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.

Since people are not used to working with seconds (or MegaSeconds, for that matter), Erlang provides several convenience functions to convert erlang:now/0 times to date-time values, for example: calendar:now_to_local_time/1, which returns the local date and time converted from the return value from erlang:now/0; and calendar:now_to_universal_time/1, which returns the same for UTC.

4> calendar:now_to_local_time(Now).
{{2006,9,25},{4,37,24}}
5> calendar:now_to_universal_time(Now).
{{2006,9,25},{5,37,24}}

Erlang also provides erlang:date/0, and erlang:time/0, which return the two respective sub-tuples of erlang:localtime/0.

6> [erlang:localtime(), erlang:date(), erlang:time()].
[{{2006,9,25},{4,49,0}},
 {2006,9,25}
 {4,49,0}]