Skip to content

Commit e7777c2

Browse files
authored
Merge pull request #7 from tpapp/pull-request/7e234fa3
added width (updated)
2 parents 9a4d3ea + 7e234fa commit e7777c2

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

src/IntervalSets.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module IntervalSets
77
using Base: @pure
88
import Base: eltype, convert, show, in, length, isempty, isequal, issubset, ==, hash, union, intersect, minimum, maximum
99

10-
export AbstractInterval, ClosedInterval, , .., ±, ordered
10+
export AbstractInterval, ClosedInterval, , .., ±, ordered, width
1111

1212
abstract AbstractInterval{T}
1313

src/closed.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,8 @@ end
7171
issubset(A::ClosedInterval, B::ClosedInterval) = ((A.left in B) && (A.right in B)) || isempty(A)
7272

7373
(A::ClosedInterval, B::ClosedInterval) = issubset(B, A)
74+
75+
function width{T}(A::ClosedInterval{T})
76+
_width = A.right - A.left
77+
max(zero(_width), _width) # this works when T is a Date
78+
end

test/runtests.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,14 @@ using Base.Test
7373
@test I ClosedInterval(1, 2)
7474

7575
@test hash(1..3) == hash(1.0..3.0)
76+
77+
let A = Date(1990, 1, 1), B = Date(1990, 3, 1)
78+
@test width(ClosedInterval(A, B)) == Base.Dates.Day(59)
79+
@test width(ClosedInterval(B, A)) == Base.Dates.Day(0)
80+
@test isempty(ClosedInterval(B, A))
81+
end
82+
83+
@test width(ClosedInterval(3,7)) 4
84+
@test width(ClosedInterval(4.0,8.0)) 4.0
7685
end
7786
end

0 commit comments

Comments
 (0)