-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add parsing of zone1970.tab table data.
- Loading branch information
Showing
7 changed files
with
495 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
defmodule Tzdata.TableData do | ||
file_read = Tzdata.TableParser.read_file |> Enum.to_list | ||
|
||
timezones = Enum.map(file_read, &(&1["timezone"])) | ||
|
||
@doc """ | ||
Returns a list of all timezones found in the zone1970.tab file | ||
""" | ||
def timezones do | ||
unquote(Macro.escape(timezones)) | ||
end | ||
|
||
country_codes = file_read | ||
|> Enum.flat_map(&(&1["country_codes"])) | ||
|> Enum.uniq | ||
|> Enum.sort | ||
|
||
@doc """ | ||
Returns a list of all country_codes found in the zone1970.tab file | ||
""" | ||
def country_codes do | ||
unquote(Macro.escape( | ||
country_codes | ||
)) | ||
end | ||
|
||
#by_timezone = file_read |> Enum.group_by(&(&1["timezone"])) | ||
|
||
keyword_dict_by_country_codes = file_read |> Enum.flat_map(fn entry -> | ||
Enum.map(entry["country_codes"], &({&1|>String.to_atom, entry})) | ||
end) | ||
|
||
Enum.each country_codes, fn (country_code) -> | ||
def for_country_code(unquote(country_code)) do | ||
unquote(Macro.escape( | ||
Keyword.get_values keyword_dict_by_country_codes, | ||
String.to_atom(country_code) | ||
)) | ||
end | ||
end | ||
def for_country_code(_), do: :country_code_not_found | ||
|
||
Enum.each timezones, fn (timezone) -> | ||
def for_timezone(unquote(timezone)) do | ||
unquote(Macro.escape( | ||
Enum.find(file_read, fn(elem) -> elem["timezone"] == timezone end ) | ||
)) | ||
end | ||
end | ||
def for_timezone(_), do: :timezone_not_found | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
defmodule Tzdata.TableParser do | ||
@moduledoc """ | ||
Parsing of the table file zone1970.tab | ||
""" | ||
@file_name "zone1970.tab" | ||
def read_file(dir_prepend \\ "source_data", file_name \\ @file_name) do | ||
File.stream!("#{dir_prepend}/#{file_name}") | ||
|> process_file | ||
end | ||
|
||
def process_file(file_stream) do | ||
file_stream | ||
|> filter_comment_lines | ||
|> filter_empty_lines | ||
|> Stream.map(&(strip_comment(&1))) # Strip comments at line end. Like this comment. | ||
|> Stream.map(&(process_line(&1))) | ||
end | ||
|
||
@line_regex ~r/(?<country_codes>[^\s]+)[\s]+(?<latlong>[^\s]+)[\s]+(?<timezone>[^\s]+)[\s]+(?<comments>[^\n]+)?/ | ||
defp process_line(line) do | ||
map = Regex.named_captures(@line_regex, line) | ||
map = %{map | "country_codes" => split_country_codes(map["country_codes"]) } | ||
map | ||
end | ||
|
||
defp split_country_codes(string) do | ||
String.split(string, ",") | ||
end | ||
|
||
defp strip_comment(line), do: Regex.replace(~r/[\s]*#.+/, line, "") | ||
defp filter_comment_lines(input) do | ||
Stream.filter(input, fn x -> !Regex.match?(~r/^[\s]*#/, x) end) | ||
end | ||
defp filter_empty_lines(input) do | ||
Stream.filter(input, fn x -> !Regex.match?(~r/^\n$/, x) end) | ||
end | ||
end |
Oops, something went wrong.