Skip to content

Commit

Permalink
Merge pull request #211 from invenia/rf/utcdatetimes
Browse files Browse the repository at this point in the history
Loosen type signatures to support UTCDateTimes
  • Loading branch information
rofinn authored Jan 25, 2023
2 parents 897bd75 + 4fff628 commit ba938f6
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name = "Intervals"
uuid = "d8418881-c3e1-53bb-8760-2df7ec849ed5"
license = "MIT"
authors = ["Invenia Technical Computing"]
version = "1.8.0"
version = "1.9.0"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Expand Down
4 changes: 2 additions & 2 deletions src/anchoredinterval.jl
Original file line number Diff line number Diff line change
Expand Up @@ -386,8 +386,8 @@ canonicalize(target_type::Type{P}, p::P) where P <: Period = p

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

function TimeZones.astimezone(i::AnchoredInterval{P, ZonedDateTime, L, R}, tz::TimeZone) where {P,L,R}
function TimeZones.astimezone(i::AnchoredInterval{P, T, L, R}, tz::TimeZone) where {P,T,L,R}
return AnchoredInterval{P, ZonedDateTime, L, R}(astimezone(anchor(i), tz))
end

TimeZones.timezone(i::AnchoredInterval{P, ZonedDateTime}) where P = timezone(anchor(i))
TimeZones.timezone(i::AnchoredInterval) = timezone(anchor(i))
4 changes: 2 additions & 2 deletions src/interval.jl
Original file line number Diff line number Diff line change
Expand Up @@ -480,11 +480,11 @@ end

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

function TimeZones.astimezone(i::Interval{ZonedDateTime, L, R}, tz::TimeZone) where {L,R}
function TimeZones.astimezone(i::Interval{T, L, R}, tz::TimeZone) where {T, L,R}
return Interval{ZonedDateTime, L, R}(astimezone(first(i), tz), astimezone(last(i), tz))
end

function TimeZones.timezone(i::Interval{ZonedDateTime})
function TimeZones.timezone(i::Interval)
if timezone(first(i)) != timezone(last(i))
throw(ArgumentError("Interval $i contains mixed timezones."))
end
Expand Down
2 changes: 2 additions & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b"
StableRNGs = "860ef19b-820b-49d6-a774-d7a799459cd3"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
TimeZones = "f269a46b-ccf7-5d73-abea-4c690281aa53"
UTCDateTimes = "0f7cfa37-7abf-4834-b969-a8aa512401c2"
VisualRegressionTests = "34922c18-7c2a-561c-bac1-01e79b2c4c92"

[compat]
Documenter = "0.23, 0.24, 0.25, 0.26, 0.27"
Infinity = "0.2.3"
UTCDateTimes = "1.1"
14 changes: 12 additions & 2 deletions test/anchoredinterval.jl
Original file line number Diff line number Diff line change
Expand Up @@ -744,24 +744,34 @@ using Intervals: Bounded, Ending, Beginning, canonicalize, isunbounded

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

for tz in (tz"America/Winnipeg", tz"America/Regina", tz"UTC")
# Note: We cannot test different bound types here as HE cannot specify them
@test isequal(astimezone(HE(zdt), tz), HE(astimezone(zdt, tz)))
@test isequal(astimezone(HE(utcdt), tz), HE(astimezone(utcdt, tz)))

for (L, R) in BOUND_PERMUTATIONS
@test isequal(
astimezone(AnchoredInterval{Day(1), L, R}(zdt), tz),
AnchoredInterval{Day(1), L, R}(astimezone(zdt, tz)),
)
@test isequal(
astimezone(AnchoredInterval{Day(1), L, R}(utcdt), tz),
AnchoredInterval{Day(1), L, R}(astimezone(utcdt, tz)),
)
end
end
end

@testset "timezone" begin
zdt = ZonedDateTime(2013, 2, 13, 0, 30, tz"America/Winnipeg")
ai = AnchoredInterval{Day(1)}(zdt)
@test timezone(ai) == tz"America/Winnipeg"
utcdt = UTCDateTime(zdt)
aiz = AnchoredInterval{Day(1)}(zdt)
aiutc = AnchoredInterval{Day(1)}(utcdt)

@test timezone(aiz) == tz"America/Winnipeg"
@test timezone(aiutc) == tz"UTC"
end

@testset "legacy deserialization" begin
Expand Down
12 changes: 12 additions & 0 deletions test/interval.jl
Original file line number Diff line number Diff line change
Expand Up @@ -694,13 +694,19 @@
@testset "astimezone" begin
zdt1 = ZonedDateTime(2013, 2, 13, 0, 30, tz"America/Winnipeg")
zdt2 = ZonedDateTime(2016, 8, 11, 21, tz"America/Winnipeg")
utcdt1 = UTCDateTime(zdt1)
utcdt2 = UTCDateTime(zdt2)

for (L, R) in BOUND_PERMUTATIONS
for tz in (tz"America/Winnipeg", tz"America/Regina", tz"UTC")
@test isequal(
astimezone(Interval{L, R}(zdt1, zdt2), tz),
Interval{L, R}(astimezone(zdt1, tz), astimezone(zdt2, tz)),
)
@test isequal(
astimezone(Interval{L, R}(utcdt1, utcdt2), tz),
Interval{L, R}(astimezone(utcdt1, tz), astimezone(utcdt2, tz)),
)
end
end
end
Expand All @@ -717,6 +723,12 @@
zdt2 = ZonedDateTime(2016, 8, 11, 21, tz"Europe/London")
@test_throws ArgumentError timezone(Interval(zdt1, zdt2))
end

@testset "utc" begin
utcdt1 = UTCDateTime(2013, 2, 13, 0, 30)
utcdt2 = UTCDateTime(2016, 8, 11, 21)
@test timezone(Interval(utcdt1, utcdt2)) == tz"UTC"
end
end

@testset "merge" begin
Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ using Intervals: isfinite
using Serialization: deserialize
using Test
using TimeZones
using UTCDateTimes

const BOUND_PERMUTATIONS = product((Closed, Open), (Closed, Open))

Expand Down

2 comments on commit ba938f6

@rofinn
Copy link
Member Author

@rofinn rofinn commented on ba938f6 Jan 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/76393

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.9.0 -m "<description of version>" ba938f6b45e4dae267434fd6f3d41008eeccbf84
git push origin v1.9.0

Please sign in to comment.