Why Does Julia Have Two Different Epoch Starts?

julia> dt = DateTime(2004, 12, 31, 17, 30, 1)
2004-12-31T17:30:01

julia> convert( Int, dt )
63240197401000

julia> ep1= convert( Int, dt )
63240197401000

julia> ep2= Dates.datetime2epochms( dt )
63271733401000

julia> (ep1-ep2) /1000/60/60/24
-365.0

They differ by 1 year. One starts at 0001, the other at 0000. It’s somewhat confusing and easy to get wrong.

julia> Dates.epochms2datetime( convert( Int, Dates.now() ) )
2017-03-09T15:14:27.272

Ahem…mea culpa.

1 Like

Which version of Julia are you using? You should have got this warning on v0.6.2:

julia> convert(Int, Dates.now())
WARNING: convert(::Type{R}, x::Dates.DateTime) where R <: Real is deprecated, use R(Dates.value(x)) instead.
Stacktrace:
 [1] depwarn(::String, ::Symbol) at ./deprecated.jl:70
 [2] convert(::Type{Int64}, ::DateTime) at ./deprecated.jl:57
 [3] eval(::Module, ::Any) at ./boot.jl:235
 [4] eval_user_input(::Any, ::Base.REPL.REPLBackend) at ./REPL.jl:66
 [5] macro expansion at /home/tamas/.julia/v0.6/Revise/src/Revise.jl:775 [inlined]
 [6] (::Revise.##17#18{Base.REPL.REPLBackend})() at ./event.jl:73
while loading no file, in expression starting on line 0
63656357005203

The lesson is that you should use the API provided for the purpose. There is literally nothing that suggests that convert(::Type{Int}, ::Date) should be used for calculating \Delta from the epoch.

1 Like

yes, I did on the first try. I checked and then forgot about it. mea culpa.

julia> Dates.value(Dates.now()) - convert(Int, Dates.now())
0

the real issue is that two julia epoch ms seem like a bad idea to me, and the source for needless bugs and confusion in the future. if it were me, I would deprecate one of them. I did not file this as an issue, because I do not know whether there is another reasoning here that I am unaware of.

Yes, it’s quite understandable that you’d be confused here. Tamas’ message reads a little overly harshly to me — the fact that we had this inconsistently defined was our fault, not yours. But he’s also demonstrating that on 0.6.2, that conversion is indeed deprecated. I highly recommend you update to version 0.6.

On master this trap is gone entirely.

1 Like

version 0.6.2:

julia> dt= Dates.now()
2018-03-10T09:40:53.757

julia> ep1, ep2= Dates.value( dt ), Dates.datetime2epochms( dt )
(63656358053757, 63687894053757)

julia> (ep2-ep1) /1000/60/60/24
365.0

I will file an issue. regards, /iaw

1 Like