diff --git a/README.markdown b/README.markdown index 8928871..1a16396 100644 --- a/README.markdown +++ b/README.markdown @@ -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 omitted, will be determined as described below in "Understanding Timezone 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. #### 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 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 -`to_date`. `Disambiguation` can be an atom of the values: +You can do this by passing a `Disambiguation` argument to `to_string`, +`to_date`, `to_unixtime`, and `to_now`. `Disambiguation` can be an atom of the +values: + `prefer_standard` *(Default Behavior)*: If an ambiguous result occurs, 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 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_string(FormatString, ToTimezone, Disambiguate, Date)` + + `to_unixtime(Disambiguate, Date)` + + `to_now(Disambiguate, Date)` Examples: diff --git a/src/qdate.erl b/src/qdate.erl index 8fd4104..4a38aa1 100644 --- a/src/qdate.erl +++ b/src/qdate.erl @@ -18,7 +18,9 @@ to_date/2, to_date/3, to_now/1, + to_now/2, to_unixtime/1, + to_unixtime/2, unixtime/0 ]). @@ -250,24 +252,41 @@ get_deterministic_datetime() -> {ok, Val} -> throw({invalid_env_var, {qdate, deterministic_parsing, Val}}) end. +to_unixtime(Date) -> + to_unixtime(?DEFAULT_DISAMBIG, Date). -to_unixtime(Unixtime) when is_integer(Unixtime) -> +to_unixtime(_, Unixtime) when is_integer(Unixtime) -> Unixtime; -to_unixtime({MegaSecs,Secs,_}) -> +to_unixtime(_, {MegaSecs,Secs,_}) -> MegaSecs*1000000 + Secs; -to_unixtime(ToParse) -> +to_unixtime(Disamb, ToParse) -> %% We want to treat all unixtimes as GMT - Date = to_date("GMT", ToParse), - calendar:datetime_to_gregorian_seconds(Date) - ?UNIXTIME_BASE. + case to_date("GMT", Disamb, ToParse) of + {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() -> to_unixtime(os:timestamp()). -to_now(Now = {_,_,_}) -> +to_now(Date) -> + to_now(?DEFAULT_DISAMBIG, Date). + +to_now(_, Now = {_,_,_}) -> Now; -to_now(ToParse) -> - Unixtime = to_unixtime(ToParse), - unixtime_to_now(Unixtime). +to_now(Disamb, ToParse) -> + case to_unixtime(Disamb, ToParse) of + {ambiguous, Standard, Daylight} -> + {ambiguous, + unixtime_to_now(Standard), + unixtime_to_now(Daylight)}; + Unixtime -> + unixtime_to_now(Unixtime) + end. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%