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

Draft for updating conversions #224

Open
wants to merge 2 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
34 changes: 9 additions & 25 deletions src/conversions.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Dates: unix2datetime, datetime2unix, julian2datetime, datetime2julian
import Dates: unix2datetime, datetime2unix, julian2datetime, datetime2julian
using Mocking: Mocking, @mock

# UTC is an abstract type defined in Dates, for some reason
Expand Down Expand Up @@ -89,34 +89,18 @@ function astimezone(zdt::ZonedDateTime, tz::FixedTimeZone)
return ZonedDateTime(zdt.utc_datetime, tz, tz)
end

function zdt2julian(zdt::ZonedDateTime)
datetime2julian(utc(zdt))
end
datetime2julian(zdt::ZonedDateTime) = datetime2julian(utc(zdt))
Copy link
Member

Choose a reason for hiding this comment

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

My preference would be to extend via:

Suggested change
datetime2julian(zdt::ZonedDateTime) = datetime2julian(utc(zdt))
Dates.datetime2julian(zdt::ZonedDateTime) = datetime2julian(utc(zdt))

The rest of this package extends this way so I'd like to keep it consistent


function zdt2julian(::Type{T}, zdt::ZonedDateTime) where T<:Integer
floor(T, datetime2julian(utc(zdt)))
end
Copy link
Member

Choose a reason for hiding this comment

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

Deprecations need to be added for all of the removed functions

datetime2julian(::Type{T}, zdt::ZonedDateTime) where T<:Integer = floor(T, datetime2julian(utc(zdt)))

function zdt2julian(::Type{T}, zdt::ZonedDateTime) where T<:Real
convert(T, datetime2julian(utc(zdt)))
end
datetime2julian(::Type{T}, zdt::ZonedDateTime) where T<:Real = convert(T, datetime2julian(utc(zdt)))

function julian2zdt(jd::Real)
ZonedDateTime(julian2datetime(jd), utc_tz, from_utc=true)
end
julian2datetime(::Type{<:ZonedDateTime}, jd::Real) = ZonedDateTime(julian2datetime(jd), utc_tz, from_utc=true)

function zdt2unix(zdt::ZonedDateTime)
datetime2unix(utc(zdt))
end
datetime2unix(zdt::ZonedDateTime) = datetime2unix(utc(zdt))

function zdt2unix(::Type{T}, zdt::ZonedDateTime) where T<:Integer
floor(T, datetime2unix(utc(zdt)))
end
datetime2unix(::Type{T}, zdt::ZonedDateTime) where T<:Integer = floor(T, datetime2unix(utc(zdt)))

function zdt2unix(::Type{T}, zdt::ZonedDateTime) where T<:Real
convert(T, datetime2unix(utc(zdt)))
end
datetime2unix(::Type{T}, zdt::ZonedDateTime) where T<:Real = convert(T, datetime2unix(utc(zdt)))

function unix2zdt(seconds::Real)
ZonedDateTime(unix2datetime(seconds), utc_tz, from_utc=true)
end
unix2datetime(::Type{<:ZonedDateTime}, seconds::Real) = ZonedDateTime(unix2datetime(seconds), utc_tz, from_utc=true)
Copy link
Member

Choose a reason for hiding this comment

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

It would be good to keep the line length to 92

42 changes: 21 additions & 21 deletions test/conversions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ zdt = ZonedDateTime(dt, warsaw)
# Vectorized accessors
n = 10
arr = fill(zdt, n)
@test Dates.DateTime.(arr) == fill(dt, n)
@test DateTime.(arr) == fill(dt, n)

# now function
dt = now(Dates.UTC)::DateTime
zdt = now(warsaw)
@test zdt.timezone == warsaw
@test Dates.datetime2unix(TimeZones.utc(zdt)) ≈ Dates.datetime2unix(dt)
@test datetime2unix(TimeZones.utc(zdt)) ≈ datetime2unix(dt)

# today function
@test abs(today() - today(warsaw)) <= Dates.Day(1)
Expand Down Expand Up @@ -56,47 +56,47 @@ zdt_warsaw = ZonedDateTime(dt, warsaw; from_utc=true)
@test astimezone(zdt_warsaw, utc) === zdt_utc

