Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add astimezone(::Interval{ZonedDateTime}, ::TimeZone) #22

Merged
merged 1 commit into from
May 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/anchoredinterval.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import TimeZones: astimezone
using Base.Dates: value, coarserperiod

"""
Expand Down Expand Up @@ -264,3 +265,9 @@ function canonicalize(target_type::Type{<:Period}, p::P) where P <: Period
end

canonicalize(target_type::Type{P}, p::P) where P <: Period = p

##### TIME ZONES #####

function astimezone(i::AnchoredInterval{P, ZonedDateTime}, tz::TimeZone) where P
return AnchoredInterval{P, ZonedDateTime}(astimezone(anchor(i), tz), inclusivity(i))
end
8 changes: 8 additions & 0 deletions src/interval.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import TimeZones: astimezone

"""
Interval(first, last, [inclusivity::Inclusivity]) -> Interval
Interval(first, last, [closed_left::Bool, closed_right::Bool]) -> Interval
Expand Down Expand Up @@ -226,3 +228,9 @@ function Base.intersect(a::AbstractInterval{T}, b::AbstractInterval{T}) where T
left > right && return Interval{T}()
return Interval{T}(left.endpoint, right.endpoint, left.included, right.included)
end

##### TIME ZONES #####

function astimezone(i::Interval{ZonedDateTime}, tz::TimeZone)
return Interval(astimezone(first(i), tz), astimezone(last(i), tz), inclusivity(i))
end
18 changes: 18 additions & 0 deletions test/anchoredinterval.jl
Original file line number Diff line number Diff line change
Expand Up @@ -513,4 +513,22 @@ using Intervals: canonicalize
@test canonicalize(Year, Millisecond(2419200000)) == Week(4)
@test canonicalize(Year, Millisecond(4233600000)) == Week(7)
end

@testset "astimezone" begin
zdt = ZonedDateTime(2013, 2, 13, 0, 30, tz"America/Winnipeg")

for inclusivity in Inclusivity.(0:3)
for tz in (tz"America/Winnipeg", tz"America/Regina", tz"UTC")
@test isequal(
astimezone(HE(zdt, inclusivity), tz),
HE(astimezone(zdt, tz), inclusivity),
)

@test isequal(
astimezone(AnchoredInterval{Day(1)}(zdt, inclusivity), tz),
AnchoredInterval{Day(1)}(astimezone(zdt, tz), inclusivity),
)
end
end
end
end
14 changes: 14 additions & 0 deletions test/interval.jl
Original file line number Diff line number Diff line change
Expand Up @@ -419,4 +419,18 @@
end
end
end

@testset "astimezone" begin
zdt1 = ZonedDateTime(2013, 2, 13, 0, 30, tz"America/Winnipeg")
zdt2 = ZonedDateTime(2016, 8, 11, 21, tz"America/Winnipeg")

for inclusivity in Inclusivity.(0:3)
for tz in (tz"America/Winnipeg", tz"America/Regina", tz"UTC")
@test isequal(
astimezone(Interval(zdt1, zdt2, inclusivity), tz),
Interval(astimezone(zdt1, tz), astimezone(zdt2, tz), inclusivity),
)
end
end
end
end