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

dates

#1

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()
2017-11-21T15:38:01.282

julia> i = Dates.value(dt)
63646961881282

julia> dt2 = DateTime(i)
132444472-12-19T17:48:18.304

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.

Cheers,

Colin


#2

You’ll want

julia> DateTime(Dates.UTInstant(Dates.Millisecond(i)))
2017-11-21T15:38:01.282

or for short

julia> DateTime(Dates.UTM(i))
2017-11-21T15:38:01.282

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


#3

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

Cheers,

Colin


#4

We should at least improve the docs though :slight_smile: