Skip to content

Commit 1b4566d

Browse files
omustkelman
authored andcommittedMay 12, 2017
Support comparison between all Period subtypes (#354)
* Support comparison between all Period subtypes Note that defining `==` methods with `Base.:(==)` does not work on Julia 0.4 * Add entry to README
1 parent d8a1c04 commit 1b4566d

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed
 

‎README.md

+2
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ Currently, the `@compat` macro supports the following syntaxes:
171171

172172
* `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])
173173

174+
* `==(::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])
175+
174176
## Renamed functions
175177

176178
* `pointer_to_array` and `pointer_to_string` have been replaced with `unsafe_wrap(Array, ...)` and `unsafe_wrap(String, ...)` respectively

‎src/Compat.jl

+14
Original file line numberDiff line numberDiff line change
@@ -1492,6 +1492,20 @@ else
14921492
const collect = Base.collect
14931493
end
14941494

1495+
# https://github.com/JuliaLang/julia/pull/21378
1496+
if VERSION < v"0.6.0-pre.beta.455"
1497+
import Base: ==, isless
1498+
1499+
==(x::Dates.Period, y::Dates.Period) = (==)(promote(x, y)...)
1500+
isless(x::Dates.Period, y::Dates.Period) = isless(promote(x,y)...)
1501+
1502+
# disallow comparing fixed to other periods
1503+
==(x::Dates.FixedPeriod, y::Dates.OtherPeriod) = throw(MethodError(==, (x, y)))
1504+
==(x::Dates.OtherPeriod, y::Dates.FixedPeriod) = throw(MethodError(==, (x, y)))
1505+
isless(x::Dates.FixedPeriod, y::Dates.OtherPeriod) = throw(MethodError(isless, (x, y)))
1506+
isless(x::Dates.OtherPeriod, y::Dates.FixedPeriod) = throw(MethodError(isless, (x, y)))
1507+
end
1508+
14951509
include("to-be-deprecated.jl")
14961510

14971511
# https://github.com/JuliaLang/julia/pull/21746

‎test/runtests.jl

+21
Original file line numberDiff line numberDiff line change
@@ -1849,6 +1849,27 @@ let foo() = begin
18491849
@test foo() == 2
18501850
end
18511851

1852+
# PR 21378
1853+
let
1854+
# https://en.wikipedia.org/wiki/Swatch_Internet_Time
1855+
immutable Beat <: Dates.Period
1856+
value::Int64
1857+
end
1858+
1859+
Dates.value(b::Beat) = b.value
1860+
Dates.toms(b::Beat) = Dates.value(b) * 86400
1861+
Dates._units(b::Beat) = " beat" * (abs(Dates.value(b)) == 1 ? "" : "s")
1862+
Base.promote_rule(::Type{Dates.Day}, ::Type{Beat}) = Dates.Millisecond
1863+
Base.convert{T<:Dates.Millisecond}(::Type{T}, b::Beat) = T(Dates.toms(b))
1864+
1865+
@test Beat(1000) == Dates.Day(1)
1866+
@test Beat(1) < Dates.Day(1)
1867+
@test_throws MethodError Dates.Day(30) == Dates.Month(1)
1868+
@test_throws MethodError Dates.Month(1) == Dates.Day(30)
1869+
@test_throws MethodError Dates.Day(1) < Dates.Month(1)
1870+
@test_throws MethodError Dates.Month(1) < Dates.Day(1)
1871+
end
1872+
18521873
include("to-be-deprecated.jl")
18531874

18541875
nothing

0 commit comments

Comments
 (0)
Please sign in to comment.