How to represent Abstract Time in Julia

Viewed 48

I am trying to represent Abstract Time in Julia but was unable to find it referenced in the Standard Library's Dates section. Is there a way to represent Abstract Time in Julia by doing something like Year(1) + Day(1)?

The reason I ask is that some code I want to test dispatches on Dates.AbstractTime so I need to pass the appropriate type.

1 Answers

I believe this functionality is already present in the language:

julia> using Dates

julia> Year(2)+Month(3)+Day(100)
2 years, 3 months, 100 days

However, if you want your very special AbstractTime operator here is a toy example that should be useful:

struct MyTime <: Dates.AbstractTime
    value::Float64
end
days(d::Day) = d.value
days(h::Hour) = h.value/24
days(m::Minute) = m.value/24/60
days(m::MyTime) = m.value
⊕(a::Dates.AbstractTime, b::Dates.AbstractTime) = MyTime(days(a)+days(b))

Example usage:

julia> Day(1) ⊕ Hour(12) ⊕ Minute(6)
MyTime(1.5041666666666667)
Related