quinnj / Dates.jl

[DEPRECATED] Date/DateTime Implementation for the Julia Language; Successor to Datetime.jl
Other
8 stars 9 forks source link

How to parse strings with ignorable start characters? #13

Closed jiahao closed 10 years ago

jiahao commented 10 years ago

I have some strings to parse which look like "[14:51:00.118]" which are hours, minutes and seconds. I'm having trouble figuring out what to pass to DateFormat to parse this string:

julia> Dates.parse("[14:51:00.118]", DateFormat("[HH:MM:SS.sss]"))
ERROR: BoundsError()
 in parse at /Users/jiahao/.julia/v0.3/Dates/src/io.jl:108

julia> Dates.parse("14:51:00.118", DateFormat("HH:MM:SS.sss"))
4-element Array{Period,1}:
 14 hours        
 51 minutes      
 0 seconds       
 118 milliseconds

julia> Dates.parse("[14:51:00.118]", DateFormat("[HH:MM:SS.sss?"))
ERROR: BoundsError()
 in parse at /Users/jiahao/.julia/v0.3/Dates/src/io.jl:108

julia> Dates.parse("[14:51:00.118]", DateFormat("?HH:MM:SS.sss?"))
ERROR: BoundsError()
 in parse at /Users/jiahao/.julia/v0.3/Dates/src/io.jl:108

julia> Dates.parse("x14:51:00.118", DateFormat("?HH:MM:SS.sss"))
ERROR: BoundsError()
 in parse at /Users/jiahao/.julia/v0.3/Dates/src/io.jl:108

julia> Dates.parse("14:51:00.118]", DateFormat("HH:MM:SS.sss?"))
4-element Array{Period,1}:
 14 hours      
 51 minutes    
 0 seconds     
 0 milliseconds
quinnj commented 10 years ago

OK, I added the ability to have "starting transition" characters and some tests. For now the characters used in the format string have to match what is later given in the date string, but I kind like the idea of using '?' as a wild card character of sorts.

jiahao commented 10 years ago

On current master, I still get:

julia> Dates.DateTime("[14:51:00.118]", "[HH:MM:SS.sss]")
0001-01-01T14:51:00.118

julia> Dates.parse("[14:51:00.118]", Date("[HH:MM:SS.sss]"))
ERROR: ArgumentError("Delimiter mismatch. Couldn't find first delimiter, \"-\", in date string")
 in parse at /home/jiahao/.julia/v0.3/Dates/src/io.jl:112
 in Date at /home/jiahao/.julia/v0.3/Dates/src/io.jl:153
quinnj commented 10 years ago

You need to use DateFormat instead of Date for the 2nd argument.

jiahao commented 10 years ago

Thanks, that solved the issue for me