|
| 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