Skip to content

Commit f07d246

Browse files
committed
add try_convert
1 parent 9125776 commit f07d246

File tree

4 files changed

+14
-3
lines changed

4 files changed

+14
-3
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Unicode = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5"
1818
EzXML = "0.9.1, 1"
1919
Mocking = "0.7"
2020
RecipesBase = "0.7, 0.8, 1"
21-
ShortStrings = 0.3.5
21+
ShortStrings = "0.3.5"
2222
julia = "1"
2323

2424
[extras]

src/types/name.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,16 @@ end
1616

1717
Base.convert(::Type{String}, name::SName) = string(name)
1818
function Base.convert(::Type{SName}, str::AbstractString)
19+
name = try_convert(SName, str)
20+
name isa Nothing && DomainError(str, "All timezone name parts must have length < 16")
21+
return name
22+
end
23+
24+
try_convert(::Type{SName}, name::SName) = name
25+
try_convert(::Type{String}, name::String) = name
26+
function try_convert(::Type{SName}, str::AbstractString)
1927
parts = split(str, "/"; limit=3)
28+
all(length(parts) < 16) ||return nothing
2029
return if length(parts) == 3
2130
SName(parts[1], parts[2], parts[3])
2231
elseif length(parts) == 2
@@ -26,6 +35,7 @@ function Base.convert(::Type{SName}, str::AbstractString)
2635
end
2736
end
2837

38+
2939
Base.isempty(name::SName) = isempty(name.region) # region being empty implies all empty
3040

3141
name_parts(str::AbstractString) = split(str, "/")

src/types/timezone.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ function istimezone(str::Union{AbstractString, Name}, mask::Class=Class(:DEFAULT
100100
if mask & Class(:FIXED) != Class(:NONE) && occursin(FIXED_TIME_ZONE_REGEX, string(str))
101101
return true
102102
end
103-
name = convert(Name, str)
103+
name = try_convert(Name, str)
104+
name isa Nothing && return false
104105

105106
# Perform more expensive checks against pre-compiled time zones
106107
tz, class = get(TIME_ZONE_CACHE, str) do

src/tzdata/compile.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ using Dates: parse_components
44

55
using ...TimeZones: TIME_ZONE_CACHE
66
using ...TimeZones: TimeZones, TimeZone, FixedTimeZone, VariableTimeZone, Transition, Class
7-
using ...TimeZones: name_parts, rename
7+
using ...TimeZones: name_parts, rename, try_convert
88
using ..TZData: TimeOffset, ZERO, MIN_GMT_OFFSET, MAX_GMT_OFFSET, MIN_SAVE, MAX_SAVE,
99
ABS_DIFF_OFFSET
1010

0 commit comments

Comments
 (0)