From 0e042476f1214db88d2651c856efcb27153f1f7d Mon Sep 17 00:00:00 2001 From: Jacob Quinn Date: Sat, 23 Jan 2016 11:36:50 -0700 Subject: [PATCH] Allow conversions to/from Real for TimeTypes. Also add documentation and tests. --- base/dates/conversions.jl | 24 +++++++++++++++++++++++- test/dates/conversions.jl | 15 +++++++++++++++ test/dates/types.jl | 8 +------- 3 files changed, 39 insertions(+), 8 deletions(-) diff --git a/base/dates/conversions.jl b/base/dates/conversions.jl index 7b5d8076ba235..fe39b4c2ff630 100644 --- a/base/dates/conversions.jl +++ b/base/dates/conversions.jl @@ -5,8 +5,30 @@ Date(dt::TimeType) = convert(Date,dt) DateTime(dt::TimeType) = convert(DateTime,dt) Base.convert(::Type{DateTime},dt::Date) = DateTime(UTM(value(dt)*86400000)) Base.convert(::Type{Date},dt::DateTime) = Date(UTD(days(dt))) +""" + convert{T<:Real}(::Type{T}, dt::DateTime) -> T +Converts a DateTime value `dt` to a number of type `T`. The returned value corresponds to the number of Rata Die milliseconds since epoch. +See `convert(DateTime, x::Real)` for inverse. +""" Base.convert{R<:Real}(::Type{R},x::DateTime) = convert(R,value(x)) +""" + convert{T<:Real}(::Type{T}, dt::Date) -> T +Converts a Date value `dt` to a number of type `T`. The returned value corresponds to the number of Rata Die days since epoch. +See `convert(Date, x::Real)` for inverse. +""" Base.convert{R<:Real}(::Type{R},x::Date) = convert(R,value(x)) +""" + convert{T<:Real}(::Type{DateTime}, x::T) -> DateTime +Converts a number of type `T` to a DateTime. `x` should be the number of Rata Die milliseconds since epoch. +See `convert(Int64,dt::DateTime)` for inverse. +""" +Base.convert{R<:Real}(::Type{DateTime}, x::R) = DateTime(UTM(x)) +""" + convert{T<:Real}(::Type{Date}, x::T) -> Date +Converts a number of type `T` to a Date. `x` should be the number of Rata Die days since epoch. +See `convert(Int64,dt::Date)` for inverse. +""" +Base.convert{R<:Real}(::Type{Date}, x::R) = Date(UTD(x)) @vectorize_1arg DateTime Date @vectorize_1arg Date DateTime @@ -71,7 +93,7 @@ rata2datetime(days) = DateTime(yearmonthday(days)...) Returns the number of Rata Die days since epoch from the given `Date` or `DateTime`. """ -datetime2rata(dt::DateTime) = days(dt) +datetime2rata(dt::TimeType) = days(dt) # Julian conversions const JULIANEPOCH = value(DateTime(-4713,11,24,12)) diff --git a/test/dates/conversions.jl b/test/dates/conversions.jl index 649ebfff7e9b7..630207f4de063 100644 --- a/test/dates/conversions.jl +++ b/test/dates/conversions.jl @@ -85,3 +85,18 @@ let dt = DateTime(1915,1,1,12) @test Dates.unix2datetime(unix) == dt @test Dates.julian2datetime(julian) == dt end + +# Conversions to/from numbers +a = Dates.DateTime(2000) +b = Dates.Date(2000) +@test convert(Real,b) == 730120 +@test convert(Float64,b) == 730120.0 +@test convert(Int32,b) == 730120 +@test convert(Real,a) == 63082368000000 +@test convert(Float64,a) == 63082368000000.0 +@test convert(Int64,a) == 63082368000000 +@test convert(DateTime,63082368000000) == a +@test convert(DateTime,63082368000000.0) == a +@test convert(Date,730120) == b +@test convert(Date,730120.0) == b +@test convert(Date,Int32(730120)) == b diff --git a/test/dates/types.jl b/test/dates/types.jl index 6f9ec94fa9edc..42446fa614404 100644 --- a/test/dates/types.jl +++ b/test/dates/types.jl @@ -114,12 +114,7 @@ b = Dates.Date(2000) @test typemax(Dates.DateTime) - typemin(Dates.DateTime) == Dates.Millisecond(9223372017043199000) @test string(typemax(Dates.Date)) == "252522163911149-12-31" @test string(typemin(Dates.Date)) == "-252522163911150-01-01" -@test convert(Real,b) == 730120 -@test convert(Float64,b) == 730120.0 -@test convert(Int32,b) == 730120 -@test convert(Real,a) == 63082368000000 -@test convert(Float64,a) == 63082368000000.0 -@test convert(Int64,a) == 63082368000000 + # Date-DateTime conversion/promotion @test Dates.DateTime(a) == a @test Dates.Date(a) == b @@ -173,4 +168,3 @@ ms = Dates.Millisecond(1) @test Dates.Date(d,y) == Dates.Date(1,1,1) @test Dates.Date(d,m) == Dates.Date(1,1,1) @test Dates.Date(m,y) == Dates.Date(1,1,1) -