I have a time series with time recorded in a vector of floats in the format yyyy,mm,dd,HH,MM,SS.ssssss; i.e.:
times = [2017 09 06 00 00 04.911360]
This comes from the MATLAB datevec()
function, which outputs a vector of floats with fractional seconds.
When I work with these data in MATLAB, I often store dates as a float using the datenum()
function. In Julia, I want to convert these time vectors to something like DateTime
, or split into Date
and Time
in order to preserve microsecond accuracy. I could simply convert the date vectors into a float like datenum()
, but I like the idea of using Date
and Time
in order to easily (I hope) bin by time windows (e.g. separate a 24-hour time series into 10-minute bins).
Here’s what I have so far:
# sample data
times = [ 2017 09 06 00 00 04.911360;
2017 09 06 04 15 55.193727;
2017 09 06 22 55 12.256655]
# attempt to initialize Date and Time arrays
sdate = Date(times[1,1]):Day(1):Date(times[3,1])
stime = Time(times[1,4]):Second(1):Time(times[3,4])
for i = 1:size(times, 1)
years = times[i,1]
months = times[i,2]
days = times[i,3]
hours = times[i,4]
minutes = times[i,5]
seconds = floor(times[i,6])
millis = floor((times[i,6] - seconds)*1000)
micros = floor(((times[i,6] - seconds)*1000 - millis)*1000)
sdate[i] = Date(years, months, days)
stime[i] = Time(hours, minutes, seconds, millis, micros)
end
There are a number of problems here, including:
- I don’t know how to initialize
sdate
andstime
, so both the initializations and the loop fail. - Even if I get the initialization and assignment in the loop working, I have to tediously break out each argument of
Date
andTime
.
It would be great to be able do something like:
sdate = Date.(times[:,1:3])
stime = Time.(times[:,4:6])
sdatetime = DateTime.(times)
…but of course .
operates elementwise, so that doesn’t work. Any ideas?