Created
November 27, 2019 12:03
-
-
Save pfitzseb/b405bead3cd85d9c769698c2ed692d4d to your computer and use it in GitHub Desktop.
timeperiod parsing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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