Fix unixtime, simplify now -> unixtime conversion

This commit is contained in:
Jesse Gumm 2013-01-15 17:33:30 -06:00
parent ab06e649b1
commit fd448b20e0

View file

@ -5,9 +5,23 @@
to_string/2,
to_date/1,
to_now/1,
to_unixtime/1
to_unixtime/1,
unixtime/0
]).
%% -export([
%% register_parser/2,
%% register_parser/1
%% ]).
%%
%% -export([
%% set_timezone/1,
%% set_timezone/2,
%% get_timezone/0,
%% get_timezone/1
%% ]).
%% Exported for API compatibility with ec_date
-export([
format/1,format/2,
@ -15,6 +29,10 @@
parse/1
]).
%% This the value in gregorian seconds for jan 1st 1970, 12am
%% It's used to convert to and from unixtime, since unixtime starts
%% 1970-01-01 12:00am
-define(UNIXTIME_BASE, 62176219200).
to_string(Format) ->
to_string(Format, now()).
@ -46,10 +64,14 @@ to_date(Date = {{_,_,_},{_,_,_}}) ->
to_unixtime(Unixtime) when is_integer(Unixtime) ->
Unixtime;
to_unixtime({MegaSecs,Secs,_}) ->
MegaSecs*1000000 + Secs;
to_unixtime(ToParse) ->
Date = to_date(ToParse),
calendar:datetime_to_gregorian_seconds(Date).
calendar:datetime_to_gregorian_seconds(Date) - ?UNIXTIME_BASE.
unixtime() ->
to_unixtime(now()).
to_now(Now = {_,_,_}) ->
Now;