Skip to content

Commit

Permalink
Improve parse constructor exception message (JuliaTime#193)
Browse files Browse the repository at this point in the history
  • Loading branch information
omus authored and kpamnany committed May 5, 2023
1 parent 1ccf384 commit c69ceb1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/parse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,19 @@ end
# Note: ISOZonedDateTimeFormat is defined in the module __init__ which means that this
# function can not be called from within this module. TODO: Ignore linting for this line
function ZonedDateTime(str::AbstractString, df::DateFormat=ISOZonedDateTimeFormat)
parse(ZonedDateTime, str, df)
try
parse(ZonedDateTime, str, df)
catch e
if e isa ArgumentError
rethrow(ArgumentError(
"Unable to parse string \"$str\" using format $df. $(e.msg)"
))
else
rethrow()
end
end
end

function ZonedDateTime(str::AbstractString, format::AbstractString; locale::AbstractString="english")
ZonedDateTime(str, DateFormat(format, locale))
end
Expand Down
21 changes: 21 additions & 0 deletions test/parse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,24 @@ end
@testset "default format" begin
@test default_format(ZonedDateTime) === TimeZones.ISOZonedDateTimeFormat
end

@testset "parse constructor" begin
@test isequal(
ZonedDateTime("2000-01-02T03:04:05.006+0700"),
ZonedDateTime(2000, 1, 2, 3, 4, 5, 6, tz"UTC+07")
)
@test isequal(
ZonedDateTime("2018-11-01-0600", dateformat"yyyy-mm-ddzzzz"),
ZonedDateTime(2018, 11, 1, tz"UTC-06"),
)

# Validate that error message contains the original string and the format used
str = "2018-11-01"
try
ZonedDateTime(str)
catch e
@test e isa ArgumentError
@test occursin(str, e.msg)
@test occursin(string(TimeZones.ISOZonedDateTimeFormat), e.msg)
end
end

0 comments on commit c69ceb1

Please sign in to comment.