How convert a string with a date and time info with submilisecond resolution to DateTime or perhaps just Time?

What is the simplest way to convert the 2019-12-06 12:01:40.300199 string into a DateTime or perhaps Time type of a variable?

The trouble is obviously that the DateTime only considers integer milliseconds as the time resolution and the following

julia> df = DateFormat("Y-m-d H:M:S.s")
dateformat"Y-m-d H:M:S.s"
julia> DateTime("2019-12-06 12:01:40.300199",df)
ERROR: InexactError: convert(Dates.Decimal3, 300199)
Stacktrace:
 [1] tryparsenext at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.3/Dates/src/io.jl:153 [inlined]
 [2] tryparsenext at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.3/Dates/src/io.jl:41 [inlined]
 [3] macro expansion at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.3/Dates/src/parse.jl:64 [inlined]
 [4] tryparsenext_core(::String, ::Int64, ::Int64, ::DateFormat{Symbol("Y-m-d H:M:S.s"),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'}}}, ::Bool) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.3/Dates/src/parse.jl:40
 [5] macro expansion at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.3/Dates/src/parse.jl:150 [inlined]
 [6] tryparsenext_internal at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.3/Dates/src/parse.jl:127 [inlined]
 [7] parse(::Type{DateTime}, ::String, ::DateFormat{Symbol("Y-m-d H:M:S.s"),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'}}}) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.3/Dates/src/parse.jl:282
 [8] DateTime(::String, ::DateFormat{Symbol("Y-m-d H:M:S.s"),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'}}}) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.3/Dates/src/io.jl:482
 [9] top-level scope at none:0

then does not work, while

julia> DateTime("2019-12-06 12:01:40.301",df)
2019-12-06T12:01:40.301

is OK.

As a matter of fact, I do not need to keep the Date part. I am only interested in the Time part. I can split the string into the two parts

julia> split("2019-12-06 12:01:40.300199")
2-element Array{SubString{String},1}:
 "2019-12-06"     
 "12:01:40.300199"

but what shall I do next? Just converting the string into Time as in

julia> Time("12:01:40.300199")
ERROR: InexactError: convert(Dates.Decimal3, 300199)
Stacktrace:
 [1] tryparsenext at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.3/Dates/src/io.jl:153 [inlined]
 [2] tryparsenext at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.3/Dates/src/io.jl:41 [inlined]
 [3] macro expansion at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.3/Dates/src/parse.jl:64 [inlined]
 [4] tryparsenext_core(::String, ::Int64, ::Int64, ::DateFormat{Symbol("HH:MM:SS.s"),Tuple{Dates.DatePart{'H'},Dates.Delim{Char,1},Dates.DatePart{'M'},Dates.Delim{Char,1},Dates.DatePart{'S'},Dates.Delim{Char,1},Dates.DatePart{'s'}}}, ::Bool) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.3/Dates/src/parse.jl:40
 [5] macro expansion at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.3/Dates/src/parse.jl:150 [inlined]
 [6] tryparsenext_internal at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.3/Dates/src/parse.jl:127 [inlined]
 [7] parse at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.3/Dates/src/parse.jl:282 [inlined]
 [8] Time at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.3/Dates/src/io.jl:524 [inlined] (repeats 2 times)
 [9] top-level scope at none:0

does not work. No surprise, the documentation clearly says that microseconds and nanoseconds are set to zero. I simply cannot find in the documentation anything equivalent to DateFormat. I seem to be unable to decipher the rest of the documentation for Time. Any advice on how to proceed systematically without parsing the string manually and fitting into the n-tuple for construction as described in the documentation?

I think you have two choices (not mutually exclusive): do the manual parsing, (e.g. ms = parse(Int,s[21:23]), μs = parse(Int,s[24:26]) ) and/or do a PR to add a new Code μ, which Matches 199 with microseconds.

1 Like