New config opt for default_timezone with fun
This commit is contained in:
parent
366bc5e8d5
commit
2dfcc52dd5
3 changed files with 25 additions and 12 deletions
|
@ -98,7 +98,9 @@ will infer the timezone in the following order.
|
||||||
`set_timezone/1` only applies to that *specific* process. If none is
|
`set_timezone/1` only applies to that *specific* process. If none is
|
||||||
specified.
|
specified.
|
||||||
+ If no timezone is specified for the process, `qdate` looks at the `qdate`
|
+ 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"
|
+ If no timezone is specified by either of the above, `qdate` assumes "GMT"
|
||||||
for all dates.
|
for all dates.
|
||||||
+ A timezone value of `auto` will act as if no timezone is specified.
|
+ A timezone value of `auto` will act as if no timezone is specified.
|
||||||
|
|
|
@ -1,5 +1,12 @@
|
||||||
%% vim: ts=4 sw=4 et ft=erlang
|
%% vim: ts=4 sw=4 et ft=erlang
|
||||||
[{qdate, [
|
[{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"},
|
{default_timezone, "GMT"},
|
||||||
|
|
||||||
%% See readme section here:
|
%% See readme section here:
|
||||||
|
|
|
@ -125,16 +125,6 @@
|
||||||
%% 1970-01-01 12:00am
|
%% 1970-01-01 12:00am
|
||||||
-define(UNIXTIME_BASE,62167219200).
|
-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(DETERMINE_TZ, determine_timezone()).
|
||||||
-define(DEFAULT_DISAMBIG, prefer_standard).
|
-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, [_TZ | TZs]) ->
|
||||||
extract_timezone_helper(RevDate, 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() ->
|
determine_timezone() ->
|
||||||
case qdate_srv:get_timezone() of
|
case qdate_srv:get_timezone() of
|
||||||
undefined -> ?DEFAULT_TZ;
|
undefined -> default_timezone();
|
||||||
TZ -> TZ
|
TZ -> TZ
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue