Skip to content

Commit db86e67

Browse files
Use chooseOpsMethod to get Date-IDate correct
1 parent 294f7f2 commit db86e67

File tree

4 files changed

+16
-1
lines changed

4 files changed

+16
-1
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ if (getRversion() >= "3.6.0") {
155155
export(as.IDate,as.ITime,IDateTime)
156156
export(second,minute,hour,yday,wday,mday,week,isoweek,isoyear,month,quarter,year,yearmon,yearqtr)
157157

158+
S3method(chooseOpsMethod, IDate)
158159
S3method("[", ITime)
159160
S3method("+", IDate)
160161
S3method("-", IDate)

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@
103103
104104
16. `between()` is now more robust with `integer64` arguments. Combining small integer `x` with certain large `integer64` bounds no longer misinterprets the bounds as `double`; if a `double` bound cannot be losslessly converted into `integer64` for comparison with `integer64` `x`, an error is signalled instead of returning a wrong answer with a warning; [#7164](https://github.com/Rdatatable/data.table/issues/7164). Thanks @aitap for the bug report and the fix.
105105
106+
17. `t1 - t2`, where one is an `IDate` and the other is a `Date`, are now consistent with the case where both are `IDate` or both are `Date`, [#4979](https://github.com/Rdatatable/data.table/issues/4749). Thanks @George9000 for the report and @MichaelChirico for the fix.
107+
106108
### NOTES
107109
108110
1. The following in-progress deprecations have proceeded:

R/IDateTime.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ round.IDate = function(x, digits=c("weeks", "months", "quarters", "years"), ...)
9191
years = ISOdate(year(x), 1L, 1L)))
9292
}
9393

94+
chooseOpsMethod.IDate = function(x, y, mx, my, cl, reverse) inherits(y, "Date")
95+
9496
#Adapted from `+.Date`
9597
`+.IDate` = function(e1, e2) {
9698
if (nargs() == 1L)
@@ -115,7 +117,7 @@ round.IDate = function(x, digits=c("weeks", "months", "quarters", "years"), ...)
115117
if (storage.mode(e1) != "integer")
116118
internal_error("storage mode of IDate is somehow no longer integer") # nocov
117119
if (nargs() == 1L)
118-
stopf("unary - is not defined for \"IDate\" objects")
120+
stopf('unary - is not defined for "IDate" objects')
119121
if (inherits(e2, "difftime"))
120122
internal_error("difftime objects may not be subtracted from IDate, but Ops dispatch should have intervened to prevent this") # nocov
121123

inst/tests/tests.Rraw

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21566,3 +21566,13 @@ test(2335.3, isoyear(as.IDate("2023-08-15")), 2023L) # Normal mid-year case
2156621566
test(2335.4, isoyear(as.IDate(c("2019-12-30", "2016-01-01", "2023-08-15"))),c(2020L, 2015L, 2023L))
2156721567
test(2335.5, isoyear("2019-12-30"), 2020L)
2156821568
test(2335.6, isoyear(as.Date("2019-12-30")), 2020L)
21569+
21570+
# t1-t2 for Date/IDate should be consistent, modulo storage mode #4979
21571+
t1 = as.IDate("2025-07-01")
21572+
t2 = as.IDate("2025-06-01")
21573+
test(2336.1, all.equal(as.Date(t1) - as.Date(t2), t1 - t2))
21574+
test(2336.2, all.equal(as.Date(t2) - as.Date(t1), t2 - t1))
21575+
test(2336.3, all.equal(as.Date(t1) - t2, t1 - t2))
21576+
test(2336.4, all.equal(as.Date(t2) - t1, t2 - t1))
21577+
test(2336.5, all.equal(t1 - as.Date(t2), t1 - t2))
21578+
test(2336.6, all.equal(t2 - as.Date(t1), t2 - t1))

0 commit comments

Comments
 (0)