[erlang-questions] Unix Epoch

70 views
Skip to first unread message

Henning Diedrich

unread,
May 2, 2010, 9:33:41 AM5/2/10
to Erlang Questions
Hi,

is there a better way to turn datetime into unix epoch seconds?

unix_time({Date,Time}) ->

UnixEpoch = {{1970,1,1},{0,0,0}},
calendar:datetime_to_gregorian_seconds({Date,Time}) -
calendar:datetime_to_gregorian_seconds(UnixEpoch).

Gregorian seconds down to year 0 might be a costly calculation I though.

Thanks!
Henning

--
You received this message because you are subscribed to the Google Groups "Erlang Programming" group.
To post to this group, send email to erlang-pr...@googlegroups.com.
To unsubscribe from this group, send email to erlang-programm...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/erlang-programming?hl=en.

Roberto Ostinelli

unread,
May 2, 2010, 9:38:05 AM5/2/10
to Henning Diedrich, Erlang Questions
i would suggest:

unix_time() ->
{M, S, _} = now(),
M*1000000 + S.

r.

2010/5/2 Henning Diedrich <hd2...@eonblast.com>:
> Hi,
>
> is there a better way to turn datetime into unix epoch seconds?
>
> unix_time({Date,Time}) ->
>
>   UnixEpoch = {{1970,1,1},{0,0,0}},
>   calendar:datetime_to_gregorian_seconds({Date,Time}) -
> calendar:datetime_to_gregorian_seconds(UnixEpoch).
>
> Gregorian seconds down to year 0 might be a costly calculation I though.
>
> Thanks!
> Henning
>



--
-------------------------------------------------------------------
Roberto Ostinelli
CTO, WideTag Inc. - Realtime, Social, Green
widetag.com
skype: rostinelli
twitter: ostinelli
mobile: +39 335 6 100 22 6

________________________________________________________________
erlang-questions (at) erlang.org mailing list.
See http://www.erlang.org/faq.html
To unsubscribe; mailto:erlang-questio...@erlang.org

Roberto Ostinelli

unread,
May 2, 2010, 9:41:26 AM5/2/10
to Henning Diedrich, Erlang Questions
sorry, my bad. too fast in responding, must be my 38° fever. ignore my
previous message.

r.

2010/5/2 Roberto Ostinelli <rob...@widetag.com>:
> i would suggest:
>
> unix_time() ->
>        {M, S, _} = now(),
>        M*1000000 + S.
>
> r.

Rapsey

unread,
May 2, 2010, 9:45:53 AM5/2/10
to erlang-q...@erlang.org
Epoch seconds from 0 is a constant. So no need to calculate it every time.


Sergej

Henning Diedrich

unread,
May 2, 2010, 11:22:55 AM5/2/10
to Erlang Questions
That's what I would hope for, this leaves

datetime_to_gregorian_seconds({Date,Time})

which is also, and needlessly, calculated back to year 0.

Henning Diedrich

unread,
May 2, 2010, 5:20:46 PM5/2/10
to Erlang Questions
Wow, thanks, is this Erlang compiler/VM source?

Tony Finch wrote:
> On Sun, 2 May 2010, Henning Diedrich wrote:
>
>> Gregorian seconds down to year 0 might be a costly calculation I though.
>>
>
> No more expensive than calculating from 1970. The only difference is the
> offset constant. The following code follows the conventions in the book
> "Calendrical Calculations" though it is somewhat optimised.
>
> int gregorian_date_to_day_number(int y, int m, int d) {
> // Move Jan and Feb to previous year so leap days fall at the end
> // and number months Mar=4 - Feb=15 so the 5 month cycle fits.
> if (m > 2) m += 1;
> else m += 13, y -= 1;
> return
> // Days in this month.
> + d
> // Days in this year before the current month, using repeating
> // cycle of 5 months Mar - Jul, Aug - Dec, Jan - (truncated).
> + m*153/5
> // Days in previous years according to 4-year Julian cycle.
> + y*1461/4
> // Gregorian correction.
> - y/100 + y/400
> // Epoch offset so that Mon 0001-01-01 is R.D. 1.
> - 428;
> // ISO 8601 numbers days of the week Mon=1 - Sun=7
> // Wed 1858-11-17 is R.D. 678576 and MJD 0
> // Thu 1970-01-01 is R.D. 719163
> }
>
> int gregorian_time_to_unix_time(int y, int m, int d, int H, int M, int S) {
> return 86400 * (gregorian_date_to_day_number(y, m, d) - 719163)
> + H * 1440 + M * 60 + S;
> }
>
> Tony.
Reply all
Reply to author
Forward
0 new messages