Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
NickNeck committed Apr 10, 2020
1 parent dc929e8 commit a153742
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 80 deletions.
4 changes: 2 additions & 2 deletions .credo.exs
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@
## Refactoring Opportunities
#
{Credo.Check.Refactor.CondStatements, []},
{Credo.Check.Refactor.CyclomaticComplexity, [max_complexity: 14]},
{Credo.Check.Refactor.FunctionArity, []},
{Credo.Check.Refactor.CyclomaticComplexity, [max_complexity: 10]},
{Credo.Check.Refactor.FunctionArity, [max_arity: 10]},
{Credo.Check.Refactor.LongQuoteBlocks, []},
{Credo.Check.Refactor.MapInto, []},
{Credo.Check.Refactor.MatchInCondition, []},
Expand Down
4 changes: 4 additions & 0 deletions lib/time_zone_info/gregorian_seconds.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
defmodule TimeZoneInfo.GregorianSeconds do
@moduledoc """
An implementation for gregorian seconds.
"""

@type t :: integer()

@utc ~w(utc gmt zulu)a
Expand Down
6 changes: 4 additions & 2 deletions lib/time_zone_info/transformer/rule.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ defmodule TimeZoneInfo.Transformer.Rule do
alias TimeZoneInfo.GregorianSeconds
alias TimeZoneInfo.IanaParser
alias TimeZoneInfo.NaiveDateTimeUtil, as: NaiveDateTime
alias TimeZoneInfo.Transformer.RuleSet

