Skip to content

Instantly share code, notes, and snippets.

@pfitzseb
Created November 27, 2019 12:03
Show Gist options
  • Save pfitzseb/b405bead3cd85d9c769698c2ed692d4d to your computer and use it in GitHub Desktop.
Save pfitzseb/b405bead3cd85d9c769698c2ed692d4d to your computer and use it in GitHub Desktop.
timeperiod parsing
using Dates
period(::Dates.DatePart{T}, val) where T = Dates.CONVERSION_SPECIFIERS[T](val)
period(::Dates.Delim, val) = Millisecond(0)
Base.parse(t::Type{TimePeriod}, str::AbstractString, df::AbstractString) = parse(t, str, DateFormat(df))
function Base.parse(t::Type{TimePeriod}, str::AbstractString, df::DateFormat)
out = Millisecond(0)
len = length(str)
i = 1
for t in df.tokens
res, i = Dates.tryparsenext(t, str, i, len)
out += period(t, res)
end
out
end
TimePeriod(str::AbstractString, df) = parse(TimePeriod, str, df)
TimePeriod("00:14:32:11.4", "dd:HH:MM:SS")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment