Skip to content

Commit

Permalink
Dates: add constructor DateTime(::Date, ::Time)
Browse files Browse the repository at this point in the history
  • Loading branch information
iblislin committed Oct 21, 2018
1 parent 1db6047 commit 3588bf8
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 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,26 @@ 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`.
```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
```
"""
DateTime(d::Date, t::Time) =
DateTime(UTM(value(d) * 86400000 + value(t) / 1000000))

# 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

0 comments on commit 3588bf8

Please sign in to comment.