Constructing dates from a vector of numbers?

It can be written shorter in this way

using Dates
import Dates: DateTime

function Dates.DateTime(x::Float64, origin = DateTime(0))
    time = Millisecond(Int(floor(Millisecond(Day(1)).value * x)))
    return origin + time
end

with the result

julia> dt = DateTime(2020,10,1)
2020-10-01T00:00:00

julia> x = range(0,10,length=50)
0.0:0.20408163265306123:10.0

julia> DateTime.(x, dt)
50-element Array{DateTime,1}:
 2020-10-01T00:00:00
 2020-10-01T04:53:52.653
 2020-10-01T09:47:45.306
 2020-10-01T14:41:37.959

On the other hand, you can remove origin and convert time periods to time periods and adding origin later

function timeperiod(x::Float64)
    Millisecond(Int(floor(Millisecond(Day(1)).value * x)))
end

and

julia> timeperiod.(x)
50-element Array{Dates.CompoundPeriod,1}:
 empty period
 17632653 milliseconds
 35265306 milliseconds
 52897959 milliseconds
 70530612 milliseconds
 88163265 milliseconds
 105795918 milliseconds

julia> dt + timeperiod.(x)
50-element Array{DateTime,1}:
 2020-10-01T00:00:00
 2020-10-01T04:53:52.653
 2020-10-01T09:47:45.306
 2020-10-01T14:41:37.959
 2020-10-01T19:35:30.612
 2020-10-02T00:29:23.265
 2020-10-02T05:23:15.918
4 Likes