Skip to content

Commit

Permalink
Remove serialization for Windows translation
Browse files Browse the repository at this point in the history
  • Loading branch information
omus committed Apr 6, 2017
1 parent db687d8 commit 5770279
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 66 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
deps/tzarchive
deps/tzsource
deps/compiled
deps/windows_to_posix
deps/local
test/tzsource
9 changes: 4 additions & 5 deletions src/local.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Compat: @static, is_apple, is_unix, is_windows, readstring
using Mocking

if is_windows()
import TimeZones.WindowsTimeZoneIDs: get_windows_translation
import TimeZones.WindowsTimeZoneIDs: WINDOWS_TRANSLATION
end

"""
Expand Down Expand Up @@ -116,12 +116,11 @@ function localzone()
end
end
elseif is_windows()
translation = get_windows_translation()

# Windows powershell should be available on Windows 7 and above
win_name = strip(@mock readstring(`powershell -Command "[TimeZoneInfo]::Local.Id"`))
if haskey(translation, win_name)
posix_name = translation[win_name]

if haskey(WINDOWS_TRANSLATION, win_name)
posix_name = WINDOWS_TRANSLATION[win_name]

# Translation dict includes Etc time zones which we currently are not supporting
# since they are deemed historical. To ensure compatibility with the translation
Expand Down
73 changes: 20 additions & 53 deletions src/winzone/WindowsTimeZoneIDs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ using LightXML
# http://cldr.unicode.org/development/development-process/design-proposals/extended-windows-olson-zid-mapping
const WINDOWS_ZONE_URL = "http://unicode.org/repos/cldr/trunk/common/supplemental/windowsZones.xml"

const WINDOWS_TRANSLATION_FILE = joinpath(DEPS_DIR, "windows_to_posix")
const WINDOWS_TRANSLATION = Dict{AbstractString,AbstractString}()
const WINDOWS_XML_DIR = joinpath(DEPS_DIR, "local")
const WINDOWS_XML_FILE = joinpath(WINDOWS_XML_DIR, "windowsZones.xml")

translation_dir = dirname(WINDOWS_TRANSLATION_FILE)
isdir(translation_dir) || mkdir(translation_dir)
isdir(WINDOWS_XML_DIR) || mkdir(WINDOWS_XML_DIR)

function compile(xml_file::AbstractString)
# Get the timezone conversions from the file
Expand All @@ -38,64 +37,32 @@ function compile(xml_file::AbstractString)
return translation
end

function compile(xml_file::AbstractString, translation_file::AbstractString)
open(translation_file, "w") do fp
serialize(fp, compile(xml_file))
end
const WINDOWS_TRANSLATION = if isfile(WINDOWS_XML_FILE)
compile(WINDOWS_XML_FILE)
else
Dict{AbstractString, AbstractString}()
end

function build(
xml_dir::AbstractString=joinpath(DEPS_DIR, "local"),
translation_file::AbstractString=WINDOWS_TRANSLATION_FILE;
force::Bool=false,
)
clean = false
xml_file = joinpath(xml_dir, "windowsZones2017a.xml")
function build(xml_file::AbstractString=WINDOWS_XML_FILE; force::Bool=false)
fallback_xml_file = joinpath(WINDOWS_XML_DIR, "windowsZones2017a.xml")

if !isfile(xml_file) || force
info("Downloading latest Windows to POSIX timezone ID XML")
xml_file = download(WINDOWS_ZONE_URL, joinpath(xml_dir, "windowsZones.xml"))
clean = true
if !isfile(xml_file)
if isfile(fallback_xml_file) && !force
cp(fallback_xml_file, xml_file)
else
info("Downloading latest Windows to POSIX timezone ID XML")
download(WINDOWS_ZONE_URL, xml_file)
end
end

info("Compiling Windows time zone name translation")
compile(xml_file, translation_file)

# Remove temporary XML file
if clean
rm(xml_file)
end
end
translation = compile(xml_file)

function build(; force::Bool=false)
build(force=force)
# Copy contents into translation constant
empty!(WINDOWS_TRANSLATION)
end

function load_translation(translation_file::AbstractString)
return open(translation_file, "r") do fp
deserialize(fp)
for (k, v) in translation
WINDOWS_TRANSLATION[k] = v
end
end

function get_windows_translation()
if isempty(WINDOWS_TRANSLATION)
if !isfile(WINDOWS_TRANSLATION_FILE)
error(
"Missing Windows to POSIX time zone translation file. ",
"Try running Pkg.build(\"TimeZones\").",
)
end

translation = load_translation(WINDOWS_TRANSLATION_FILE)

# Copy contents into translation constant
for (k, v) in translation
WINDOWS_TRANSLATION[k] = v
end
end

return WINDOWS_TRANSLATION
end

end
15 changes: 8 additions & 7 deletions test/winzone/WindowsTimeZoneIDs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ trans = TimeZones.WindowsTimeZoneIDs.compile(xml_file)
@test trans["Central European Standard Time"] == "Europe/Warsaw"

mktempdir() do temp_dir
translation_file = joinpath(temp_dir, "windows_to_posix")
@test !isfile(translation_file)
xml_file = joinpath(temp_dir, "windowZones.xml")
@test !isfile(xml_file)

# Does not perform download
TimeZones.WindowsTimeZoneIDs.build(dirname(xml_file), translation_file)
@test isfile(translation_file)
empty!(TimeZones.WindowsTimeZoneIDs.WINDOWS_TRANSLATION)
@test isempty()

trans = TimeZones.WindowsTimeZoneIDs.load_translation(translation_file)
@test isa(trans, Dict{AbstractString, AbstractString})
# Does not perform download
TimeZones.WindowsTimeZoneIDs.build(xml_file)
@test isfile(xml_file)
@test !isempty(TimeZones.WindowsTimeZoneIDs.WINDOWS_TRANSLATION)
end

0 comments on commit 5770279

Please sign in to comment.