diff --git a/README.markdown b/README.markdown index f249ce8..6749887 100644 --- a/README.markdown +++ b/README.markdown @@ -98,7 +98,9 @@ will infer the timezone in the following order. `set_timezone/1` only applies to that *specific* process. If none is specified. + If no timezone is specified for the process, `qdate` looks at the `qdate` - application variable `default_timezone`. + application variable `default_timezone`. `default_timezone` can be either a + hard-specified timezone, or a `{Module, Function}` tuple. The tuple format + should return either a timezone or the atom `undefined`. + If no timezone is specified by either of the above, `qdate` assumes "GMT" for all dates. + A timezone value of `auto` will act as if no timezone is specified. diff --git a/qdate.config b/qdate.config index 5daf2a7..877e139 100644 --- a/qdate.config +++ b/qdate.config @@ -1,5 +1,12 @@ %% vim: ts=4 sw=4 et ft=erlang [{qdate, [ + %% default_timezone can be one of two things: + %% 1) An actual timezone. Either short-form like "GMT", "UTC", or a + %% longer-form (but more likely to pick the correct daylight saving + %% config timezone like "America/Chicago" + %% 2) A 2-tuple of {Module, Function}, which will be called as + %% Module:Function() to determine the timezone (say you wanted to + %% determine timezone based on some kind of environmental conditions) {default_timezone, "GMT"}, %% See readme section here: diff --git a/src/qdate.erl b/src/qdate.erl index 610febf..0366a69 100644 --- a/src/qdate.erl +++ b/src/qdate.erl @@ -125,16 +125,6 @@ %% 1970-01-01 12:00am -define(UNIXTIME_BASE,62167219200). -%% This is the timezone only if the qdate application variable -%% "default_timezone" isn't set or is set to undefined. -%% It's recommended that your app sets the var in a config, or at least using -%% -%% application:set_env(qdate, default_timezone, "GMT"). -%% --define(DEFAULT_TZ, case application:get_env(qdate, default_timezone) of - undefined -> "GMT"; - {ok, TZ} -> TZ - end). -define(DETERMINE_TZ, determine_timezone()). -define(DEFAULT_DISAMBIG, prefer_standard). @@ -825,9 +815,23 @@ extract_timezone_helper(RevDate, [TZ | TZs]) when length(RevDate) >= length(TZ) extract_timezone_helper(RevDate, [_TZ | TZs]) -> extract_timezone_helper(RevDate, TZs). + +%% This is the timezone only if the qdate application variable +%% "default_timezone" isn't set or is set to undefined. +%% It's recommended that your app sets the var in a config, or at least using +%% +%% application:set_env(qdate, default_timezone, "GMT"). +%% +default_timezone() -> + case application:get_env(qdate, default_timezone) of + undefined -> "GMT"; + {ok, {Mod, Fun}} -> Mod:Fun(); + {ok, TZ} -> TZ + end. + determine_timezone() -> case qdate_srv:get_timezone() of - undefined -> ?DEFAULT_TZ; + undefined -> default_timezone(); TZ -> TZ end.