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

Improve handling edge cases for Time::Location#load #10140

Merged
Merged
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
Empty file.
18 changes: 15 additions & 3 deletions spec/std/time/location_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,23 @@ class Time::Location
end

it "invalid timezone identifier" do
expect_raises(InvalidLocationNameError, "Foobar/Baz") do
Location.load("Foobar/Baz")
with_zoneinfo(datapath("zoneinfo")) do
expect_raises(InvalidLocationNameError, "Foobar/Baz") do
Location.load("Foobar/Baz")
end
end

Location.load?("Foobar/Baz", Crystal::System::Time.zone_sources).should be_nil
Location.load?("Foobar/Baz", [datapath("zoneinfo")]).should be_nil
end

it "name is folder" do
Location.load?("Foo", [datapath("zoneinfo")]).should be_nil
end

it "invalid zone file" do
expect_raises(Time::Location::InvalidTZDataError) do
Location.load?("Foo/invalid", [datapath("zoneinfo")])
end
end

it "treats UTC as special case" do
Expand Down
7 changes: 5 additions & 2 deletions src/time/location/loader.cr
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,12 @@ class Time::Location
def self.find_zoneinfo_file(name : String, sources : Enumerable(String))
sources.each do |source|
if source.ends_with?(".zip")
return source if File.exists?(source)
path = source
else
path = File.join(source, name)
return source if File.exists?(path)
end

return source if File.exists?(path) && File.file?(path) && File.readable?(path)
end
end

Expand Down Expand Up @@ -145,6 +146,8 @@ class Time::Location
end

new(location_name, zones, transitions)
rescue exc : IO::Error
raise InvalidTZDataError.new(cause: exc)
end

private def self.read_int32(io : IO)
Expand Down