Re-implemented PR #8
This commit is contained in:
parent
458cedf2a2
commit
cea3d79936
2 changed files with 24 additions and 5 deletions
|
@ -17,6 +17,9 @@
|
||||||
,tz_name/2
|
,tz_name/2
|
||||||
,tz_shift/2
|
,tz_shift/2
|
||||||
,tz_shift/3
|
,tz_shift/3
|
||||||
|
,get_timezone/1
|
||||||
|
,list_timezones/0
|
||||||
|
,adjust_datetime/2
|
||||||
]).
|
]).
|
||||||
|
|
||||||
% utc_to_local(UtcDateTime, Timezone) -> LocalDateTime | [LocalDateTime, DstLocalDateTime] | {error, ErrDescr}
|
% utc_to_local(UtcDateTime, Timezone) -> LocalDateTime | [LocalDateTime, DstLocalDateTime] | {error, ErrDescr}
|
||||||
|
@ -185,14 +188,20 @@ tz_shift(LocalDateTime, TimezoneFrom, TimezoneTo) ->
|
||||||
Err
|
Err
|
||||||
end.
|
end.
|
||||||
|
|
||||||
% =======================================================================
|
|
||||||
% privates
|
|
||||||
% =======================================================================
|
|
||||||
|
|
||||||
adjust_datetime(DateTime, Minutes) ->
|
adjust_datetime(DateTime, Minutes) ->
|
||||||
Seconds = calendar:datetime_to_gregorian_seconds(DateTime) + Minutes * 60,
|
Seconds = calendar:datetime_to_gregorian_seconds(DateTime) + Minutes * 60,
|
||||||
calendar:gregorian_seconds_to_datetime(Seconds).
|
calendar:gregorian_seconds_to_datetime(Seconds).
|
||||||
|
|
||||||
|
get_timezone(TimeZone) ->
|
||||||
|
get_timezone_inner(TimeZone).
|
||||||
|
|
||||||
|
list_timezones() ->
|
||||||
|
dict:fetch_keys(?tz_index).
|
||||||
|
|
||||||
|
% =======================================================================
|
||||||
|
% privates
|
||||||
|
% =======================================================================
|
||||||
|
|
||||||
invert_shift(Minutes) ->
|
invert_shift(Minutes) ->
|
||||||
-Minutes.
|
-Minutes.
|
||||||
|
|
||||||
|
@ -222,7 +231,7 @@ tr_char([H|T], From, To, Acc) ->
|
||||||
end.
|
end.
|
||||||
|
|
||||||
-define(SPACE_CHAR, 32).
|
-define(SPACE_CHAR, 32).
|
||||||
get_timezone(TimeZone) ->
|
get_timezone_inner(TimeZone) ->
|
||||||
TimeZoneNoSpaces = tr_char(TimeZone, ?SPACE_CHAR, $_),
|
TimeZoneNoSpaces = tr_char(TimeZone, ?SPACE_CHAR, $_),
|
||||||
case dict:find(TimeZoneNoSpaces, ?tz_index) of
|
case dict:find(TimeZoneNoSpaces, ?tz_index) of
|
||||||
error ->
|
error ->
|
||||||
|
|
|
@ -5,6 +5,9 @@
|
||||||
|
|
||||||
-author("Dmitry Melnikov <dmitryme@gmail.com>").
|
-author("Dmitry Melnikov <dmitryme@gmail.com>").
|
||||||
|
|
||||||
|
-include("tz_database.hrl").
|
||||||
|
-include("tz_index.hrl").
|
||||||
|
|
||||||
-export(
|
-export(
|
||||||
[
|
[
|
||||||
check/2
|
check/2
|
||||||
|
@ -16,6 +19,13 @@
|
||||||
% check(DateTime, TimeZone) -> is_in_dst | is_not_in_dst | ambiguous_time | time_not_exists
|
% check(DateTime, TimeZone) -> is_in_dst | is_not_in_dst | ambiguous_time | time_not_exists
|
||||||
% DateTime = DateTime()
|
% DateTime = DateTime()
|
||||||
% TimeZone = tuple()
|
% TimeZone = tuple()
|
||||||
|
check(DateTime, Timezone) when is_list(Timezone) ->
|
||||||
|
case lists:keyfind(localtime:get_timezone(Timezone), 1, ?tz_database) of
|
||||||
|
false ->
|
||||||
|
{error, unknown_tz};
|
||||||
|
TZ ->
|
||||||
|
check(DateTime, TZ)
|
||||||
|
end;
|
||||||
check({Date = {Year, _, _},Time}, {_, _, _, _Shift, DstShift, DstStartRule, DstStartTime, DstEndRule, DstEndTime}) ->
|
check({Date = {Year, _, _},Time}, {_, _, _, _Shift, DstShift, DstStartRule, DstStartTime, DstEndRule, DstEndTime}) ->
|
||||||
DstStartDay = get_dst_day_of_year(DstStartRule, Year),
|
DstStartDay = get_dst_day_of_year(DstStartRule, Year),
|
||||||
DstEndDay = get_dst_day_of_year(DstEndRule, Year),
|
DstEndDay = get_dst_day_of_year(DstEndRule, Year),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue