DateTime constructor that uses Date.value(x) as input



Hi all,

I’m probably overlooking something obvious, but I can’t work out how to construct a DateTime using as input the Int64 returned by Dates.value(). Note the following:

julia> dt = now()

julia> i = Dates.value(dt)

julia> dt2 = DateTime(i)

Obviously dt is not equal to dt2 since in the call DateTime(i), the i is being interpreted as years, rather than milliseconds since epoch.

So how do I construct a DateTime using i, such that dt will equal dt2? I can see how I could divide i up into years, months, days, hours, minutes, seconds, milliseconds, but I’m fairly sure there must be a better method than this, given that DateTime is literally just an immutable wrapper on i.

Note that DateTime(Base.Dates.UTInstant{Base.Dates.Millisecond}(i)) works, but also gives a deprecation warning which I found a bit confusing.

Incidentally, the reason why I might want to do this is so that I can write DateTime to a binary file, then read it back in later with minimum overhead.




You’ll want

julia> DateTime(Dates.UTInstant(Dates.Millisecond(i)))

or for short

julia> DateTime(Dates.UTM(i))

I agree this is pretty clunky and we should provide a better way to create a DateTime from the result of Dates.value .


Thanks for responding so quickly. DateTime(Dates.UTM(i)) is definitely un-clunky enough for me! :slight_smile:




We should at least improve the docs though :slight_smile: