Skip to content

Commit 586ce5d

Browse files
maximilian-gelbrechtgithub-actions[bot]mofeing
authored
Dates extension (#2540)
* initial draft of Dates extension * Update ext/ReactantDatesExt/arithmetic.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update ext/ReactantDatesExt/arithmetic.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update ext/ReactantDatesExt/arithmetic.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update ext/ReactantDatesExt/arithmetic.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update test/integration/dates.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update ext/ReactantDatesExt/arithmetic.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update test/integration/dates.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update test/integration/dates.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update test/integration/dates.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update test/integration/dates.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update ext/ReactantDatesExt/arithmetic.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update ext/ReactantDatesExt/arithmetic.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update ext/ReactantDatesExt/arithmetic.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update ext/ReactantDatesExt/arithmetic.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update ext/ReactantDatesExt/arithmetic.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update ext/ReactantDatesExt/conversions.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update ext/ReactantDatesExt/conversions.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update ext/ReactantDatesExt/conversions.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update test/integration/dates.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update test/integration/dates.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update test/integration/dates.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update test/integration/dates.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update test/integration/dates.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * basic io added and tests fixed * Update ext/ReactantDatesExt/types.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update ext/ReactantDatesExt/types.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update ext/ReactantDatesExt/types.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update ext/ReactantDatesExt/types.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * datetime2julia, timestepper test, imports slightly revised * Update ext/ReactantDatesExt/conversions.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update ext/ReactantDatesExt/reactant_bindings.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update test/integration/dates.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update ext/ReactantDatesExt/reactant_bindings.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update ext/ReactantDatesExt/reactant_bindings.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * more conversions added * Update ext/ReactantDatesExt/conversions.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update ext/ReactantDatesExt/conversions.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update test/integration/dates.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update test/integration/dates.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * change defintion order in test (issue #2582) * move functions/structs out of testset * dates compat 1.10 * jlfmt * less imports, more Dates.* * fix conversion bug due to different imports * add additional unit test for conversions * Update ext/ReactantDatesExt/conversions.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update ext/ReactantDatesExt/conversions.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update ext/ReactantDatesExt/conversions.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * adjust conversion which caused problem in Speedy * more converts for Speedy * add addtional cross conversions (as also needed by Speedy) * rename tracedR* to reactant* * no make_tracer anymore, tests adjusted * simpler IO, and reactive make_tracer for ArrayToConcrete mode * adjust timestepper test to rarray * Update test/integration/dates.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * only convert in `traced_type_inner` if mode is `ArrayToConcrete` * Update ext/ReactantDatesExt/reactant_bindings.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * more cross conversions for Speedy * track_numbers & test a little bit simplified * remove old todo comment from tests --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Sergio Sánchez Ramírez <[email protected]>
1 parent fba864d commit 586ce5d

10 files changed

Lines changed: 1259 additions & 0 deletions

File tree

Project.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ p7zip_jll = "3f19e933-33d8-53b3-aaab-bd5110c3b7a0"
4646
AbstractFFTs = "621f4979-c628-5d54-868e-fcf4e3e8185c"
4747
ArrayInterface = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"
4848
CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"
49+
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
4950
DLFP8Types = "f4c16678-4a16-415b-82ef-ed337c5d6c7c"
5051
FFTW = "7a1cc6ca-52ef-59f5-83cd-3a7055c09341"
5152
FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b"
@@ -75,6 +76,7 @@ ReactantCore = {path = "lib/ReactantCore"}
7576
ReactantAbstractFFTsExt = "AbstractFFTs"
7677
ReactantArrayInterfaceExt = "ArrayInterface"
7778
ReactantCUDAExt = ["CUDA", "Enzyme", "GPUCompiler", "KernelAbstractions", "LLVM"]
79+
ReactantDatesExt = "Dates"
7880
ReactantDLFP8TypesExt = "DLFP8Types"
7981
ReactantFFTWExt = ["FFTW", "AbstractFFTs", "LinearAlgebra"]
8082
ReactantFillArraysExt = "FillArrays"
@@ -104,6 +106,7 @@ CEnum = "0.5"
104106
CUDA = "5.9"
105107
Crayons = "4.1.1"
106108
DLFP8Types = "0.1"
109+
Dates = "1.10"
107110
Downloads = "1.6"
108111
EnumX = "1"
109112
Enzyme = "0.13.104"
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module ReactantDatesExt
2+
3+
using Dates:
4+
Dates,
5+
value,
6+
DatePeriod,
7+
TimePeriod,
8+
AbstractDateTime,
9+
TimeType,
10+
UTInstant,
11+
DateTime,
12+
Date,
13+
Time
14+
using Reactant: Reactant
15+
16+
include("types.jl")
17+
include("accessors.jl")
18+
include("io.jl")
19+
include("arithmetic.jl")
20+
include("conversions.jl")
21+
include("reactant_bindings.jl")
22+
end

ext/ReactantDatesExt/accessors.jl

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import Dates:
2+
year,
3+
quarter,
4+
month,
5+
week,
6+
day,
7+
hour,
8+
minute,
9+
second,
10+
millisecond,
11+
microsecond,
12+
nanosecond,
13+
yearmonth,
14+
monthday,
15+
yearmonthday,
16+
dayofmonth,
17+
days
18+
19+
# days accessor
20+
days(dt::ReactantDate) = value(dt)
21+
days(dt::ReactantDateTime) = fld(value(dt), 86400000)
22+
23+
# Calendar accessors dispatching on Reactant* TimeTypes
24+
year(dt::Union{ReactantDate,ReactantDateTime}) = Dates.year(days(dt))
25+
quarter(dt::Union{ReactantDate,ReactantDateTime}) = Dates.quarter(days(dt))
26+
month(dt::Union{ReactantDate,ReactantDateTime}) = Dates.month(days(dt))
27+
week(dt::Union{ReactantDate,ReactantDateTime}) = Dates.week(days(dt))
28+
day(dt::Union{ReactantDate,ReactantDateTime}) = Dates.day(days(dt))
29+
30+
# DateTime time-of-day accessors
31+
hour(dt::ReactantDateTime) = mod(fld(value(dt), 3600000), 24)
32+
minute(dt::ReactantDateTime) = mod(fld(value(dt), 60000), 60)
33+
second(dt::ReactantDateTime) = mod(fld(value(dt), 1000), 60)
34+
millisecond(dt::ReactantDateTime) = mod(value(dt), 1000)
35+
36+
# Time accessors
37+
hour(t::ReactantTime) = mod(fld(value(t), 3600000000000), Int64(24))
38+
minute(t::ReactantTime) = mod(fld(value(t), 60000000000), Int64(60))
39+
second(t::ReactantTime) = mod(fld(value(t), 1000000000), Int64(60))
40+
millisecond(t::ReactantTime) = mod(fld(value(t), Int64(1000000)), Int64(1000))
41+
microsecond(t::ReactantTime) = mod(fld(value(t), Int64(1000)), Int64(1000))
42+
nanosecond(t::ReactantTime) = mod(value(t), Int64(1000))
43+
44+
dayofmonth(dt::Union{ReactantDate,ReactantDateTime}) = day(dt)
45+
46+
# Compound accessors
47+
yearmonth(dt::Union{ReactantDate,ReactantDateTime}) = Dates.yearmonth(days(dt))
48+
monthday(dt::Union{ReactantDate,ReactantDateTime}) = Dates.monthday(days(dt))
49+
yearmonthday(dt::Union{ReactantDate,ReactantDateTime}) = Dates.yearmonthday(days(dt))

ext/ReactantDatesExt/arithmetic.jl

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
import Base: +, -, fld, mod, div
2+
import Dates:
3+
Period,
4+
yearmonthday,
5+
daysinmonth,
6+
hour,
7+
minute,
8+
second,
9+
millisecond,
10+
toms,
11+
tons,
12+
days,
13+
monthwrap,
14+
yearwrap,
15+
totaldays
16+
17+
# toms (to milliseconds) for traced period types
18+
toms(c::ReactantNanosecond) = div(value(c), 1000000, RoundNearest)
19+
toms(c::ReactantMicrosecond) = div(value(c), 1000, RoundNearest)
20+
toms(c::ReactantMillisecond) = value(c)
21+
toms(c::ReactantSecond) = 1000 * value(c)
22+
toms(c::ReactantMinute) = 60000 * value(c)
23+
toms(c::ReactantHour) = 3600000 * value(c)
24+
toms(c::ReactantDay) = 86400000 * value(c)
25+
toms(c::ReactantWeek) = 604800000 * value(c)
26+
27+
# tons (to nanoseconds) for traced period types
28+
tons(c::ReactantNanosecond) = value(c)
29+
tons(c::ReactantMicrosecond) = value(c) * 1000
30+
tons(c::ReactantMillisecond) = value(c) * 1000000
31+
tons(c::ReactantSecond) = value(c) * 1000000000
32+
tons(c::ReactantMinute) = value(c) * 60000000000
33+
tons(c::ReactantHour) = value(c) * 3600000000000
34+
35+
# days for traced period types
36+
days(c::ReactantDay) = value(c)
37+
days(c::ReactantWeek) = 7 * value(c)
38+
39+
# ReactantDateTime/ReactantDate-ReactantYear arithmetic
40+
function (+)(dt::ReactantDateTime, y::ReactantYear)
41+
oy, m, d = yearmonthday(dt)
42+
ny = oy + value(y)
43+
ld = daysinmonth(ny, m)
44+
return ReactantDateTime(
45+
UTInstant(
46+
ReactantMillisecond(
47+
value(dt) - toms(ReactantDay(totaldays(oy, m, d))) +
48+
toms(ReactantDay(totaldays(ny, m, d <= ld ? d : ld))),
49+
),
50+
),
51+
)
52+
end
53+
function (+)(dt::ReactantDate, y::ReactantYear)
54+
oy, m, d = yearmonthday(dt)
55+
ny = oy + value(y)
56+
ld = daysinmonth(ny, m)
57+
return ReactantDate(UTInstant(ReactantDay(totaldays(ny, m, d <= ld ? d : ld))))
58+
end
59+
function (-)(dt::ReactantDateTime, y::ReactantYear)
60+
oy, m, d = yearmonthday(dt)
61+
ny = oy - value(y)
62+
ld = daysinmonth(ny, m)
63+
return ReactantDateTime(
64+
UTInstant(
65+
ReactantMillisecond(
66+
value(dt) - toms(ReactantDay(totaldays(oy, m, d))) +
67+
toms(ReactantDay(totaldays(ny, m, d <= ld ? d : ld))),
68+
),
69+
),
70+
)
71+
end
72+
function (-)(dt::ReactantDate, y::ReactantYear)
73+
oy, m, d = yearmonthday(dt)
74+
ny = oy - value(y)
75+
ld = daysinmonth(ny, m)
76+
return ReactantDate(UTInstant(ReactantDay(totaldays(ny, m, d <= ld ? d : ld))))
77+
end
78+
79+
# ReactantDateTime/ReactantDate-ReactantMonth arithmetic
80+
function (+)(dt::ReactantDateTime, z::ReactantMonth)
81+
y, m, d = yearmonthday(dt)
82+
ny = yearwrap(y, m, value(z))
83+
mm = monthwrap(m, value(z))
84+
ld = daysinmonth(ny, mm)
85+
return ReactantDateTime(
86+
UTInstant(
87+
ReactantMillisecond(
88+
value(dt) - toms(ReactantDay(totaldays(y, m, d))) +
89+
toms(ReactantDay(totaldays(ny, mm, d <= ld ? d : ld))),
90+
),
91+
),
92+
)
93+
end
94+
function (+)(dt::ReactantDate, z::ReactantMonth)
95+
y, m, d = yearmonthday(dt)
96+
ny = yearwrap(y, m, value(z))
97+
mm = monthwrap(m, value(z))
98+
ld = daysinmonth(ny, mm)
99+
return ReactantDate(UTInstant(ReactantDay(totaldays(ny, mm, d <= ld ? d : ld))))
100+
end
101+
function (-)(dt::ReactantDateTime, z::ReactantMonth)
102+
y, m, d = yearmonthday(dt)
103+
ny = yearwrap(y, m, -value(z))
104+
mm = monthwrap(m, -value(z))
105+
ld = daysinmonth(ny, mm)
106+
return ReactantDateTime(
107+
UTInstant(
108+
ReactantMillisecond(
109+
value(dt) - toms(ReactantDay(totaldays(y, m, d))) +
110+
toms(ReactantDay(totaldays(ny, mm, d <= ld ? d : ld))),
111+
),
112+
),
113+
)
114+
end
115+
function (-)(dt::ReactantDate, z::ReactantMonth)
116+
y, m, d = yearmonthday(dt)
117+
ny = yearwrap(y, m, -value(z))
118+
mm = monthwrap(m, -value(z))
119+
ld = daysinmonth(ny, mm)
120+
return ReactantDate(UTInstant(ReactantDay(totaldays(ny, mm, d <= ld ? d : ld))))
121+
end
122+
123+
# ReactantDateTime/ReactantDate-ReactantQuarter arithmetic (delegates to Month)
124+
(+)(x::ReactantDate, y::ReactantQuarter) = x + ReactantMonth(3 * value(y))
125+
(-)(x::ReactantDate, y::ReactantQuarter) = x - ReactantMonth(3 * value(y))
126+
(+)(x::ReactantDateTime, y::ReactantQuarter) = x + ReactantMonth(3 * value(y))
127+
(-)(x::ReactantDateTime, y::ReactantQuarter) = x - ReactantMonth(3 * value(y))
128+
129+
# ReactantDate-ReactantWeek/ReactantDay arithmetic
130+
function (+)(x::ReactantDate, y::ReactantWeek)
131+
return ReactantDate(UTInstant(ReactantDay(value(x) + 7 * value(y))))
132+
end
133+
function (-)(x::ReactantDate, y::ReactantWeek)
134+
return ReactantDate(UTInstant(ReactantDay(value(x) - 7 * value(y))))
135+
end
136+
function (+)(x::ReactantDate, y::ReactantDay)
137+
return ReactantDate(UTInstant(ReactantDay(value(x) + value(y))))
138+
end
139+
function (-)(x::ReactantDate, y::ReactantDay)
140+
return ReactantDate(UTInstant(ReactantDay(value(x) - value(y))))
141+
end
142+
143+
# ReactantDateTime + any Period (via toms)
144+
function (+)(x::ReactantDateTime, y::Period)
145+
return ReactantDateTime(UTInstant(ReactantMillisecond(value(x) + toms(y))))
146+
end
147+
function (-)(x::ReactantDateTime, y::Period)
148+
return ReactantDateTime(UTInstant(ReactantMillisecond(value(x) - toms(y))))
149+
end
150+
151+
# ReactantTime + any TimePeriod (via tons)
152+
(+)(x::ReactantTime, y::TimePeriod) = ReactantTime(ReactantNanosecond(value(x) + tons(y)))
153+
(-)(x::ReactantTime, y::TimePeriod) = ReactantTime(ReactantNanosecond(value(x) - tons(y)))
154+
155+
# Commutativity
156+
(+)(y::Period, x::ReactantDateTime) = x + y
157+
(+)(y::Period, x::ReactantDate) = x + y
158+
(+)(y::TimePeriod, x::ReactantTime) = x + y

0 commit comments

Comments
 (0)