Skip to content

Commit

Permalink
Fix compatibility with Julia 0.7 (#30)
Browse files Browse the repository at this point in the history
* Updates for compatibility with Julia 0.7

In particular:

* Use definitions from Compat, e.g. Compat.Dates

* Define a local version of sprint(show, args; kwargs), as this is not
  available in Compat

* Use arguments in DomainError constructors

* Remove custom show methods for types

* Raise the minimum required version of TimeZones

* Add 0.7 to the Travis testing matrix

* Add 0.7 to the AppVeyor builds
  • Loading branch information
ararslan authored and omus committed Jun 6, 2018
1 parent f93eb28 commit 3addff5
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 95 deletions.
2 changes: 2 additions & 0 deletions .appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ environment:
matrix:
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x86/0.6/julia-0.6-latest-win32.exe"
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x64/0.6/julia-0.6-latest-win64.exe"
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x86/0.7/julia-0.7-latest-win32.exe"
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x64/0.7/julia-0.7-latest-win64.exe"
- JULIA_URL: "https://julialangnightlies-s3.julialang.org/bin/winnt/x86/julia-latest-win32.exe"
- JULIA_URL: "https://julialangnightlies-s3.julialang.org/bin/winnt/x64/julia-latest-win64.exe"

Expand Down
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ os:
- osx
julia:
- 0.6
- 0.7
- nightly
matrix:
allow_failures:
Expand Down
4 changes: 2 additions & 2 deletions REQUIRE
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
julia 0.6
TimeZones 0.4
Compat 0.45
TimeZones 0.7
Compat 0.62.0
6 changes: 5 additions & 1 deletion src/Intervals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ __precompile__()

module Intervals

using Base.Dates
using Compat
using Compat.Dates
using Compat.Printf
using TimeZones

using Compat.Dates: value, coarserperiod
using Compat: AbstractDateTime

import Base: , , , , union, union!, merge
Expand Down
15 changes: 2 additions & 13 deletions src/anchoredinterval.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import TimeZones: astimezone
using Base.Dates: value, coarserperiod

"""
AnchoredInterval{P, T}(anchor::T, [inclusivity::Inclusivity]) where {P, T} -> AnchoredInterval{P, T}
Expand Down Expand Up @@ -166,21 +165,11 @@ end
Base.convert(::Type{T}, interval::AnchoredInterval{P, T}) where {P, T} = anchor(interval)

# Date/DateTime attempt to convert to Int64 instead of falling back to convert(T, ...)
Base.Date(interval::AnchoredInterval{P, Date}) where P = convert(Date, interval)
Base.DateTime(interval::AnchoredInterval{P, DateTime}) where P = convert(DateTime, interval)
Compat.Dates.Date(interval::AnchoredInterval{P, Date}) where P = convert(Date, interval)
Compat.Dates.DateTime(interval::AnchoredInterval{P, DateTime}) where P = convert(DateTime, interval)

##### DISPLAY #####

Base.show(io::IO, ::Type{HourEnding}) = print(io, "HourEnding{T}")
Base.show(io::IO, ::Type{HourBeginning}) = print(io, "HourBeginning{T}")

Base.show(io::IO, ::Type{HourEnding{T}}) where T <: TimeType = print(io, "HourEnding{$T}")
Base.show(io::IO, ::Type{HourBeginning{T}}) where T <: TimeType = print(io, "HourBeginning{$T}")

function Base.show(io::IO, ::Type{AnchoredInterval{P, T}}) where {P, T}
print(io, "AnchoredInterval{$P, $T}")
end

function Base.show(io::IO, interval::T) where T <: AnchoredInterval
if get(io, :compact, false)
print(io, interval)
Expand Down
2 changes: 0 additions & 2 deletions src/description.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using Base.Dates: value, coarserperiod

description(i::AnchoredInterval{P}) where P = description(i, P > zero(P) ? "B" : "E")

function description(interval::AnchoredInterval{P, T}, s::String) where {P, T}
Expand Down
6 changes: 3 additions & 3 deletions src/interval.jl
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,12 @@ Base.isopen(interval::AbstractInterval) = isopen(inclusivity(interval))

function Base.convert(::Type{T}, i::Interval{T}) where T
first(i) == last(i) && isclosed(i) && return first(i)
throw(DomainError())
throw(DomainError(i, "The interval is not closed with coinciding endpoints"))
end

# Date/DateTime attempt to convert to Int64 instead of falling back to convert(T, ...)
Base.Date(interval::Interval{Date}) = convert(Date, interval)
Base.DateTime(interval::Interval{DateTime}) = convert(DateTime, interval)
Compat.Dates.Date(interval::Interval{Date}) = convert(Date, interval)
Compat.Dates.DateTime(interval::Interval{DateTime}) = convert(DateTime, interval)

##### DISPLAY #####

Expand Down
121 changes: 57 additions & 64 deletions test/anchoredinterval.jl
Original file line number Diff line number Diff line change
Expand Up @@ -104,175 +104,168 @@ using Intervals: canonicalize
end

@testset "display" begin
@test sprint(show, HourEnding) == "HourEnding{T}"
@test sprint(show, HourBeginning) == "HourBeginning{T}"
@test sprint(show, AnchoredInterval{Hour(-1)}) ==
"Intervals.AnchoredInterval{-1 hour,T} where T"
mod_prefix * "AnchoredInterval{-1 hour,T} where T"
@test sprint(show, AnchoredInterval{Hour(1)}) ==
"Intervals.AnchoredInterval{1 hour,T} where T"

@test sprint(show, HourEnding{DateTime}) == "HourEnding{DateTime}"
@test sprint(show, HourBeginning{DateTime}) == "HourBeginning{DateTime}"
@test sprint(show, AnchoredInterval{Hour(-1), DateTime}) == "HourEnding{DateTime}"
@test sprint(show, AnchoredInterval{Hour(1), DateTime}) == "HourBeginning{DateTime}"
mod_prefix * "AnchoredInterval{1 hour,T} where T"

@test sprint(show, AnchoredInterval{Day(-1)}) ==
"Intervals.AnchoredInterval{-1 day,T} where T"
mod_prefix * "AnchoredInterval{-1 day,T} where T"
@test sprint(show, AnchoredInterval{Day(1)}) ==
"Intervals.AnchoredInterval{1 day,T} where T"
mod_prefix * "AnchoredInterval{1 day,T} where T"
@test sprint(show, AnchoredInterval{Day(-1), DateTime}) ==
"AnchoredInterval{-1 day, DateTime}"
mod_prefix * "AnchoredInterval{-1 day,DateTime}"
@test sprint(show, AnchoredInterval{Day(1), DateTime}) ==
"AnchoredInterval{1 day, DateTime}"
mod_prefix * "AnchoredInterval{1 day,DateTime}"

interval = HourEnding(dt)
@test string(interval) == "(2016-08-11 HE02]"
@test sprint(showcompact, interval) == string(interval)
@test sprint(show, interval, context=:compact=>true) == string(interval)
@test sprint(show, interval) ==
"HourEnding{DateTime}(2016-08-11T02:00:00, Inclusivity(false, true))"
mod_prefix * "AnchoredInterval{-1 hour,DateTime}(2016-08-11T02:00:00, Inclusivity(false, true))"

interval = HourEnding(DateTime(2013, 2, 13), Inclusivity(true, false))
@test string(interval) == "[2013-02-12 HE24)"
@test sprint(showcompact, interval) == string(interval)
@test sprint(show, interval, context=:compact=>true) == string(interval)
@test sprint(show, interval) ==
"HourEnding{DateTime}(2013-02-13T00:00:00, Inclusivity(true, false))"
mod_prefix * "AnchoredInterval{-1 hour,DateTime}(2013-02-13T00:00:00, Inclusivity(true, false))"

interval = HourEnding(dt + Minute(15) + Second(30))
@test string(interval) == "(2016-08-11 HE02:15:30]"
@test sprint(showcompact, interval) == string(interval)
@test sprint(show, interval, context=:compact=>true) == string(interval)
@test sprint(show, interval) ==
"HourEnding{DateTime}(2016-08-11T02:15:30, Inclusivity(false, true))"
mod_prefix * "AnchoredInterval{-1 hour,DateTime}(2016-08-11T02:15:30, Inclusivity(false, true))"

interval = HourEnding(dt + Millisecond(2))
@test string(interval) == "(2016-08-11 HE02:00:00.002]"
@test sprint(showcompact, interval) == string(interval)
@test sprint(show, interval, context=:compact=>true) == string(interval)
@test sprint(show, interval) ==
"HourEnding{DateTime}(2016-08-11T02:00:00.002, Inclusivity(false, true))"
mod_prefix * "AnchoredInterval{-1 hour,DateTime}(2016-08-11T02:00:00.002, Inclusivity(false, true))"

interval = HourEnding(DateTime(2013, 2, 13, 0, 1), Inclusivity(true, false))
@test string(interval) == "[2013-02-13 HE00:01:00)"
@test sprint(showcompact, interval) == string(interval)
@test sprint(show, interval, context=:compact=>true) == string(interval)
@test sprint(show, interval) ==
"HourEnding{DateTime}(2013-02-13T00:01:00, Inclusivity(true, false))"
mod_prefix * "AnchoredInterval{-1 hour,DateTime}(2013-02-13T00:01:00, Inclusivity(true, false))"

interval = HourBeginning(dt)
@test string(interval) == "[2016-08-11 HB02)"
@test sprint(showcompact, interval) == string(interval)
@test sprint(show, interval, context=:compact=>true) == string(interval)
@test sprint(show, interval) ==
"HourBeginning{DateTime}(2016-08-11T02:00:00, Inclusivity(true, false))"
mod_prefix * "AnchoredInterval{1 hour,DateTime}(2016-08-11T02:00:00, Inclusivity(true, false))"

interval = HourBeginning(DateTime(2013, 2, 13), Inclusivity(false, true))
@test string(interval) == "(2013-02-13 HB00]"
@test sprint(showcompact, interval) == string(interval)
@test sprint(show, interval, context=:compact=>true) == string(interval)
@test sprint(show, interval) ==
"HourBeginning{DateTime}(2013-02-13T00:00:00, Inclusivity(false, true))"
mod_prefix * "AnchoredInterval{1 hour,DateTime}(2013-02-13T00:00:00, Inclusivity(false, true))"

interval = HourEnding(ZonedDateTime(dt, tz"America/Winnipeg"))
@test string(interval) == "(2016-08-11 HE02-05:00]"
@test sprint(showcompact, interval) == string(interval)
@test sprint(show, interval) == string(
"HourEnding{$ZonedDateTime}(2016-08-11T02:00:00-05:00, ",
@test sprint(show, interval, context=:compact=>true) == string(interval)
@test sprint(show, interval) == mod_prefix * string(
"AnchoredInterval{-1 hour,$ZonedDateTime}(2016-08-11T02:00:00-05:00, ",
"Inclusivity(false, true))",
)

interval = AnchoredInterval{Year(-1)}(Date(dt))
@test string(interval) == "(YE 2016-08-11]"
@test sprint(showcompact, interval) == string(interval)
@test sprint(show, interval, context=:compact=>true) == string(interval)
@test sprint(show, interval) ==
"AnchoredInterval{-1 year, Date}(2016-08-11, Inclusivity(false, true))"
mod_prefix * "AnchoredInterval{-1 year,Date}(2016-08-11, Inclusivity(false, true))"

interval = AnchoredInterval{Year(-1)}(ceil(Date(dt), Year))
@test string(interval) == "(YE 2017-01-01]"
@test sprint(showcompact, interval) == string(interval)
@test sprint(show, interval, context=:compact=>true) == string(interval)
@test sprint(show, interval) ==
"AnchoredInterval{-1 year, Date}(2017-01-01, Inclusivity(false, true))"
mod_prefix * "AnchoredInterval{-1 year,Date}(2017-01-01, Inclusivity(false, true))"

interval = AnchoredInterval{Month(-1)}(dt)
@test string(interval) == "(MoE 2016-08-11 02:00:00]"
@test sprint(showcompact, interval) == string(interval)
@test sprint(show, interval) == string(
"AnchoredInterval{-1 month, DateTime}(2016-08-11T02:00:00, ",
@test sprint(show, interval, context=:compact=>true) == string(interval)
@test sprint(show, interval) == mod_prefix * string(
"AnchoredInterval{-1 month,DateTime}(2016-08-11T02:00:00, ",
"Inclusivity(false, true))",
)

interval = AnchoredInterval{Month(-1)}(ceil(dt, Month))
@test string(interval) == "(MoE 2016-09-01]"
@test sprint(showcompact, interval) == string(interval)
@test sprint(show, interval) == string(
"AnchoredInterval{-1 month, DateTime}(2016-09-01T00:00:00, ",
@test sprint(show, interval, context=:compact=>true) == string(interval)
@test sprint(show, interval) == mod_prefix * string(
"AnchoredInterval{-1 month,DateTime}(2016-09-01T00:00:00, ",
"Inclusivity(false, true))",
)

interval = AnchoredInterval{Day(-1)}(DateTime(dt))
@test string(interval) == "(DE 2016-08-11 02:00:00]"
@test sprint(showcompact, interval) == string(interval)
@test sprint(show, interval) == string(
"AnchoredInterval{-1 day, DateTime}(2016-08-11T02:00:00, ",
@test sprint(show, interval, context=:compact=>true) == string(interval)
@test sprint(show, interval) == mod_prefix * string(
"AnchoredInterval{-1 day,DateTime}(2016-08-11T02:00:00, ",
"Inclusivity(false, true))"
)

interval = AnchoredInterval{Day(-1)}(ceil(DateTime(dt), Day))
@test string(interval) == "(DE 2016-08-12]"
@test sprint(showcompact, interval) == string(interval)
@test sprint(show, interval) == string(
"AnchoredInterval{-1 day, DateTime}(2016-08-12T00:00:00, ",
@test sprint(show, interval, context=:compact=>true) == string(interval)
@test sprint(show, interval) == mod_prefix * string(
"AnchoredInterval{-1 day,DateTime}(2016-08-12T00:00:00, ",
"Inclusivity(false, true))"
)

# Date(dt) will truncate the DateTime to the nearest day
interval = AnchoredInterval{Day(-1)}(Date(dt))
@test string(interval) == "(DE 2016-08-11]"
@test sprint(showcompact, interval) == string(interval)
@test sprint(show, interval, context=:compact=>true) == string(interval)
@test sprint(show, interval) ==
"AnchoredInterval{-1 day, Date}(2016-08-11, Inclusivity(false, true))"
mod_prefix * "AnchoredInterval{-1 day,Date}(2016-08-11, Inclusivity(false, true))"

# Prevent confusion when dealing with time zones by ensuring that the full date and
# time are displayed
zdt = ceil(ZonedDateTime(dt, tz"America/Winnipeg"), Day)
interval = AnchoredInterval{Day(-1)}(zdt)
@test string(interval) == "(DE 2016-08-12 00:00:00-05:00]"
@test sprint(showcompact, interval) == string(interval)
@test sprint(show, interval) == string(
"AnchoredInterval{-1 day, TimeZones.ZonedDateTime}(2016-08-12T00:00:00-05:00, ",
@test sprint(show, interval, context=:compact=>true) == string(interval)
@test sprint(show, interval) == mod_prefix * string(
"AnchoredInterval{-1 day,$(tz_prefix)ZonedDateTime}(2016-08-12T00:00:00-05:00, ",
"Inclusivity(false, true))"
)

interval = AnchoredInterval{Minute(-5)}(dt)
@test string(interval) == "(2016-08-11 5ME02:00]"
@test sprint(showcompact, interval) == string(interval)
@test sprint(show, interval) == string(
"AnchoredInterval{-5 minutes, DateTime}(2016-08-11T02:00:00, ",
@test sprint(show, interval, context=:compact=>true) == string(interval)
@test sprint(show, interval) == mod_prefix * string(
"AnchoredInterval{-5 minutes,DateTime}(2016-08-11T02:00:00, ",
"Inclusivity(false, true))",
)

interval = AnchoredInterval{Second(-30)}(dt)
@test string(interval) == "(2016-08-11 30SE02:00:00]"
@test sprint(showcompact, interval) == string(interval)
@test sprint(show, interval) == string(
"AnchoredInterval{-30 seconds, DateTime}(2016-08-11T02:00:00, ",
@test sprint(show, interval, context=:compact=>true) == string(interval)
@test sprint(show, interval) == mod_prefix * string(
"AnchoredInterval{-30 seconds,DateTime}(2016-08-11T02:00:00, ",
"Inclusivity(false, true))",
)

interval = AnchoredInterval{Millisecond(-10)}(dt)
@test string(interval) == "(2016-08-11 10msE02:00:00.000]"
@test sprint(showcompact, interval) == string(interval)
@test sprint(show, interval) == string(
"AnchoredInterval{-10 milliseconds, DateTime}(2016-08-11T02:00:00, ",
@test sprint(show, interval, context=:compact=>true) == string(interval)
@test sprint(show, interval) == mod_prefix * string(
"AnchoredInterval{-10 milliseconds,DateTime}(2016-08-11T02:00:00, ",
"Inclusivity(false, true))",
)

# Non-period AnchoredIntervals
interval = AnchoredInterval{-10}(10)
@test string(interval) == "(0 .. 10]"
@test sprint(showcompact, interval) == string(interval)
@test sprint(show, interval, context=:compact=>true) == string(interval)
@test sprint(show, interval) ==
"AnchoredInterval{-10, $Int}(10, Inclusivity(false, true))"
mod_prefix * "AnchoredInterval{-10,$Int}(10, Inclusivity(false, true))"

interval = AnchoredInterval{25}('a')
@test string(interval) == "[a .. z)"
@test sprint(showcompact, interval) == string(interval)
@test sprint(show, interval, context=:compact=>true) == string(interval)
@test sprint(show, interval) ==
"AnchoredInterval{25, Char}('a', Inclusivity(true, false))"
mod_prefix * "AnchoredInterval{25,Char}('a', Inclusivity(true, false))"
end

@testset "equality" begin
Expand Down
8 changes: 4 additions & 4 deletions test/inclusivity.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,22 @@
@testset "display" begin
inc = Inclusivity(false, false)
@test string(inc) == "Inclusivity (Open)"
@test sprint(showcompact, inc) == string(inc)
@test sprint(show, inc, context=:compact=>true) == string(inc)
@test sprint(show, inc) == "Inclusivity(false, false)"

inc = Inclusivity(false, true)
@test string(inc) == "Inclusivity (Right]"
@test sprint(showcompact, inc) == string(inc)
@test sprint(show, inc, context=:compact=>true) == string(inc)
@test sprint(show, inc) == "Inclusivity(false, true)"

inc = Inclusivity(true, false)
@test string(inc) == "Inclusivity [Left)"
@test sprint(showcompact, inc) == string(inc)
@test sprint(show, inc, context=:compact=>true) == string(inc)
@test sprint(show, inc) == "Inclusivity(true, false)"

inc = Inclusivity(true, true)
@test string(inc) == "Inclusivity [Closed]"
@test sprint(showcompact, inc) == string(inc)
@test sprint(show, inc, context=:compact=>true) == string(inc)
@test sprint(show, inc) == "Inclusivity(true, true)"
end

Expand Down
8 changes: 4 additions & 4 deletions test/interval.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,23 +80,23 @@
@testset "display" begin
interval = Interval(1, 2, Inclusivity(false, false))
@test string(interval) == "(1 .. 2)"
@test sprint(showcompact, interval) == string(interval)
@test sprint(show, interval, context=:compact=>true) == string(interval)
@test sprint(show, interval) == "Interval{$Int}(1, 2, Inclusivity(false, false))"

interval = Interval('a', 'b', Inclusivity(false, true))
@test string(interval) == "(a .. b]"
@test sprint(showcompact, interval) == string(interval)
@test sprint(show, interval, context=:compact=>true) == string(interval)
@test sprint(show, interval) == "Interval{Char}('a', 'b', Inclusivity(false, true))"

interval = Interval(Date(2012), Date(2013), Inclusivity(true, false))
@test string(interval) == "[2012-01-01 .. 2013-01-01)"
@test sprint(showcompact, interval) == string(interval)
@test sprint(show, interval, context=:compact=>true) == string(interval)
@test sprint(show, interval) ==
"Interval{Date}(2012-01-01, 2013-01-01, Inclusivity(true, false))"

interval = Interval("a", "b", Inclusivity(true, true))
@test string(interval) == "[a .. b]"
@test sprint(showcompact, interval) == string(interval)
@test sprint(show, interval, context=:compact=>true) == string(interval)
@test sprint(show, interval) ==
"Interval{String}(\"a\", \"b\", Inclusivity(true, true))"
end
Expand Down
Loading

0 comments on commit 3addff5

Please sign in to comment.