Skip to content

Commit fe94435

Browse files
authored
isoweek()/isoyear(): compute in C via convertDate() (#7951)
1 parent 086c646 commit fe94435

2 files changed

Lines changed: 22 additions & 11 deletions

File tree

‎R/IDateTime.R‎

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -393,15 +393,8 @@ yday = function(x) convertDate(as.IDate(x), "yday")
393393
wday = function(x) convertDate(as.IDate(x), "wday")
394394
mday = function(x) convertDate(as.IDate(x), "mday")
395395
week = function(x) convertDate(as.IDate(x), "week")
396-
# ISO 8601-conformant week, as described at
397-
# https://en.wikipedia.org/wiki/ISO_week_date
398-
# Approach:
399-
# * Find nearest Thursday to each element of x
400-
# * Find the number of weeks having passed between
401-
# January 1st of the year of the nearest Thursdays and x
402-
nearest_thurs = function(x) 7L * ((as.integer(as.IDate(x)) + 3L) %/% 7L)
403-
isoweek = function(x) (convertDate(nearest_thurs(x), "yday") - 1L) %/% 7L + 1L
404-
isoyear = function(x) convertDate(nearest_thurs(x), "year")
396+
isoweek = function(x) convertDate(as.IDate(x), "isoweek")
397+
isoyear = function(x) convertDate(as.IDate(x), "isoyear")
405398

406399
month = function(x) convertDate(as.IDate(x), "month")
407400
quarter = function(x) convertDate(as.IDate(x), "quarter")
@@ -432,6 +425,6 @@ yearqtr = function(x, format = c("numeric", "character")) {
432425
}
433426

434427
convertDate = function(x, type) {
435-
type = match.arg(type, c("yday", "wday", "mday", "week", "month", "quarter", "year", "yearmon", "yearqtr"))
428+
type = match.arg(type, c("yday", "wday", "mday", "week", "isoweek", "isoyear", "month", "quarter", "year", "yearmon", "yearqtr"))
436429
.Call(CconvertDate, x, type)
437430
}

‎src/idatetime.c‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ static const int YEARS100 = 36524;
55
static const int YEARS4 = 1461;
66
static const int YEARS1 = 365;
77

8-
typedef enum { YDAY, WDAY, MDAY, WEEK, MONTH, QUARTER, YEAR, YEARMON, YEARQTR} datetype;
8+
typedef enum { YDAY, WDAY, MDAY, WEEK, ISOWEEK, ISOYEAR, MONTH, QUARTER, YEAR, YEARMON, YEARQTR} datetype;
99

1010
static inline bool isLeapYear(int year) {
1111
return (year % 100 != 0 || year % 400 == 0) && year % 4 == 0;
@@ -29,6 +29,22 @@ void convertSingleDate(int x, datetype type, void *out)
2929
return;
3030
}
3131

32+
if (type == ISOWEEK || type == ISOYEAR) {
33+
// ISO 8601-conformant week, as described at https://en.wikipedia.org/wiki/ISO_week_date
34+
// The ISO week and year are those of the nearest Thursday
35+
// Thursdays are multiples of 7, since x=0 (1970-01-01) was a Thursday
36+
int thu = x + (3 - (x % 7 + 10) % 7);
37+
int res;
38+
if (type == ISOWEEK) {
39+
convertSingleDate(thu, YDAY, &res);
40+
res = (res - 1) / 7 + 1;
41+
} else {
42+
convertSingleDate(thu, YEAR, &res);
43+
}
44+
*(int *)out = res;
45+
return;
46+
}
47+
3248
int days = x - 11017;
3349

3450
int years400 = days / YEARS400;
@@ -134,6 +150,8 @@ SEXP convertDate(SEXP x, SEXP type)
134150
else if (!strcmp(ctype_str, "wday")) ctype = WDAY;
135151
else if (!strcmp(ctype_str, "mday")) ctype = MDAY;
136152
else if (!strcmp(ctype_str, "week")) ctype = WEEK;
153+
else if (!strcmp(ctype_str, "isoweek")) ctype = ISOWEEK;
154+
else if (!strcmp(ctype_str, "isoyear")) ctype = ISOYEAR;
137155
else if (!strcmp(ctype_str, "month")) ctype = MONTH;
138156
else if (!strcmp(ctype_str, "quarter")) ctype = QUARTER;
139157
else if (!strcmp(ctype_str, "year")) ctype = YEAR;

0 commit comments

Comments
 (0)