Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make min_year and max_year configurable #141

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,18 @@ This feature can be disabled with the following configuration:
config :tzdata, :autoupdate, :disabled
```

## Min and Max Year configuration

By default Tzdata will use 1900 for the min year to look at rules and 44 years + the current year
for the max. This may be too much data for some (e.g. when using an embedded system), so
we can configure the max and min years of data to use as well. This can greatly greatly reduce the
size of the ets table created by `:tzdata`.

```elixir
config :tzdata, :max_year, 2030

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isnt' that a bit too close for comfort ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean? Bearing in mind that this is just an example in the README, it is not in the code.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah missed the README part, nevermind

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is it actually configured to on the system ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"44 years + the current year"

config :tzdata, :min_year, 2020
```

If the autoupdate setting is set to disabled, one has to manually put updated .ets files
in the release_ets sub-dir of the "data_dir" (see the "Data directory and releases" section above).
When IANA releases new versions of the time zone data, this Tzdata library can be used to generate
Expand Down
74 changes: 70 additions & 4 deletions lib/tzdata/period_builder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,58 @@ defmodule Tzdata.PeriodBuilder do
{:ok, Map.get(btz_data.rules, rules_name)}
end

def max_year() do
case Application.fetch_env(:tzdata, :max_year) do
{:ok, nil} ->
@max_year

{:ok, max_year} ->
if is_valid_max_year?(max_year) do
max_year
else
@max_year
end

_ ->
@max_year
end
end

def min_year() do
case Application.fetch_env(:tzdata, :min_year) do
{:ok, nil} ->
@min_year

{:ok, min_year} ->
if is_valid_min_year?(min_year) do
min_year
else
@min_year
end

_ ->
@min_year
end
end

defp is_valid_max_year?(max_year) when is_integer(max_year) do
# don't allow greater than default max_year
max_year <= @max_year
end

defp is_valid_max_year?(_max_year) do
false
end

defp is_valid_min_year?(min_year) when is_integer(min_year) do
# don't allow lower than default min_year
min_year >= @min_year
end

defp is_valid_min_year?(_min_year) do
false
end

def calc_periods(btz_data, [zone_line_hd | zone_line_tl], from, zone_hd_rules, letter)
when zone_hd_rules == nil do
# since there are no rules, there is no standard offset
Expand Down Expand Up @@ -51,6 +103,8 @@ defmodule Tzdata.PeriodBuilder do
utc_off = zone_line_hd.gmtoff
from_standard_time = standard_time_from_utc(from, utc_off)
zone_line_limit = Map.get(zone_line_hd, :until)
min_year = min_year()
max_year = max_year()

# Get the year of the "from" time. We use the standard time with utc offset
# applied. If for instance we are ahead of UTC and the period starts at the
Expand All @@ -59,17 +113,29 @@ defmodule Tzdata.PeriodBuilder do
from_standard_time_year =
case from do
:min ->
@min_year
min_year

_ ->
{{year, _, _}, _} = :calendar.gregorian_seconds_to_datetime(from_standard_time)
year

if year < min_year do
min_year
else
year
end
end

max_year_to_use =
case zone_line_limit do
{{{year, _, _}, _}, _} -> year
nil -> @max_year
{{{year, _, _}, _}, _} ->
if year > max_year do
max_year
else
year
end

nil ->
max_year
end

years_to_use = from_standard_time_year..max_year_to_use |> Enum.to_list()
Expand Down