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))