JuliaTime / TimeZones.jl

IANA time zone database access for the Julia programming language
Other
87 stars 52 forks source link

UTC Zone issue with Dates.format #306

Closed yalwan-iqvia closed 3 years ago

yalwan-iqvia commented 3 years ago

If I have a project using only Dates, and want to format a datetime with Z at the end to indicate it is in UTC, I would do this:

import Dates
Dates.format(Dates.DateTime(Dates.now()), "YYYY-mm-ddTHH:MM:SS.sssZ")

"2020-11-13T13:52:02.276Z"

If I import TimeZones and try this:

import Dates, TimeZones
Dates.format(Dates.DateTime(Dates.now()), "YYYY-mm-ddTHH:MM:SS.sssZ")

ERROR: type DateTime has no field zone Stacktrace: [1] getproperty(::Dates.DateTime, ::Symbol) at ./Base.jl:33 [2] format(::Base.GenericIOBuffer{Array{UInt8,1}}, ::Dates.DatePart{'Z'}, ::Dates.DateTime, ::Dates.DateLocale) at /home/yaxattax/.julia/packages/TimeZones/fr1IP/src/parse.jl:81 [3] macro expansion at /builddir/build/BUILD/julia/build/usr/share/julia/stdlib/v1.4/Dates/src/io.jl:531 [inlined] [4] format(::Base.GenericIOBuffer{Array{UInt8,1}}, ::Dates.DateTime, ::Dates.DateFormat{Symbol("YYYY-mm-ddTHH:MM:SS.sssZ"),Tuple{Dates.DatePart{'Y'},Dates.Delim{Char,1},Dates.DatePart{'m'},Dates.Delim{Char,1},Dates.DatePart{'d'},Dates.Delim{Char,1},Dates.DatePart{'H'},Dates.Delim{Char,1},Dates.DatePart{'M'},Dates.Delim{Char,1},Dates.DatePart{'S'},Dates.Delim{Char,1},Dates.DatePart{'s'},Dates.DatePart{'Z'}}}) at /builddir/build/BUILD/julia/build/usr/share/julia/stdlib/v1.4/Dates/src/io.jl:527 [5] format(::Dates.DateTime, ::Dates.DateFormat{Symbol("YYYY-mm-ddTHH:MM:SS.sssZ"),Tuple{Dates.DatePart{'Y'},Dates.Delim{Char,1},Dates.DatePart{'m'},Dates.Delim{Char,1},Dates.DatePart{'d'},Dates.Delim{Char,1},Dates.DatePart{'H'},Dates.Delim{Char,1},Dates.DatePart{'M'},Dates.Delim{Char,1},Dates.DatePart{'S'},Dates.Delim{Char,1},Dates.DatePart{'s'},Dates.DatePart{'Z'}}}, ::Int64) at /builddir/build/BUILD/julia/build/usr/share/julia/stdlib/v1.4/Dates/src/io.jl:538 [6] format(::Dates.DateTime, ::Dates.DateFormat{Symbol("YYYY-mm-ddTHH:MM:SS.sssZ"),Tuple{Dates.DatePart{'Y'},Dates.Delim{Char,1},Dates.DatePart{'m'},Dates.Delim{Char,1},Dates.DatePart{'d'},Dates.Delim{Char,1},Dates.DatePart{'H'},Dates.Delim{Char,1},Dates.DatePart{'M'},Dates.Delim{Char,1},Dates.DatePart{'S'},Dates.Delim{Char,1},Dates.DatePart{'s'},Dates.DatePart{'Z'}}}) at /builddir/build/BUILD/julia/build/usr/share/julia/stdlib/v1.4/Dates/src/io.jl:537 [7] format(::Dates.DateTime, ::String; locale::Dates.DateLocale) at /builddir/build/BUILD/julia/build/usr/share/julia/stdlib/v1.4/Dates/src/io.jl:577 [8] format(::Dates.DateTime, ::String) at /builddir/build/BUILD/julia/build/usr/share/julia/stdlib/v1.4/Dates/src/io.jl:577

iamed2 commented 3 years ago

If you want to use a literal Z, then escape it like so:

Dates.format(Dates.DateTime(Dates.now()), "YYYY-mm-ddTHH:MM:SS.sss\\Z")

This will work in both cases.

yalwan-iqvia commented 3 years ago

Thanks for the suggestion! However, do you not think that it is a problem that modifying stdlib functions in this way is perhaps unexpected on the part of the user? Is there something this package can do to prevent this from occurring?

The reason why I say so was that this occurred for me as part of a complex chain of dependencies and I had to isolate the "fault" out from it all to this. I'm able to work around it in my application via type piracy, but I feel like type piracy to fix issues introduced by type piracy is not the best solution.

iamed2 commented 3 years ago

I agree that this is a frustrating problem and not a great solution.

Unfortunately this has to do with the way Dates formatting was set up a while ago. @omus may be looking at an overhaul of that when he's back from leave.

Perhaps Base can change to recognize the Z with a fallback in order to make the case when TimeZones is loaded similar to the case where it isn't?

Or, with a major release, TimeZones could stop using the capital Z, which is a common literal character in timestamps.

I'm going to leave this up to @omus though as I'm not really a maintainer of this package.

omus commented 3 years ago

Closing as a duplicate of #303. Will move the conversation there