# ZonedDateTime to Unix timestamp (and vice versa)
@test TimeZones.zdt2unix(ZonedDateTime(1970, utc)) == 0
@test TimeZones.unix2zdt(0) == ZonedDateTime(1970, utc)
@test datetime2unix(ZonedDateTime(1970, utc)) == 0
@test unix2datetime(ZonedDateTime, 0) == ZonedDateTime(1970, utc)

for dt in (DateTime(2013, 2, 13), DateTime(2016, 8, 11))
local dt
local zdt = ZonedDateTime(dt, warsaw)
offset = TimeZones.value(zdt.zone.offset) # Total offset in seconds
@test TimeZones.zdt2unix(zdt) == datetime2unix(dt) - offset
@test datetime2unix(zdt) == datetime2unix(dt) - offset
end

@test isa(TimeZones.zdt2unix(ZonedDateTime(1970, utc)), Float64)
@test isa(TimeZones.zdt2unix(Float32, ZonedDateTime(1970, utc)), Float32)
@test isa(TimeZones.zdt2unix(Int64, ZonedDateTime(1970, utc)), Int64)
@test isa(TimeZones.zdt2unix(Int32, ZonedDateTime(1970, utc)), Int32)
@test isa(datetime2unix(ZonedDateTime(1970, utc)), Float64)
@test isa(datetime2unix(Float32, ZonedDateTime(1970, utc)), Float32)
@test isa(datetime2unix(Int64, ZonedDateTime(1970, utc)), Int64)
@test isa(datetime2unix(Int32, ZonedDateTime(1970, utc)), Int32)

@test TimeZones.zdt2unix(ZonedDateTime(1970, 1, 1, 0, 0, 0, 750, utc)) == 0.75
@test TimeZones.zdt2unix(Float32, ZonedDateTime(1970, 1, 1, 0, 0, 0, 750, utc)) == 0.75
@test TimeZones.zdt2unix(Int64, ZonedDateTime(1970, 1, 1, 0, 0, 0, 750, utc)) == 0
@test TimeZones.zdt2unix(Int32, ZonedDateTime(1970, 1, 1, 0, 0, 0, 750, utc)) == 0
@test datetime2unix(ZonedDateTime(1970, 1, 1, 0, 0, 0, 750, utc)) == 0.75
@test datetime2unix(Float32, ZonedDateTime(1970, 1, 1, 0, 0, 0, 750, utc)) == 0.75
@test datetime2unix(Int64, ZonedDateTime(1970, 1, 1, 0, 0, 0, 750, utc)) == 0
@test datetime2unix(Int32, ZonedDateTime(1970, 1, 1, 0, 0, 0, 750, utc)) == 0

# round-trip
zdt = ZonedDateTime(2010, 1, 2, 3, 4, 5, 999, utc)
round_trip = TimeZones.unix2zdt(TimeZones.zdt2unix(zdt))
round_trip = unix2datetime(ZonedDateTime, datetime2unix(zdt))
@test round_trip == zdt

# millisecond loss
zdt = ZonedDateTime(2010, 1, 2, 3, 4, 5, 999, utc)
round_trip = TimeZones.unix2zdt(TimeZones.zdt2unix(Int64, zdt))
round_trip = unix2datetime(ZonedDateTime, datetime2unix(Int64, zdt))
@test round_trip != zdt
@test round_trip == floor(zdt, Dates.Second(1))

# timezone loss
zdt = ZonedDateTime(2010, 1, 2, 3, 4, 5, warsaw)
round_trip = TimeZones.unix2zdt(TimeZones.zdt2unix(Int64, zdt))
round_trip = unix2datetime(ZonedDateTime, datetime2unix(Int64, zdt))
@test round_trip == zdt
@test timezone(round_trip) != timezone(zdt)

# Julian dates
jd = 2457241.855
jd_zdt = ZonedDateTime(Dates.julian2datetime(jd), warsaw, from_utc=true)
@test TimeZones.zdt2julian(jd_zdt) == jd
@test TimeZones.zdt2julian(Int, jd_zdt) === floor(Int, jd)
@test TimeZones.zdt2julian(Float64, jd_zdt) === jd
@test TimeZones.julian2zdt(jd) == jd_zdt
jd_zdt = ZonedDateTime(julian2datetime(jd), warsaw, from_utc=true)
@test datetime2julian(jd_zdt) == jd
@test datetime2julian(Int, jd_zdt) === floor(Int, jd)
@test datetime2julian(Float64, jd_zdt) === jd
@test julian2datetime(ZonedDateTime, jd) == jd_zdt