Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .ci/atime/tests.R
Original file line number Diff line number Diff line change
Expand Up @@ -277,5 +277,14 @@ test.list <- atime::atime_test_list(
Slow = "73d79edf8ff8c55163e90631072192301056e336", # Parent of the first commit in the PR (https://github.com/Rdatatable/data.table/commit/8397dc3c993b61a07a81c786ca68c22bc589befc)
Fast = "8397dc3c993b61a07a81c786ca68c22bc589befc"), # Commit in the PR (https://github.com/Rdatatable/data.table/pull/7019/commits) that removes inefficiency

"isoweek improved in #7144" = atime::atime_test(
setup = {
set.seed(349)
x = sample(Sys.Date() - 0:5000, N, replace=TRUE)
},
expr = data.table::isoweek(x),
Slow = "548410d23dd74b625e8ea9aeb1a5d2e9dddd2927", # Parent of the first commit in the PR (https://github.com/Rdatatable/data.table/commit/548410d23dd74b625e8ea9aeb1a5d2e9dddd2927)
Fast = "c0b32a60466bed0e63420ec105bc75c34590865e"), # Commit in the PR (https://github.com/Rdatatable/data.table/pull/7144/commits) that uses a much faster implementation

tests=extra.test.list)
# nolint end: undesirable_operator_linter.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@

8. `groupingsets()` gets a new argument `enclos` for use together with the `jj` argument in functions wrapping `groupingsets()`, including the existing wrappers `rollup()` and `cube()`, [#5560](https://github.com/Rdatatable/data.table/issues/5560). When forwarding a `j`-expression as `groupingsets(jj = substitute(j))`, make sure to pass `enclos = parent.frame()` as well, so that the `j`-expression will be evaluated in the right context. This makes it possible for `j` to refer to variables outside the `data.table`. Thanks @sindribaldur for the report and @aitap for the fix.

9. `isoweek()` is much faster (e.g. 20x) by re-using an implementation from {base}, [#5111](https://github.com/Rdatatable/data.table/issues/5111). Thanks @MichaelChirico for the report and PR.

### BUG FIXES

1. Custom binary operators from the `lubridate` package now work with objects of class `IDate` as with a `Date` subclass, [#6839](https://github.com/Rdatatable/data.table/issues/6839). Thanks @emallickhossain for the report and @aitap for the fix.
Expand Down
13 changes: 7 additions & 6 deletions R/IDateTime.R
Original file line number Diff line number Diff line change
Expand Up @@ -342,19 +342,20 @@ yday = function(x) convertDate(as.IDate(x), "yday")
wday = function(x) convertDate(as.IDate(x), "wday")
mday = function(x) convertDate(as.IDate(x), "mday")
week = function(x) convertDate(as.IDate(x), "week")
isoweek = function(x) {
# TODO(#3279): Investigate if improved as.IDate() makes our below implementation faster than this
isoweek = function(x) as.integer(format(as.IDate(x), "%V"))
# ISO 8601-conformant week, as described at
# https://en.wikipedia.org/wiki/ISO_week_date
# Approach:
# * Find nearest Thursday to each element of x
# * Find the number of weeks having passed between
# January 1st of the year of the nearest Thursdays and x

x = as.IDate(x) # number of days since 1 Jan 1970 (a Thurs)
nearest_thurs = as.IDate(7L * (as.integer(x + 3L) %/% 7L))
year_start = as.IDate(format(nearest_thurs, '%Y-01-01'))
1L + (nearest_thurs - year_start) %/% 7L
}
# x = as.IDate(x) # number of days since 1 Jan 1970 (a Thurs)
# nearest_thurs = as.IDate(7L * (as.integer(x + 3L) %/% 7L))
# year_start = as.IDate(format(nearest_thurs, '%Y-01-01'))
# 1L + (nearest_thurs - year_start) %/% 7L


month = function(x) convertDate(as.IDate(x), "month")
quarter = function(x) convertDate(as.IDate(x), "quarter")
Expand Down
Loading