Converting Between struct:time and ISO8601 Format

From Erlang Community

(Difference between revisions)
Revision as of 17:44, 21 November 2006 (edit)
Kaiserpanda (Talk | contribs)
m
← Previous diff
Current revision (06:38, 9 March 2007) (edit) (undo)
Bfulgham (Talk | contribs)
(Solution)
 
(2 intermediate revisions not shown.)
Line 19: Line 19:
</code> </code>
-However, if you're writing a web application, you may need to observe the W3C profile for time formats, which is slightly different. See the TimeRFC1123 Recipe for details:+However, if you're writing a web application, you may need to observe the W3C profile for time formats, which is slightly different. See the [[RFC1123_Dates_and_Times]] Recipe for details.
The nice thing about the W3C profile of the ISO 8601 format is that it's very easy to sort dates using a simple string compare. The nice thing about the W3C profile of the ISO 8601 format is that it's very easy to sort dates using a simple string compare.
[[Category:CookBook]][[Category:DateTimeRecipes]] [[Category:CookBook]][[Category:DateTimeRecipes]]
- 
- 
- 
-[http://www.magical-casino.com/online_games.html Online casino games.] 
-[http://www.casino-games-wiki.com/index.php/play_casino_games play casino games] 
-[http://www.casino-theory.com/online-casino-royale/strategy-online-casino.html strategy online casino] 
-[http://www.magical-casino.com/choosing_casino.html Online Casino - Choosing the best casinos.] 
-[http://www.casino-theory.com/online-casino-royale/online-casino-gamble.html online casino gamble] 
-[http://www.slots-wiki.com/index.php/slots_tips slots tips] 
-[http://www.casinos-go.com/online-casino-tips/online-casino-games.html online casino games] 
-[http://www.bestweb-online-casinos.com/slots-game/diamond-club-slots.html diamond club slots] 
-[http://www.casino-theory.com/online-casino-bonus/online-casino-net.html online casino net] 
-[http://www.gambling-online-theory.com/casinos-portal/best-casinos-online.html best casinos online] 

Current revision

[edit] Problem

You have an application that requires you to use ISO-8601 Format for input and output.

[edit] Solution

Unfortunately, no Erlang libraries provide this functionality. Luckily, the native Erlang date and time formats are very easy to format for display or transmission, even in ISO-8061 format:

iso_8601_fmt(DateTime) ->
    {{Year,Month,Day},{Hour,Min,Sec}} = DateTime,
    io_lib:format("~4.10.0B-~2.10.0B-~2.10.0B ~2.10.0B:~2.10.0B:~2.10.0B",
        [Year, Month, Day, Hour, Min, Sec]).

1> CurrDateTime = erlang:localtime().
{{2004,8,28},{1,19,37}}
2> {{Year,Month,Day},{Hour,Min,Sec}} = CurrDateTime.
{{2004,8,28},{1,19,37}}
3> io:fwrite("~s\n",[iso_8601_fmt(erlang:localtime())]).
2004-08-28 01:48:48

However, if you're writing a web application, you may need to observe the W3C profile for time formats, which is slightly different. See the RFC1123_Dates_and_Times Recipe for details.

The nice thing about the W3C profile of the ISO 8601 format is that it's very easy to sort dates using a simple string compare.

Erlang/OTP Projects
Personal tools