e r l a n g : c o o k b o o k

/ Erlang.TimeISO8601

This Web


WebHome 
WebChanges 
TOC
NewRecipe 
WebTopicList 
WebStatistics 

All Webs


Chicken
Cookbook
Erlang
Know
Main
Plugins
Sandbox
Scm
TWiki  

Erlang Links


Erlang.org
Erlang Wiki
ErlMan
Erlang Wiki
The Jungerl
Erlang-fr.org
Joe Armstrong
Lambda the Ultimate

Erlang Web Ring


[Prev]: Joe Armstrong's Page
[Next]: Joe Armstrong's Page

Converting Between struct:time and ISO-8601 Format

Problem

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

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 TimeRFC1123 Recipe for details:

Discussion

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.

References

Contributors

Based on work by GordonWeakliem.

-- BrentAFulgham - 28 Aug 2004

CookbookForm
TopicType: Recipe
ParentTopic: TimeRecipes
TopicOrder: 060

 
 
Copyright © 2004 by the contributing authors. All material on the Erlang Cookbook web site is the property of the contributing authors.
This material can be redistributed and/or modified under the terms of the GNU Lesser General Public License (LGPL), version 2.1, as published by the Free Software Foundation.
Ideas, requests, problems regarding Schematics Cookbook? Send feedback.
/ You are Main.guest