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

Make ZonedDateTime parametric on timezone type #332

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
10 changes: 5 additions & 5 deletions src/arithmetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function Base.:(-)(zdt::ZonedDateTime, p::TimePeriod)
return ZonedDateTime(DateTime(zdt, UTC) - p, timezone(zdt); from_utc=true)
end

function broadcasted(::typeof(+), r::StepRange{ZonedDateTime}, p::DatePeriod)
function broadcasted(::typeof(+), r::StepRange{<:ZonedDateTime}, p::DatePeriod)
start, step, stop = first(r), Base.step(r), last(r)

# Since the local time + period can result in an invalid local datetime when working with
Expand All @@ -41,10 +41,10 @@ function broadcasted(::typeof(+), r::StepRange{ZonedDateTime}, p::DatePeriod)
return StepRange(start, step, stop)
end

function broadcasted(::typeof(+), r::StepRange{ZonedDateTime}, p::TimePeriod)
function broadcasted(::typeof(+), r::StepRange{<:ZonedDateTime}, p::TimePeriod)
return StepRange(r.start + p, r.step, r.stop + p)
end

broadcasted(::typeof(+), p::Period, r::StepRange{ZonedDateTime}) = broadcasted(+, r, p)
broadcasted(::typeof(-), r::StepRange{ZonedDateTime}, p::Period) = broadcasted(+, r, -p)
broadcasted(::typeof(-), p::Period, r::StepRange{ZonedDateTime}) = broadcasted(-, r, p)
broadcasted(::typeof(+), p::Period, r::StepRange{<:ZonedDateTime}) = broadcasted(+, r, p)
broadcasted(::typeof(-), r::StepRange{<:ZonedDateTime}, p::Period) = broadcasted(+, r, -p)
broadcasted(::typeof(-), p::Period, r::StepRange{<:ZonedDateTime}) = broadcasted(-, r, p)
26 changes: 12 additions & 14 deletions src/types/zoneddatetime.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,20 @@ using Dates: AbstractDateTime, argerror, validargs
# A `DateTime` that includes `TimeZone` information.
# """

struct ZonedDateTime <: AbstractDateTime
struct ZonedDateTime{T<:TimeZone} <: AbstractDateTime
utc_datetime::DateTime
timezone::TimeZone
timezone::T
zone::FixedTimeZone # The current zone for the utc_datetime.
end

function ZonedDateTime(utc_datetime::DateTime, timezone::TimeZone, zone::FixedTimeZone)
return new(utc_datetime, timezone, zone)
function ZonedDateTime(
utc_datetime::DateTime, timezone::VariableTimeZone, zone::FixedTimeZone
)
if timezone.cutoff !== nothing && utc_datetime >= timezone.cutoff
throw(UnhandledTimeError(timezone))
end

function ZonedDateTime(utc_datetime::DateTime, timezone::VariableTimeZone, zone::FixedTimeZone)
if timezone.cutoff !== nothing && utc_datetime >= timezone.cutoff
throw(UnhandledTimeError(timezone))
end

return new(utc_datetime, timezone, zone)
end
return ZonedDateTime{VariableTimeZone}(utc_datetime, timezone, zone)
end

"""
Expand Down Expand Up @@ -181,11 +179,11 @@ function Base.hash(zdt::ZonedDateTime, h::UInt)
return h
end

Base.typemin(::Type{ZonedDateTime}) = ZonedDateTime(typemin(DateTime), utc_tz; from_utc=true)
Base.typemax(::Type{ZonedDateTime}) = ZonedDateTime(typemax(DateTime), utc_tz; from_utc=true)
Base.typemin(::Type{<:ZonedDateTime}) = ZonedDateTime(typemin(DateTime), utc_tz; from_utc=true)
Base.typemax(::Type{<:ZonedDateTime}) = ZonedDateTime(typemax(DateTime), utc_tz; from_utc=true)

# Note: The `validargs` function is as part of the Dates parsing interface.
function Dates.validargs(::Type{ZonedDateTime}, y::Int64, m::Union{Int64, Int32}, d::Int64, h::Int64, mi::Int64, s::Int64, ms::Int64, tz::AbstractString)
function Dates.validargs(::Type{<:ZonedDateTime}, y::Int64, m::Union{Int64, Int32}, d::Int64, h::Int64, mi::Int64, s::Int64, ms::Int64, tz::AbstractString)
err = validargs(DateTime, y, Int64(m), d, h, mi, s, ms)
err === nothing || return err
istimezone(tz) || return argerror("TimeZone: \"$tz\" is not a recognized time zone")
Expand Down
6 changes: 3 additions & 3 deletions test/arithmetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ spring_zdt = ZonedDateTime(spring, warsaw)

# Arithmetic with a StepRange should always work even when the start/stop lands on
# ambiguous or non-existent DateTimes.
@testset "StepRange{ZonedDateTime}" begin
@testset "StepRange{<:ZonedDateTime}" begin
@testset "time-period" begin
dt = DateTime(2015, 6, 1)

Expand All @@ -71,7 +71,7 @@ spring_zdt = ZonedDateTime(spring, warsaw)
)
@test results == expected
@test length(results) == 2
@test results isa StepRange{ZonedDateTime}
@test results isa StepRange{<:ZonedDateTime}
end

@testset "date-period" begin
Expand All @@ -89,7 +89,7 @@ spring_zdt = ZonedDateTime(spring, warsaw)
)
@test results == expected
@test length(results) == 2
@test results isa StepRange{ZonedDateTime}
@test results isa StepRange{<:ZonedDateTime}
end

@testset "ambiguous" begin
Expand Down
3 changes: 1 addition & 2 deletions test/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ zdt = ZonedDateTime(dt, warsaw)
zdt_vector = [zdt]
@test sprint(show, MIME("text/plain"), zdt_vector) == summary(zdt_vector) * ":\n 1942-12-25T01:23:45+01:00"

prefix = VERSION >= v"1.5.0-DEV.224" ? "" : "ZonedDateTime"
@test sprint(show, zdt_vector; context=:compact => true) == "$prefix[ZonedDateTime(1942, 12, 25, 1, 23, 45, tz\"Europe/Warsaw\")]"
@test sprint(show, zdt_vector; context=:compact => true) == "ZonedDateTime{VariableTimeZone}[ZonedDateTime(1942, 12, 25, 1, 23, 45, tz\"Europe/Warsaw\")]"


# TimeZone parsing
Expand Down
14 changes: 14 additions & 0 deletions test/types/zoneddatetime.jl
Original file line number Diff line number Diff line change
Expand Up @@ -430,4 +430,18 @@ using Dates: Hour, Second, UTM, @dateformat_str
@test typemin(ZonedDateTime) <= ZonedDateTime(typemin(DateTime), utc)
@test typemax(ZonedDateTime) >= ZonedDateTime(typemax(DateTime), utc)
end

@testset "isbits(::ZonedDateTime)" begin
# https://github.com/JuliaTime/TimeZones.jl/issues/271

@testset "typeof($zone)" for zone in (tz"America/Winnipeg", FixedTimeZone("0123"))
# We are not isbits until we fix the timezone to also be isbits
zdt = ZonedDateTime(Date(2000), zone)
if isbits(zone)
@test isbits(zdt)
else
@test_broken isbits(zdt)
end
end
end
end