Skip to content

Commit

Permalink
Allow conversions to/from Real for TimeTypes. Also add documentation …
Browse files Browse the repository at this point in the history
…and tests.
  • Loading branch information
quinnj committed Jan 23, 2016
1 parent 3a421c9 commit 0e04247
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
24 changes: 23 additions & 1 deletion base/dates/conversions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down
15 changes: 15 additions & 0 deletions test/dates/conversions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 1 addition & 7 deletions test/dates/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

0 comments on commit 0e04247

Please sign in to comment.