[erlang-questions] Working with dates.

Per Hedeland per@REDACTED
Mon Jul 16 11:32:19 CEST 2007


Michael McDaniel <erlangx@REDACTED> wrote:
>
>On Sun, Jul 15, 2007 at 06:44:50PM -0700, Michael McDaniel wrote:
>> On Sun, Jul 15, 2007 at 06:40:45PM -0700, Michael McDaniel wrote:
>> > 
>> > On Sun, Jul 15, 2007 at 06:39:18PM -0500, Julio César Carrascal Urquijo (MCTS) wrote:
>> > > Hi,
>> > > 
>> > > Two quick questions on functionality that I couldn't find on the documentation:
>> > > 
>> > > 1) Are there functions in OTP to convert unix timestamp <--> the tuple
>> > > format used in Erlang?
>> > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>> > 
>> >  (del@REDACTED)174> Seconds = os:cmd("../c/time").
>> >  "1184549721"
>> >  (del@REDACTED)175> {Days, Time} = calendar:seconds_to_daystime( list_to_integer(Seconds) ).
>> >  {13710,{1,35,21}}
>> >  (del@REDACTED)176> Date = calendar:gregorian_days_to_date( Days ).
>> >  {37,7,15}
>> ^^^^^^^^^^^^^^
>> ^^^^^^^^^^^^^^
>>   oops, 37 ? something's not right, obviously...
>> ~M
>^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>
>  Ok, because Unix time starts @1970 and Gregorian starts, apparently, at 0.
>  Adding 1970+37 = 2007 but I wonder if there would be an "off by 1" somewhere
>  down the road...

Assuming that Julio meant what you think with "unix timestamp", i.e. the
thing returned by the time() function, the way to go would be to first
convert it to erlang:now() form:

1> T = 1184549721.
1184549721
2> Now = {T div 1000000, T rem 1000000, 0}.
{1184,549721,0}

And depending on what Julio meant with "the tuple format used in Erlang",
that may be the answer.:-) Otherwise go on to use either

3> calendar:now_to_universal_time(Now).
{{2007,7,16},{1,35,21}}

or

4> calendar:now_to_local_time(Now).    
{{2007,7,16},{3,35,21}}

- and then you can use whatever else you need from the calendar module.

--Per Hedeland




More information about the erlang-questions mailing list