[ANN] TimeFloats.jl: Converting time to floats and vice versa

Hi all!

TimeFloats.jl is a tiny utility package that converts between Julia’s Dates period types (Nanosecond, Second, Hour, Time, CompoundPeriod, etc.) and plain floating-point numbers, using any FixedPeriod as the unit of measurement.

My main motivation for creating this package was the fact that I’ve been copy-pasting tosecond{T}(t::T) = t / convert(T, Base.Dates.Second(1)) from Kristoffer Carlsson’s post for long enough..!

Who needs this?

  • Do you need to convert a Dates.Time to float seconds?
julia> tosecond(Time(13,39,42,652))
49182.652
  • Do you need to convert a Dates.CompoundPeriod to float minutes?
julia> tofloat(Minute, Week(21) + Hour(32) + Millisecond(45))
213600.00075
  • Do you need to convert a Dates.Hour to float nanoseconds?
julia> tofloat(Nanosecond, Hour(3))
1.08e13
  • Or how about converting from a float seconds to its canonical form?
julia> fromsecond(92384756.9823465)
92384756 seconds, 982 milliseconds, 346 microseconds, 504 nanoseconds
  • Or converting from a float milliseconds to Minutes (OBS: this approximates the result to the closest integer of minutes)?
julia> fromfloat(60_123, Millisecond)
60123 milliseconds

Registration

Package registration will occur in 3 days. After helpful discussion with @aplavin I discovered DateFormats.jl which does basically the same as TimeFloats. There is a more detailed comparison between the two packages in TimeFloats’s README, but the similarity was enough to dissuade me from adding this to General. I’ll keep tinkering and see where this settles though.

10 Likes