From ebfb4bec4ddada5526d22986fdd1492df7c19754 Mon Sep 17 00:00:00 2001 From: Iblis Lin Date: Mon, 22 Oct 2018 01:33:56 +0800 Subject: [PATCH] Dates: add constructor DateTime(::Date, ::Time) --- stdlib/Dates/src/arithmetic.jl | 8 ++------ stdlib/Dates/src/types.jl | 27 ++++++++++++++++++++++++++- stdlib/Dates/test/arithmetic.jl | 10 ++++++++++ 3 files changed, 38 insertions(+), 7 deletions(-) diff --git a/stdlib/Dates/src/arithmetic.jl b/stdlib/Dates/src/arithmetic.jl index 6bccab9106194..0d0d68e25d8a2 100644 --- a/stdlib/Dates/src/arithmetic.jl +++ b/stdlib/Dates/src/arithmetic.jl @@ -16,12 +16,8 @@ The addition of a `Date` with a `Time` produces a `DateTime`. The hour, minute, the `Time` are used along with the year, month, and day of the `Date` to create the new `DateTime`. Non-zero microseconds or nanoseconds in the `Time` type will result in an `InexactError` being thrown. """ -function (+)(dt::Date, t::Time) - (microsecond(t) > 0 || nanosecond(t) > 0) && throw(InexactError(:+, DateTime, t)) - y, m, d = yearmonthday(dt) - return DateTime(y, m, d, hour(t), minute(t), second(t), millisecond(t)) -end -(+)(t::Time, dt::Date) = dt + t +(+)(dt::Date, t::Time) = DateTime(dt ,t) +(+)(t::Time, dt::Date) = DateTime(dt, t) # TimeType-Year arithmetic function (+)(dt::DateTime, y::Year) diff --git a/stdlib/Dates/src/types.jl b/stdlib/Dates/src/types.jl index c0202bd9171b3..24b3a6ebad1dc 100644 --- a/stdlib/Dates/src/types.jl +++ b/stdlib/Dates/src/types.jl @@ -240,7 +240,7 @@ function DateTime(y::Year, m::Month=Month(1), d::Day=Day(1), h::Hour=Hour(0), mi::Minute=Minute(0), s::Second=Second(0), ms::Millisecond=Millisecond(0)) return DateTime(value(y), value(m), value(d), - value(h), value(mi), value(s), value(ms)) + value(h), value(mi), value(s), value(ms)) end Date(y::Year, m::Month=Month(1), d::Day=Day(1)) = Date(value(y), value(m), value(d)) @@ -310,6 +310,31 @@ function Time(period::TimePeriod, periods::TimePeriod...) return Time(h, mi, s, ms, us, ns) end +# Convenience constructor for DateTime from Date and Time +""" + DateTime(d::Date, t::Time) + +Construct a `DateTime` type by `Date` and `Time`. +Non-zero microseconds or nanoseconds in the `Time` type will result in an +`InexactError`. + +```jldoctest +julia> d = Date(2018, 1, 1) +2018-01-01 + +julia> t = Time(8, 15, 42) +08:15:42 + +julia> DateTime(d, t) +2018-01-01T08:15:42 +``` +""" +function DateTime(dt::Date, t::Time) + (microsecond(t) > 0 || nanosecond(t) > 0) && throw(InexactError(:DateTime, DateTime, t)) + y, m, d = yearmonthday(dt) + return DateTime(y, m, d, hour(t), minute(t), second(t), millisecond(t)) +end + # Fallback constructors DateTime(y, m=1, d=1, h=0, mi=0, s=0, ms=0) = DateTime(Int64(y), Int64(m), Int64(d), Int64(h), Int64(mi), Int64(s), Int64(ms)) Date(y, m=1, d=1) = Date(Int64(y), Int64(m), Int64(d)) diff --git a/stdlib/Dates/test/arithmetic.jl b/stdlib/Dates/test/arithmetic.jl index ac27cfea1190d..87be3670224e0 100644 --- a/stdlib/Dates/test/arithmetic.jl +++ b/stdlib/Dates/test/arithmetic.jl @@ -299,6 +299,16 @@ end @test dt - Dates.Day(100) == Dates.Date(1999, 9, 18) @test dt - Dates.Day(1000) == Dates.Date(1997, 4, 1) end + @testset "Date-Time arithmetic" begin + dt = Dates.Date(1999, 12, 27) + @test dt + Dates.Time(0, 0, 0) == Dates.DateTime(1999, 12, 27, 0, 0, 0) + @test dt + Dates.Time(1, 0, 0) == Dates.DateTime(1999, 12, 27, 1, 0, 0) + @test dt + Dates.Time(0, 1, 0) == Dates.DateTime(1999, 12, 27, 0, 1, 0) + @test dt + Dates.Time(0, 0, 1) == Dates.DateTime(1999, 12, 27, 0, 0, 1) + + t = Dates.Time(0, 0, 0) + Dates.Hour(24) + @test dt + t == Dates.DateTime(1999, 12, 27, 0, 0, 0) + end end @testset "Time-TimePeriod arithmetic" begin t = Dates.Time(0)