[erlang-questions] Unix Epoch

Henning Diedrich hd2010@REDACTED
Sun May 2 23:20:46 CEST 2010


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.
>   


More information about the erlang-questions mailing list