diff --git a/src/conversions.jl b/src/conversions.jl index 3f2a53daa..499ef1bfd 100644 --- a/src/conversions.jl +++ b/src/conversions.jl @@ -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 @@ -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)) -function zdt2julian(::Type{T}, zdt::ZonedDateTime) where T<:Integer - floor(T, datetime2julian(utc(zdt))) -end +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) diff --git a/test/conversions.jl b/test/conversions.jl index 96fbd1dd3..e1718c2cc 100644 --- a/test/conversions.jl +++ b/test/conversions.jl @@ -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) @@ -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