Skip to content

Commit

Permalink
New config opt for default_timezone with fun
Browse files Browse the repository at this point in the history
  • Loading branch information
choptastic committed May 31, 2016
1 parent 366bc5e commit 2dfcc52
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
4 changes: 3 additions & 1 deletion README.markdown
Expand Up @@ -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.
Expand Down
7 changes: 7 additions & 0 deletions 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:
Expand Down
26 changes: 15 additions & 11 deletions src/qdate.erl
Expand Up @@ -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).
Expand Down Expand Up @@ -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.

Expand Down

0 comments on commit 2dfcc52

Please sign in to comment.