But, is this really what’s intended? I thought there should be a more elegant solution.
What is the standard way to express “t1 + onehour/r” for a general real number r ? It seems to me that we need a proper “time interval type” which works seamlessly with DateTime.
Yeah, float-based time intervals are often more convenient than integers. Julia doesn’t have them in stdlib, unfortunately.
See (my)DateFormats.jl package that helps dealing with different date/time formats, including decimal periods:
Note that you can convert from lower to higher precision time periods where the answer is always the same (so you can do Millisecond(Hour(1)) but not Day(Month(1))). This just skips the need for your dummy variable:
using Dates
hr = Dates.Hour(1)
t1 = DateTime(2023, 1, 3)
r = 7.4
t1 + Millisecond(round(Millisecond(hr).value/r))
This does seem annoying. But the reason all sorts of fractional arithmetic doesn’t just work is because
Period types represent discrete, human representations of time.
as opposed to physical durations of time. The lubridate cheatsheet for R is good for seeing how complex things need to be to support date arithmetic fully.
That also works, and was indeed my message before I edited it. I recently noticed that multiplication/division is more natural for this operation than the period_decimal function, and made a new DateFormats version with *ₜ and /ₜ operators. This message prompted me to actually release the new version, so I updated the message on discourse.
Meaning of 1/7.4 *ₜ Hour is more obvious than period_decimal(Hour, 1/7.4), so I suggest using this operator.
You seem to ignore the context of this discussion: It seems to me that you lift my words out of the context and propose a solution that doesn’t work in the present context. The following code doesn’t work:
using Dates
using Unitful
del = 1u"hr" + 5u"minute" + 2u"s"
t1 = DateTime(2023, 1, 29)
t2 = t1 + del/5 # MethodError: no method matching +(::DateTime, . . .
From the context of this discussion, it’s clear that I’m wanting a method to handle physical duration of time together with DateTime. (But, it would be nice if Dates worked with the time in the Unitful package.)
I know that! Yours isn’t my “desired code”. Again you lift my example out of the context. The code you wrote stops working once we introduce division:
Wanting a time interval type that allows for usual arithmetics as if it were a floating-point number together with DateTime, is the starting point of this discussion!