felipenoris / XLSX.jl

Excel file reader and writer for the Julia language.
https://felipenoris.github.io/XLSX.jl/stable
Other
275 stars 58 forks source link

DateTime and Time data wrong #274

Open cxen opened 1 month ago

cxen commented 1 month ago

I have time data in this format 00:00:00. However, contrary to the expectations of XLSX.jl and Date.jl, my "Time" data is only measured in hours, so it will go to > 23:59:59, like 65:08:06. Unfortunately, the Time values over 24 h get converted to something like 1900-01-01T16:48:06.

I tried infer_eltypes true/false but it makes not difference.

Since I need time in minutes, I wrote a custom function to bypass this issue, but I am sure there must be a more elegant way to handle these time data.

function datetime_to_minutes(dt::DateTime)
    base_minutes = Dates.hour(dt) * 60 + Dates.minute(dt) + Dates.second(dt) / 60
    additional_minutes = (Dates.year(dt) - 1899 + 1) * 24 * 60
    return base_minutes + additional_minutes
end
hhaensel commented 1 week ago

The origin of your problem is Excel behaviour. If you enter 65:08:06 Excel will convert this to a DateTime with 1900-1-1 as reference value.

Julia provides many ways of converting DateTime, in particular it can substract to return the time difference. I'm not sure what your column returns if you have values <24h and >24. If it is always of DateTime but with different offsets, then I'd go for something like

function myminutes(t::DateTime)
    Minute(t - (t < DateTime(0, 1, 1) ? DateTime(0) : DateTime(1900))
end

If it is Time for t<24h and DateTime for t>24h then it's

function myminutes(t::DateTime)
    Minute(t - DateTime(1900))
end

function myminutes(t::Time)
    Minute(t)
end

If you need the value instead of the Minute type, just append `.value``

myminutes(t).value