Dates and Time
From Erlang Community
| Revision as of 01:38, 26 August 2006 (edit) Cyberlync (Talk | contribs) ← Previous diff |
Current revision (09:43, 25 September 2006) (edit) (undo) Ayrnieu (Talk | contribs) (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 ?) |
||
| (One intermediate revision not shown.) | |||
| Line 1: | Line 1: | ||
| == Introduction == | == Introduction == | ||
| - | Erlang provides basic, but important ways of manipulating time and dates. erlang:localtime | + | 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, | + | 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> | ||
| - | { | + | 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 | + | 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. | |
| - | 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> | ||
| - | + | 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}} | |
| </code> | </code> | ||
| - | [[Category:CookBook]] | + | 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> | ||
| + | |||
| + | [[Category:CookBook]][[Category:DateTimeRecipes]] | ||
Current revision
[edit] 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}]
|

Digg It
Del.icio.us
Reddit
Facebook
Stumble Upon
Technorati

