Skip to content

Commit

Permalink
Support comparison between all Period subtypes (#354)
Browse files Browse the repository at this point in the history
* Support comparison between all Period subtypes

Note that defining `==` methods with `Base.:(==)` does not work on
Julia 0.4

* Add entry to README
  • Loading branch information
omus authored and tkelman committed May 12, 2017
1 parent d8a1c04 commit 1b4566d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ Currently, the `@compat` macro supports the following syntaxes:

* `Compat.StringVector` is supported on 0.5 and below. On 0.6 and later, it aliases `Base.StringVector`. This function allocates a `Vector{UInt8}` whose data can be made into a `String` in constant time; that is, without copying. On 0.5 and later, use `String(...)` with the vector allocated by `StringVector` as an argument to create a string without copying. Note that if 0.4 support is needed, `Compat.UTF8String(...)` should be used instead. ([#19449])

* `==(::Period, ::Period)` and `isless(::Period, ::Period)` is supported for 0.5 and below. Earlier versions of Julia only supported limited comparison methods between Periods which did not support comparing custom Period subtypes. ([#21378])

## Renamed functions

* `pointer_to_array` and `pointer_to_string` have been replaced with `unsafe_wrap(Array, ...)` and `unsafe_wrap(String, ...)` respectively
Expand Down
14 changes: 14 additions & 0 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1492,6 +1492,20 @@ else
const collect = Base.collect
end

# https://github.com/JuliaLang/julia/pull/21378
if VERSION < v"0.6.0-pre.beta.455"
import Base: ==, isless

==(x::Dates.Period, y::Dates.Period) = (==)(promote(x, y)...)
isless(x::Dates.Period, y::Dates.Period) = isless(promote(x,y)...)

# disallow comparing fixed to other periods
==(x::Dates.FixedPeriod, y::Dates.OtherPeriod) = throw(MethodError(==, (x, y)))
==(x::Dates.OtherPeriod, y::Dates.FixedPeriod) = throw(MethodError(==, (x, y)))
isless(x::Dates.FixedPeriod, y::Dates.OtherPeriod) = throw(MethodError(isless, (x, y)))
isless(x::Dates.OtherPeriod, y::Dates.FixedPeriod) = throw(MethodError(isless, (x, y)))
end

include("to-be-deprecated.jl")

# https://github.com/JuliaLang/julia/pull/21746
Expand Down
21 changes: 21 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1849,6 +1849,27 @@ let foo() = begin
@test foo() == 2
end

# PR 21378
let
# https://en.wikipedia.org/wiki/Swatch_Internet_Time
immutable Beat <: Dates.Period
value::Int64
end

Dates.value(b::Beat) = b.value
Dates.toms(b::Beat) = Dates.value(b) * 86400
Dates._units(b::Beat) = " beat" * (abs(Dates.value(b)) == 1 ? "" : "s")
Base.promote_rule(::Type{Dates.Day}, ::Type{Beat}) = Dates.Millisecond
Base.convert{T<:Dates.Millisecond}(::Type{T}, b::Beat) = T(Dates.toms(b))

@test Beat(1000) == Dates.Day(1)
@test Beat(1) < Dates.Day(1)
@test_throws MethodError Dates.Day(30) == Dates.Month(1)
@test_throws MethodError Dates.Month(1) == Dates.Day(30)
@test_throws MethodError Dates.Day(1) < Dates.Month(1)
@test_throws MethodError Dates.Month(1) < Dates.Day(1)
end

include("to-be-deprecated.jl")

nothing

0 comments on commit 1b4566d

Please sign in to comment.