T0 = now()
T = now() - T0
typeof(T)
Base.Dates.Millisecond
If I want to convert to seconds, I can do
convert(Float64, (now() - T0).value)/1000
Well, that’s ugly, and depends on knowing that the time period happens to be milliseconds. Is there a better way?
Perhaps tosecond{T}(t::T) = t / convert(T, Base.Dates.Second(1))
?
3 Likes
Nice! Maybe should be included in DateTimes, or added to docs
1 Like
I apologize for necroposting, but I’ve found this implementation so useful that I’ve been returning to this specific post about a billion times, so I can imagine others might have too…
Anyways, here’s a slight improvement that takes care of time periods that are larger than a second (in case this is useful for others as well):
using Dates
tosecond(x::Hour) = Float64(Dates.value(Second(x)))
tosecond(x::Minute) = Float64(Dates.value(Second(x)))
tosecond(x::T) where {T<:TimePeriod} = x/convert(T, Second(1))
3 Likes