Skip to content

Commit d4d0b2d

Browse files
committed
sequential and legacy
1 parent b78b3d8 commit d4d0b2d

File tree

3 files changed

+22
-22
lines changed

3 files changed

+22
-22
lines changed

NEWS.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@
3838
3939
1. `data.table(x=1, <expr>)`, where `<expr>` is an expression resulting in a 1-column matrix without column names, will eventually have names `x` and `V2`, not `x` and `V1`, consistent with `data.table(x=1, <expr>)` where `<expr>` results in an atomic vector, for example `data.table(x=1, cbind(1))` and `data.table(x=1, 1)` will both have columns named `x` and `V2`. In this release, the matrix case continues to be named `V1`, but the new behavior can be activated by setting `options(datatable.old.matrix.autoname)` to `FALSE`. See point 5 under Bug Fixes for more context; this change will provide more internal consistency as well as more consistency with `data.frame()`.
4040
41-
2. The `week()` function will be fixed in a future release to correctly calculate weeks sequentially (days 1-7 as week 1), which is a potential breaking change. For now, the current (buggy) behavior remains the default. A one-time deprecation warning will be issued when the old and new behaviors differ; this can be suppressed with `options(datatable.warn.week.deprecation = FALSE)`. Users can opt-in to the correct behavior now with the temporary option `options(datatable.week.new = TRUE)`. See [#2611](https://github.com/Rdatatable/data.table/issues/2611) for details. Thanks @MichaelChirico for the report.
41+
2. The behavior of `week()` will be changed in a future release to calculate weeks sequentially (days 1-7 as week 1), which is a potential breaking change. For now, the current "legacy" behavior remains the default, and a one-time deprecation warning will be issued. Users can control this behavior with the temporary option `options(datatable.week = "...")`:
42+
* `"sequential"`: Opt-in to the new, sequential behavior (no warning).
43+
* `"legacy"`: Continue using the legacy behavior but suppress the deprecation warning.
44+
See [#2611](https://github.com/Rdatatable/data.table/issues/2611) for details. Thanks @MichaelChirico for the report and @venom12_04 for the PR.
4245
4346
### NEW FEATURES
4447

inst/tests/tests.Rraw

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18384,7 +18384,7 @@ x = c("1111-11-11", "2019-01-01", "2019-02-28", "2019-03-01", "2019-12-31", "202
1838418384
test(2236.1, yday(x), c(315L, 1L, 59L, 60L, 365L, 60L, 61L, 366L, 1L, 366L, 60L, NA))
1838518385
test(2236.2, mday(x), c(11L, 1L, 28L, 1L, 31L, 29L, 1L, 31L, 1L, 31L, 1L, NA))
1838618386
test(2236.3, wday(x), c(7L, 3L, 5L, 6L, 3L, 7L, 1L, 5L, 1L, 2L, 2L, NA))
18387-
test(2236.4, week(x), c(46L, 1L, 9L, 9L, 53L, 9L, 9L, 53L, 1L, 53L, 9L, NA), warning="The current behavior of data.table::week() is deprecated", options = list(datatable.warn.week.deprecation = NULL))
18387+
test(2236.4, week(x), c(46L, 1L, 9L, 9L, 53L, 9L, 9L, 53L, 1L, 53L, 9L, NA), warning = "The default behavior of data.table::week() is deprecated", options = list(datatable.week = NULL))
1838818388
test(2236.5, month(x), c(11L, 1L, 2L, 3L, 12L, 2L, 3L, 12L, 1L, 12L, 3L, NA))
1838918389
test(2236.6, quarter(x), c(4L, 1L, 1L, 1L, 4L, 1L, 1L, 4L, 1L, 4L, 1L, NA))
1839018390
test(2236.7, year(x), c(1111L, 2019L, 2019L, 2019L, 2019L, 2020L, 2020L, 2020L, 2040L, 2040L, 2100L, NA))
@@ -21817,9 +21817,9 @@ b
2181721817
', comment.char = '#', strip.white = FALSE, sep = ","), data.table(a=c(" ", "b")))
2181821818

2181921819
# week() sequential numbering fix tests #2611
21820-
test(2342.1, week(as.IDate("1970-01-01") + 0:7), c(1L,1L,1L,1L,1L,1L,1L,2L), options = list(datatable.week.new = TRUE))
21821-
test(2342.2, week(as.IDate(c("2012-02-28","2012-02-29","2012-03-01"))), c(9L,9L,9L), options = list(datatable.week.new = TRUE))
21822-
test(2342.3, week(as.IDate(c("2019-12-31","2020-01-01"))), c(53L,1L), options = list(datatable.week.new = TRUE))
21823-
test(2342.4, week(as.IDate(c("2020-12-31","2021-01-01"))), c(53L,1L), options = list(datatable.week.new = TRUE))
21824-
test(2342.5, week(as.IDate("2021-01-06") + 0:6), c(1L,1L,2L,2L,2L,2L,2L), options = list(datatable.week.new = TRUE))
21825-
test(2342.6, week(as.IDate(c("2016-02-27","2016-02-28","2016-02-29","2016-03-01","2016-03-02"))), c(9L,9L,9L,9L,9L), options = list(datatable.week.new = TRUE))
21820+
test(2342.1, week(as.IDate("1970-01-01") + 0:7), c(1L,1L,1L,1L,1L,1L,1L,2L), options = list(datatable.week = "sequential"))
21821+
test(2342.2, week(as.IDate(c("2012-02-28","2012-02-29","2012-03-01"))), c(9L,9L,9L), options = list(datatable.week = "sequential"))
21822+
test(2342.3, week(as.IDate(c("2019-12-31","2020-01-01"))), c(53L,1L), options = list(datatable.week = "sequential"))
21823+
test(2342.4, week(as.IDate(c("2020-12-31","2021-01-01"))), c(53L,1L), options = list(datatable.week = "sequential"))
21824+
test(2342.5, week(as.IDate("2021-01-06") + 0:6), c(1L,1L,2L,2L,2L,2L,2L), options = list(datatable.week = "sequential"))
21825+
test(2342.6, week(as.IDate(c("2016-02-27","2016-02-28","2016-02-29","2016-03-01","2016-03-02"))), c(9L,9L,9L,9L,9L), options = list(datatable.week = "sequential"))

src/idatetime.c

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -147,14 +147,12 @@ SEXP convertDate(SEXP x, SEXP type)
147147
SEXP ans = PROTECT(allocVector(INTSXP, n));
148148
int *ansp = INTEGER(ans);
149149

150-
SEXP opt_new = GetOption(install("datatable.week.new"), R_NilValue);
151-
bool use_new_behavior = isLogical(opt_new) && LOGICAL(opt_new)[0] == TRUE;
150+
SEXP opt = GetOption(install("datatable.week"), R_NilValue);
151+
const char *mode = isString(opt) && length(opt) == 1 ? CHAR(STRING_ELT(opt, 0)) : "default";
152152

153-
bool can_warn = false;
154-
if (!use_new_behavior && !week_deprecation_warning_issued) {
155-
SEXP opt_warn_depr = GetOption(install("datatable.warn.week.deprecation"), R_NilValue);
156-
can_warn = !isLogical(opt_warn_depr) || LOGICAL(opt_warn_depr)[0] != FALSE;
157-
}
153+
bool use_sequential = !strcmp(mode, "sequential");
154+
bool use_legacy = !strcmp(mode, "legacy");
155+
bool can_warn = !use_sequential && !use_legacy && !week_deprecation_warning_issued;
158156

159157
for (int i = 0; i < n; i++) {
160158
if (ix[i] == NA_INTEGER) {
@@ -163,19 +161,18 @@ SEXP convertDate(SEXP x, SEXP type)
163161
}
164162
int yday;
165163
convertSingleDate(ix[i], YDAY, &yday);
164+
int new_week = ((yday - 1) / 7) + 1;
166165

167-
if (use_new_behavior) {
168-
ansp[i] = ((yday - 1) / 7) + 1;
166+
if (use_sequential) {
167+
ansp[i] = new_week;
169168
} else {
170169
int old_week = (yday / 7) + 1;
171170
ansp[i] = old_week;
172-
if (can_warn) {
173-
int new_week = ((yday - 1) / 7) + 1;
174-
if (new_week != old_week) {
175-
warning("The current behavior of data.table::week() is deprecated and will be fixed in a future version to be sequential (day 1-7 is week 1). To opt-in to the new behavior now, run: options(datatable.week.new = TRUE). To suppress this warning, run: options(datatable.warn.week.deprecation = FALSE)");
171+
if (can_warn && new_week != old_week) {
172+
warning(_("The default behavior of data.table::week() is deprecated. It will be changed to 'sequential' in a future version. To opt-in to the new behavior now, run: options(datatable.week = 'sequential'). To suppress this warning and continue using the legacy behavior, run: options(datatable.week = 'legacy')."));
176173
week_deprecation_warning_issued = 1;
177174
can_warn = false;
178-
}
175+
179176
}
180177
}
181178
}

0 commit comments

Comments
 (0)