@doc """
Returns a map of rule-set for the given map of `rules`.
"""
# TODO: @spec
@spec to_rule_sets(%{String.t() => [TimeZoneInfo.rule()]}, non_neg_integer()) ::
%{String.t() => RuleSet.t()}
def to_rule_sets(rules, lookahead) do
Enum.into(rules, %{}, fn {name, rules} ->
{name, to_rule_set(rules, lookahead)}
Expand All @@ -20,7 +22,7 @@ defmodule TimeZoneInfo.Transformer.Rule do
@doc """
Returns a rule-set for the given `rules`.
"""
# TODO: @spec
@spec to_rule_set([TimeZoneInfo.rule()], non_neg_integer) :: RuleSet.t()
def to_rule_set(rules, lookahead) do
now = NaiveDateTime.utc_now()

Expand Down
185 changes: 118 additions & 67 deletions lib/time_zone_info/transformer/rule_set.ex
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ defmodule TimeZoneInfo.Transformer.RuleSet do
end

def transitions(
[rule | rule_set],
[rule | _] = rule_set,
since,
zone_state,
last_utc_offset,
Expand All @@ -61,59 +61,123 @@ defmodule TimeZoneInfo.Transformer.RuleSet do
acc
) do
until = ZoneState.until(zone_state, last_std_offset)
{at, {std_offset, letters}} = to_utc(rule, zone_state, last_std_offset)

case position(at, since, until) do
:before ->
transitions(rule_set, since, zone_state, last_utc_offset, last_std_offset, rule, acc)

:start ->
acc = add(acc, at, zone_state, std_offset, letters)
transitions(rule_set, since, zone_state, last_utc_offset, std_offset, rule, acc)

:inside ->
acc =
cond do
start?(since, at, zone_state, last_utc_offset) ->
add(acc, since, zone_state, std_offset, letters)

start?(since, at, std_offset) ->
add(acc, since, zone_state, std_offset, letters)

Enum.empty?(acc) ->
since_wall = to_wall(since, zone_state, last_rule)
at_wall = to_wall(at, zone_state, std_offset)

case since_wall == at_wall do
true ->
add(acc, since, zone_state, std_offset, letters)

false ->
{at, _} = to_utc(rule, zone_state, std_offset(last_rule))

acc
|> add(since, zone_state, last_rule)
|> add(at, zone_state, std_offset, letters)
end

true ->
add(acc, at, zone_state, std_offset, letters)
end

transitions(rule_set, since, zone_state, last_utc_offset, std_offset, rule, acc)

:after ->
case Enum.empty?(acc) do
true ->
acc = add(acc, since, zone_state, last_rule)
std_offset = std_offset(last_rule)
until = ZoneState.until(zone_state, std_offset)
{acc, until, std_offset}

false ->
{acc, until, last_std_offset}
end
end
{at, _} = transition = to_utc(rule, zone_state, last_std_offset)

at
|> position(since, until)
|> transitions(
transition,
rule_set,
since,
zone_state,
last_utc_offset,
last_std_offset,
last_rule,
acc
)
end

defp transitions(
:before,
_transition,
[rule | rule_set],
since,
zone_state,
last_utc_offset,
last_std_offset,
_last_rule,
acc
) do
transitions(rule_set, since, zone_state, last_utc_offset, last_std_offset, rule, acc)
end

defp transitions(
:start,
{at, {std_offset, letters}},
[rule | rule_set],
since,
zone_state,
last_utc_offset,
_last_std_offset,
_last_rule,
acc
) do
acc = add(acc, at, zone_state, std_offset, letters)
transitions(rule_set, since, zone_state, last_utc_offset, std_offset, rule, acc)
end

defp transitions(
:inside,
{at, {std_offset, letters}},
[rule | rule_set],
since,
zone_state,
last_utc_offset,
_last_std_offset,
last_rule,
[]
) do
acc =
case start?(since, at, zone_state, last_utc_offset) do
true ->
add([], since, zone_state, std_offset, letters)

false ->
{at, _} = to_utc(rule, zone_state, std_offset(last_rule))

[]
|> add(since, zone_state, last_rule)
|> add(at, zone_state, std_offset, letters)
end

transitions(rule_set, since, zone_state, last_utc_offset, std_offset, rule, acc)
end

defp transitions(
:inside,
{at, {std_offset, letters}},
[rule | rule_set],
since,
zone_state,
last_utc_offset,
_last_std_offset,
_last_rule,
acc
) do
acc = add(acc, at, zone_state, std_offset, letters)
transitions(rule_set, since, zone_state, last_utc_offset, std_offset, rule, acc)
end

defp transitions(
:after,
_transition,
_rule_set,
since,
zone_state,
_last_utc_offset,
_last_std_offset,
last_rule,
[]
) do
acc = add([], since, zone_state, last_rule)
std_offset = std_offset(last_rule)
until = ZoneState.until(zone_state, std_offset)
{acc, until, std_offset}
end

defp transitions(
:after,
_transition,
_rule_set,
_since,
zone_state,
_last_utc_offset,
last_std_offset,
_last_rule,
acc
) do
until = ZoneState.until(zone_state, last_std_offset)
{acc, until, last_std_offset}
end

@doc """
Expand Down Expand Up @@ -144,10 +208,6 @@ defmodule TimeZoneInfo.Transformer.RuleSet do

defp std_offset({_at, {_, std_offset, _}}), do: std_offset

defp start?(since, at, std_offset) do
since == at + std_offset * -1
end

defp start?(since, at, zone_state, last_utc_offset) do
utc_offset_diff = zone_state[:utc_offset] - last_utc_offset
since == at + utc_offset_diff
Expand All @@ -165,15 +225,6 @@ defmodule TimeZoneInfo.Transformer.RuleSet do
[transition | transitions]
end

defp to_wall(at, zone_state, {_, {_, std_offset, _}}) do
to_wall(at, zone_state, std_offset)
end

defp to_wall(at, zone_state, std_offset) do
utc_offset = zone_state[:utc_offset]
at + utc_offset + std_offset
end

defp to_utc({at, {_time_standard, std_offset, letters}}, _utc_offset, _last_std_offset)
when at < 0 do
{at, {std_offset, letters}}
Expand Down
2 changes: 1 addition & 1 deletion test/test_helper.exs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ config =
mod: TimeZoneInfo.PerlChecker,
info: false,
assert: false,
active: false
active: true
]
]
]
Expand Down
1 change: 0 additions & 1 deletion test/time_zone_info/naive_datetime_util_test.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
defmodule TimeZoneInfo.NaiveDateTimeUtilTest do
use ExUnit.Case, async: true

end
20 changes: 14 additions & 6 deletions test/time_zone_info/time_zone_database_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -676,14 +676,23 @@ defmodule TimeZoneInfo.TimeZoneDatabaseTest do
# ==========================================================================
# Periods in the future will be calculate on the fly.

prove "at the start of an ambiguous time span in the future (last without cal.)",
prove "in the future (last without calculation)",
time_zone_periods_from_wall_datetime(
~N[2039-10-30 02:00:00],
~N[2035-03-25 00:30:00],
"Europe/Berlin"
) == {
:ambiguous,
%{utc_offset: 3600, std_offset: 3600, zone_abbr: "CEST"},
%{utc_offset: 3600, std_offset: 0, zone_abbr: "CET"}
:ok,
%{std_offset: 0, utc_offset: 3600, zone_abbr: "CET"}
}

prove "at the start of a gap in the future (first with calculation)",
time_zone_periods_from_wall_datetime(
~N[2035-03-25 02:30:00],
"Europe/Berlin"
) == {
:gap,
{%{std_offset: 0, utc_offset: 3600, zone_abbr: "CET"}, ~N[2035-03-25 02:00:00]},
{%{std_offset: 3600, utc_offset: 3600, zone_abbr: "CEST"}, ~N[2035-03-25 03:00:00]}
}

prove "before an ambiguous time span in the future",
Expand Down Expand Up @@ -792,7 +801,6 @@ defmodule TimeZoneInfo.TimeZoneDatabaseTest do
"Asia/Amman"
) == {:ok, %{std_offset: 3600, utc_offset: 7200, zone_abbr: "EEST"}}

@tag :only
prove "in the future in an ambiguous time span",
time_zone_periods_from_wall_datetime(
~N[2041-10-25 00:30:00],
Expand Down
1 change: 0 additions & 1 deletion test/time_zone_info/updater_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ defmodule TimeZoneInfo.UpdaterTest do
)
end

@tag :only
test "gets an error if the data is not on the server" do
rm_data(@path)
mkdir_data(@path)
Expand Down

0 comments on commit a153742

Please sign in to comment.