Skip to content

Commit

Permalink
correct the conversion of MIT-range to a range of float
Browse files Browse the repository at this point in the history
e.g. `float(2021Q1:2022Q4)` returns `2021.0:0.25:2022.75`
  • Loading branch information
bbejanov committed Dec 12, 2023
1 parent 8474159 commit b6f3f79
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/momentintime.jl
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,8 @@ Base.one(::Union{MIT,Duration,Type{<:MIT},Type{<:Duration}}) = Int(1)
# In the special case of YPFrequency we want the year to be the whole part and the period to be the fractional part.
(T::Type{<:AbstractFloat})(x::MIT{<:YPFrequency{N}}) where {N} = convert(T, ((y, p) = mit2yp(x); y + (p - 1) / N))
Base.promote_rule(::Type{<:MIT}, ::Type{T}) where {T<:AbstractFloat} = T
(T::Type{<:AbstractFloat})(x::Duration) = convert(T, Int(x))
(T::Type{<:AbstractFloat})(x::Duration{<:YPFrequency{N}}) where {N} = convert(T, Int(x) / N)

# frequency comparisons
Base.isless(x::Type{<:Frequency}, y::Type{<:Frequency}) = isless(ppy(x), ppy(y))
Expand Down Expand Up @@ -863,6 +865,8 @@ Base.union(l::UnitRange{MIT{F}}, r::UnitRange{MIT{F}}) where {F<:Frequency} = mi
# Base.issubset(l::UnitRange{<:MIT}, r::UnitRange{<:MIT}) = false
# Base.issubset(l::UnitRange{MIT{F}}, r::UnitRange{MIT{F}}) where F <: Frequency = first(r) <= first(l) && last(l) <= last(r)

Base.float(rng::UnitRange{MIT{F}}) where {F} = float(first(rng)):float(Duration{F}(1)):float(last(rng))

#------------------------------
# sort!() a list of MITs
Base.sort!(a::AbstractVector{<:MIT}, args...; kwargs...) = (sort!(reinterpret(Int, a), args...; kwargs...); a)
Expand Down

0 comments on commit b6f3f79

Please sign in to comment.