Reputation: 10350
Having ISO8601 datetime strings like 2020-11-22T12:14:50+0100
and 2020-11-22T12:14:50+0200
(note the different timezone), is it possible to use Julia's Dates.DateTime(::DateTime, ::DateFormat)
to convert this to a proper datetime by adding some wildcard characters in the DateFormat
? We learn from the docs:
DateTime type is not aware of time zones (naive, in Python parlance), analogous to a LocalDateTime in Java 8. Additional time zone functionality can be added through the TimeZones.jl package, which compiles the IANA time zone database
x = ["2020-11-22T12:14:50+0100", "2020-11-22T12:14:50+0200"]
Dates.DateTime.(x) # fails
# somewhat hacky but doesn't fail.
x = replace.(x, r"\+0.+00" => "")
Dates.DateTime.(x, DateFormat("y-m-dTH:M:S"))
# 2-element Array{DateTime,1}:
# 2020-11-22T12:14:50
# 2020-11-22T12:14:50
Would be there any way to add wildcards to avoid replace
?
E.g.
# not run
Dates.DateTime.(x, DateFormat("y-m-dTH:M:S+0*00"))
Upvotes: 1
Views: 778
Reputation: 31362
As the docs you quoted suggest, this functionality is provided by the TimeZones.jl package. That's the best way to do this:
julia> using Dates, TimeZones
julia> x = ["2020-11-22T12:14:50+0100", "2020-11-22T12:14:50+0200"]
2-element Array{String,1}:
"2020-11-22T12:14:50+0100"
"2020-11-22T12:14:50+0200"
julia> ZonedDateTime.(x, DateFormat("y-m-dTHH:MM:SSzzzz"))
2-element Array{ZonedDateTime,1}:
2020-11-22T12:14:50+01:00
2020-11-22T12:14:50+02:00
julia> DateTime.(x, DateFormat("y-m-dTHH:MM:SSzzzz")) # Drops the timezone
2-element Array{DateTime,1}:
2020-11-22T12:14:50
2020-11-22T12:14:50
If you really just want to trash all TimeZone information, I'd probably do it by split
ting on the final +
or -
character instead of using a regex (there are some half- and even quarter-hour timezones, so your regex won't always work).
julia> first.(rsplit.(x, [['+','-']], limit=2))
2-element Array{SubString{String},1}:
"2020-11-22T12:14:50"
"2020-11-22T12:14:50"
julia> DateTime.(first.(rsplit.(x, [['+','-']], limit=2)), DateFormat("y-m-dTH:M:S"))
2-element Array{DateTime,1}:
2020-11-22T12:14:50
2020-11-22T12:14:50
Upvotes: 3