Add disambiguation to to_now and to_unixtime
This commit is contained in:
parent
7384819edb
commit
4a98e54d9d
2 changed files with 36 additions and 13 deletions
|
@ -72,7 +72,8 @@ timezone. For example, `to_date(Date, auto)` is exactly the same as
|
||||||
**A Note About Argument Order**: In all cases, `ToTimezone` is optional and if
|
**A Note About Argument Order**: In all cases, `ToTimezone` is optional and if
|
||||||
omitted, will be determined as described below in "Understanding Timezone
|
omitted, will be determined as described below in "Understanding Timezone
|
||||||
Determining and Conversion". If `ToTimezone` is specified, it will always be
|
Determining and Conversion". If `ToTimezone` is specified, it will always be
|
||||||
immediately left of the `Date` argument. `Date` will always be the last
|
immediately left of the `Disambiguate` argument (if it's specified), which is
|
||||||
|
always immediately left of `Date` argument. `Date` will always be the last
|
||||||
argument to any of the conversion and formatting functions.
|
argument to any of the conversion and formatting functions.
|
||||||
|
|
||||||
#### Understanding Timezone Determining and Conversions
|
#### Understanding Timezone Determining and Conversions
|
||||||
|
@ -120,8 +121,9 @@ provide an option to disambiguate if you so desire. By default disambiguation
|
||||||
is disabled, and `qdate` will just guess as to it's best choice. But if you so
|
is disabled, and `qdate` will just guess as to it's best choice. But if you so
|
||||||
desire, you can make sure qdate does *both* conversions, and returns both.
|
desire, you can make sure qdate does *both* conversions, and returns both.
|
||||||
|
|
||||||
You can do this by passing a `Disambiguation` argument to `to_string` or
|
You can do this by passing a `Disambiguation` argument to `to_string`,
|
||||||
`to_date`. `Disambiguation` can be an atom of the values:
|
`to_date`, `to_unixtime`, and `to_now`. `Disambiguation` can be an atom of the
|
||||||
|
values:
|
||||||
|
|
||||||
+ `prefer_standard` *(Default Behavior)*: If an ambiguous result occurs,
|
+ `prefer_standard` *(Default Behavior)*: If an ambiguous result occurs,
|
||||||
qdate will return the date in standard time rather than daylight time.
|
qdate will return the date in standard time rather than daylight time.
|
||||||
|
@ -131,10 +133,12 @@ You can do this by passing a `Disambiguation` argument to `to_string` or
|
||||||
`{ambiguous, DateStandard, DateDaylight}`, where `DateStandard` is the date
|
`{ambiguous, DateStandard, DateDaylight}`, where `DateStandard` is the date
|
||||||
in Standard Time, and `DateDaylight` is the date in Daylight Saving Time.
|
in Standard Time, and `DateDaylight` is the date in Daylight Saving Time.
|
||||||
|
|
||||||
So the two more helper functions are:
|
So the expanded conversions functions are:
|
||||||
|
|
||||||
+ `to_date(ToTimezone, Disambiguate, Date)`
|
+ `to_date(ToTimezone, Disambiguate, Date)`
|
||||||
+ `to_string(FormatString, ToTimezone, Disambiguate, Date)`
|
+ `to_string(FormatString, ToTimezone, Disambiguate, Date)`
|
||||||
|
+ `to_unixtime(Disambiguate, Date)`
|
||||||
|
+ `to_now(Disambiguate, Date)`
|
||||||
|
|
||||||
Examples:
|
Examples:
|
||||||
|
|
||||||
|
|
|
@ -18,7 +18,9 @@
|
||||||
to_date/2,
|
to_date/2,
|
||||||
to_date/3,
|
to_date/3,
|
||||||
to_now/1,
|
to_now/1,
|
||||||
|
to_now/2,
|
||||||
to_unixtime/1,
|
to_unixtime/1,
|
||||||
|
to_unixtime/2,
|
||||||
unixtime/0
|
unixtime/0
|
||||||
]).
|
]).
|
||||||
|
|
||||||
|
@ -250,24 +252,41 @@ get_deterministic_datetime() ->
|
||||||
{ok, Val} -> throw({invalid_env_var, {qdate, deterministic_parsing, Val}})
|
{ok, Val} -> throw({invalid_env_var, {qdate, deterministic_parsing, Val}})
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
to_unixtime(Date) ->
|
||||||
|
to_unixtime(?DEFAULT_DISAMBIG, Date).
|
||||||
|
|
||||||
to_unixtime(Unixtime) when is_integer(Unixtime) ->
|
to_unixtime(_, Unixtime) when is_integer(Unixtime) ->
|
||||||
Unixtime;
|
Unixtime;
|
||||||
to_unixtime({MegaSecs,Secs,_}) ->
|
to_unixtime(_, {MegaSecs,Secs,_}) ->
|
||||||
MegaSecs*1000000 + Secs;
|
MegaSecs*1000000 + Secs;
|
||||||
to_unixtime(ToParse) ->
|
to_unixtime(Disamb, ToParse) ->
|
||||||
%% We want to treat all unixtimes as GMT
|
%% We want to treat all unixtimes as GMT
|
||||||
Date = to_date("GMT", ToParse),
|
case to_date("GMT", Disamb, ToParse) of
|
||||||
calendar:datetime_to_gregorian_seconds(Date) - ?UNIXTIME_BASE.
|
{ambiguous, Standard, Daylight} ->
|
||||||
|
{ambiguous,
|
||||||
|
calendar:datetime_to_gregorian_seconds(Standard) - ?UNIXTIME_BASE,
|
||||||
|
calendar:datetime_to_gregorian_seconds(Daylight) - ?UNIXTIME_BASE};
|
||||||
|
Date ->
|
||||||
|
calendar:datetime_to_gregorian_seconds(Date) - ?UNIXTIME_BASE
|
||||||
|
end.
|
||||||
|
|
||||||
unixtime() ->
|
unixtime() ->
|
||||||
to_unixtime(os:timestamp()).
|
to_unixtime(os:timestamp()).
|
||||||
|
|
||||||
to_now(Now = {_,_,_}) ->
|
to_now(Date) ->
|
||||||
|
to_now(?DEFAULT_DISAMBIG, Date).
|
||||||
|
|
||||||
|
to_now(_, Now = {_,_,_}) ->
|
||||||
Now;
|
Now;
|
||||||
to_now(ToParse) ->
|
to_now(Disamb, ToParse) ->
|
||||||
Unixtime = to_unixtime(ToParse),
|
case to_unixtime(Disamb, ToParse) of
|
||||||
unixtime_to_now(Unixtime).
|
{ambiguous, Standard, Daylight} ->
|
||||||
|
{ambiguous,
|
||||||
|
unixtime_to_now(Standard),
|
||||||
|
unixtime_to_now(Daylight)};
|
||||||
|
Unixtime ->
|
||||||
|
unixtime_to_now(Unixtime)
|
||||||
|
end.
|
||||||
|
|
||||||
|
|
||||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue