tz_shift added
This commit is contained in:
parent
82cc45dc5a
commit
4f3e906ad3
3 changed files with 435 additions and 405 deletions
|
@ -25,6 +25,7 @@
|
|||
,local_to_utc/2
|
||||
,local_to_local/3
|
||||
,tz_name/2
|
||||
,tz_shift/2
|
||||
]).
|
||||
|
||||
% utc_to_local(UtcDateTime, Timezone) -> LocalDateTime | {error, ErrDescr}
|
||||
|
@ -121,14 +122,43 @@ tz_name(LocalDateTime, Timezone) ->
|
|||
end
|
||||
end.
|
||||
|
||||
adjust_datetime(DateTime, {Hours, Minutes}) ->
|
||||
Seconds = calendar:datetime_to_gregorian_seconds(DateTime) + Hours * 3600,
|
||||
case Hours < 0 of
|
||||
true ->
|
||||
calendar:gregorian_seconds_to_datetime(Seconds - Minutes * 60);
|
||||
% tz_shift(LocalDateTime, Timezone) -> Shift | {Shift, DstSift} | unable_to_detect
|
||||
% returns time shift from GMT
|
||||
% LocalDateTime = DateTime()
|
||||
% Timezone = String()
|
||||
% Shift = DstShift = {Sign, Hours, Minutes}
|
||||
% Sign = term(), '+', '-'
|
||||
% Hours = Minutes = Integer(),
|
||||
% {Shift, DstShift} - returns, when shift is ambiguous
|
||||
tz_shift(_UtcDateTime, "UTC") ->
|
||||
0;
|
||||
tz_shift(LocalDateTime, Timezone) ->
|
||||
case lists:keyfind(Timezone, 1, ?tz_database) of
|
||||
false ->
|
||||
calendar:gregorian_seconds_to_datetime(Seconds + Minutes * 60)
|
||||
{error, unknown_tz};
|
||||
{_Tz, _StdName, undef, Shift, _DstShift, undef, _DstStartTime, undef, _DstEndTime} ->
|
||||
Shift;
|
||||
TzRule = {_, _StdName, _DstName, Shift, DstShift, _, _, _, _} ->
|
||||
case localtime_dst:check(LocalDateTime, TzRule) of
|
||||
is_in_dst ->
|
||||
fmt_min(Shift + DstShift);
|
||||
is_not_in_dst ->
|
||||
fmt_min(Shift);
|
||||
ambiguous_time ->
|
||||
{fmt_min(Shift), fmt_min(Shift + DstShift)};
|
||||
time_not_exists ->
|
||||
unable_to_detect
|
||||
end
|
||||
end.
|
||||
|
||||
invert_shift({Hours, Minutes}) ->
|
||||
{-Hours, Minutes}.
|
||||
adjust_datetime(DateTime, Minutes) ->
|
||||
Seconds = calendar:datetime_to_gregorian_seconds(DateTime) + Minutes * 60,
|
||||
calendar:gregorian_seconds_to_datetime(Seconds).
|
||||
|
||||
invert_shift(Minutes) ->
|
||||
-Minutes.
|
||||
|
||||
fmt_min(Shift) when Shift < 0 ->
|
||||
{'-', abs(Shift) div 60, abs(Shift) rem 60};
|
||||
fmt_min(Shift) ->
|
||||
{'+', Shift div 60, Shift rem 60}.
|
||||
|
|
|
@ -35,9 +35,9 @@ check({Date = {Year, _, _},Time}, {_, _, _, _Shift, DstShift, DstStartRule, DstS
|
|||
CurrDay = get_day_of_year(Date),
|
||||
case is_dst_date(DstStartDay, DstEndDay, CurrDay) of
|
||||
equal_to_start ->
|
||||
is_dst_start_time(time_to_minutes(Time), time_to_minutes(DstStartTime), time_to_minutes(DstShift));
|
||||
is_dst_start_time(time_to_minutes(Time), time_to_minutes(DstStartTime), DstShift);
|
||||
equal_to_end ->
|
||||
is_dst_end_time(time_to_minutes(Time), time_to_minutes(DstEndTime), time_to_minutes(DstShift));
|
||||
is_dst_end_time(time_to_minutes(Time), time_to_minutes(DstEndTime), DstShift);
|
||||
Res ->
|
||||
Res
|
||||
end.
|
||||
|
@ -131,8 +131,6 @@ day_to_int(fri) -> 5;
|
|||
day_to_int(sat) -> 6;
|
||||
day_to_int(sun) -> 7.
|
||||
|
||||
time_to_minutes({Hours, Minutes}) ->
|
||||
Hours * 60 + Minutes;
|
||||
time_to_minutes({Hours, Minutes, _Seconds}) ->
|
||||
Hours * 60 + Minutes.
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue