Skip to content

Commit

Permalink
Merge pull request #1 from alanj853/year_config
Browse files Browse the repository at this point in the history
Make min_year and max_year configurable
  • Loading branch information
alanj853 authored Feb 9, 2024
2 parents 830f445 + e2c8fde commit 082ecb0
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 4 deletions.
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
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

0 comments on commit 082ecb0

Please sign in to comment.