Skip to content

Commit

Permalink
Merge pull request #29754 from iblis17/ib/datetime
Browse files Browse the repository at this point in the history
Dates: add constructor DateTime(::Date, ::Time)
  • Loading branch information
quinnj authored Oct 27, 2018
2 parents 27c27fc + ebfb4be commit ab25ae4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
8 changes: 2 additions & 6 deletions stdlib/Dates/src/arithmetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
27 changes: 26 additions & 1 deletion stdlib/Dates/src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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))
Expand Down
10 changes: 10 additions & 0 deletions stdlib/Dates/test/arithmetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

2 comments on commit ab25ae4

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

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

Executing the daily benchmark build, I will reply here when finished:

@nanosoldier runbenchmarks(ALL, isdaily = true)

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

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

Your benchmark job has completed - possible performance regressions were detected. A full report can be found here. cc @ararslan

Please sign in to comment.