Build a DateTime from a Date and a Time


#1

Hi all,

I’d like to extend the DateTime constructor to accept DateTime(x::Date, y::Time). Can anyone think of anything more sensible than the following?

DateTime(x::Date, y::Time) = DateTime(x) + Millisecond(Int(round(Dates.value(y) / 1000000)))

I just convert the nanosecond from epoch to milliseconds from epoch and then add it to a DateTime that starts at the beginning of the day.

I’m not 100% convinced by round. Maybe floor or ceil would be more appropriate?

Would this be a sensible method to include in Dates? I would have thought it quite a common operation…

Cheers,

Colin


#2
using Dates
d = Date("2001-01-01")
t = Time("20:05")
dt = d + t

#3

Ah! Thank you! I thought it was strange that there wasn’t a constructor that took a Date and Time input. This explains why.

I must admit, I still feel like a DateTime constructor would be more natural than adding a Date and Time. A Date does not intrinsically have a “time” component. For example, today() + Hour(1) is an error, so why should today() + Time(now()) work? But I guess ultimately it is a judgment call, and I am by no means an expert at any of this.

Cheers and thanks,

Colin


#4

There are a few more constructors:

DateTime(2013, 7, 1, 12, 30, 59)
DateTime(Dates.Year(2013), Dates.Month(7), Dates.Day(1),
         Dates.Hour(12), Dates.Minute(23))

#5

Yes those ones are all in the official docs so I’m familiar with them. But I don’t think the Date + Time is in there (yet).

Cheers,

Colin