From 6a2e77855d90a34bc3d90f79ca30fc0c439874fc Mon Sep 17 00:00:00 2001 From: venom1204 Date: Tue, 22 Jul 2025 01:52:00 +0000 Subject: [PATCH 1/5] added isoyear --- NAMESPACE | 2 +- NEWS.md | 2 ++ R/IDateTime.R | 2 +- inst/tests/tests.Rraw | 10 ++++++++++ man/IDateTime.Rd | 19 ++++++++++++++++++- 5 files changed, 32 insertions(+), 3 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index cd7027c77d..c0dff9067c 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -153,7 +153,7 @@ if (getRversion() >= "3.6.0") { # IDateTime support: export(as.IDate,as.ITime,IDateTime) -export(second,minute,hour,yday,wday,mday,week,isoweek,month,quarter,year,yearmon,yearqtr) +export(second,minute,hour,yday,wday,mday,week,isoweek,isoyear,month,quarter,year,yearmon,yearqtr) S3method("[", ITime) S3method("+", IDate) diff --git a/NEWS.md b/NEWS.md index 598e534d26..a6fa7ed31e 100644 --- a/NEWS.md +++ b/NEWS.md @@ -67,6 +67,8 @@ 14. `fcoalesce()` and `setcoalesce()` gain `nan` argument to control whether `NaN` values should be treated as missing (`nan=NA`, the default) or non-missing (`nan=NaN`), [#4567](https://github.com/Rdatatable/data.table/issues/4567). This provides full compatibility with `nafill()` behavior. Thanks to @ethanbsmith for the feature request and @Mukulyadav2004 for the implementation. +15. New function `isoyear()` has been added as a complement to `isoweek()`, returning the year that corresponds to the ISO 8601 week. For example, `isoyear("2019-12-30")` is 2020. Closes [#7154](https://github.com/Rdatatable/data.table/issues/7154). Thanks to @ben-schwen and @MichaelChirico for the suggestion and @venom1204 for the fix. + ### BUG FIXES 1. `fread()` no longer warns on certain systems on R 4.5.0+ where the file owner can't be resolved, [#6918](https://github.com/Rdatatable/data.table/issues/6918). Thanks @ProfFancyPants for the report and PR. diff --git a/R/IDateTime.R b/R/IDateTime.R index 383ceb54e7..5b079a0298 100644 --- a/R/IDateTime.R +++ b/R/IDateTime.R @@ -355,7 +355,7 @@ isoweek = function(x) as.integer(format(as.IDate(x), "%V")) # 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 - +isoyear = function(x) as.integer(format(as.IDate(x), "%G")) month = function(x) convertDate(as.IDate(x), "month") quarter = function(x) convertDate(as.IDate(x), "quarter") diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index a2549ccf6d..9490f82dd0 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -21549,3 +21549,13 @@ f = tempfile() writeLines(c('a', rep('0x1.ffffp0', 10000L), '0x1.ff\x9fp0', rep('0x1.ffffp0', 20000L)), f) test(2334, names(fread(f)), "a") unlink(f) + +# Tests for new isoyear() helper (complement to isoweek) #7154 +test(2335.1, isoyear(as.IDate("2019-12-30")), 2020L) # End of year edge case +test(2335.2, isoyear(as.IDate("2016-01-01")), 2015L) # Start of year edge case +test(2335.3, isoyear(as.IDate("2023-08-15")), 2023L) # Normal mid-year case +test(2335.4, + isoyear(as.IDate(c("2019-12-30", "2016-01-01", "2023-08-15"))), + c(2020L, 2015L, 2023L)) +test(2335.5, isoyear("2019-12-30"), 2020L) +test(2335.6, isoyear(as.Date("2019-12-30")), 2020L) diff --git a/man/IDateTime.Rd b/man/IDateTime.Rd index 64c75d3902..e67ca732d2 100644 --- a/man/IDateTime.Rd +++ b/man/IDateTime.Rd @@ -38,6 +38,7 @@ \alias{mday} \alias{week} \alias{isoweek} +\alias{isoyear} \alias{month} \alias{quarter} \alias{year} @@ -92,6 +93,7 @@ wday(x) mday(x) week(x) isoweek(x) +isoyear(x) month(x) quarter(x) year(x) @@ -187,6 +189,10 @@ which specify that the first week of the year is the one containing the first Th This convention ensures that week boundaries align consistently with year boundaries, accounting for both year transitions and varying day counts per week. +Similarly, \code{isoyear()} returns the year corresponding to the ISO week, +which is essential for correct analysis as this may differ from the calendar +year for dates near the beginning or end of a year. + } \value{ @@ -200,7 +206,7 @@ accounting for both year transitions and varying day counts per week. \code{itime} in \code{IDate} and \code{ITime} format. \code{second}, \code{minute}, \code{hour}, \code{yday}, \code{wday}, - \code{mday}, \code{week}, \code{month}, \code{quarter}, + \code{mday}, \code{week}, \code{isoweek}, \code{isoyear}, \code{month}, \code{quarter}, and \code{year} return integer values for second, minute, hour, day of year, day of week, day of month, week, month, quarter, and year, respectively. @@ -281,6 +287,17 @@ round(seqdates, "months") round(seqtimes, "hours") trunc(seqtimes, "hours") +# Examples for isoyear() and isoweek() +d1 <- as.IDate("2019-12-30") +year(d1) # Returns 2019 +isoweek(d1) # Returns 1 +isoyear(d1) # Correctly returns 2020 + +d2 <- as.IDate("2016-01-01") +year(d2) # Returns 2016 +isoweek(d2) # Returns 53 +isoyear(d2) # Correctly returns 2015 + } \keyword{utilities} From 1be35e3a182dc146d66aa8f6bb54ad4043f477a6 Mon Sep 17 00:00:00 2001 From: venom1204 Date: Tue, 22 Jul 2025 01:55:21 +0000 Subject: [PATCH 2/5] test --- inst/tests/tests.Rraw | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index 9490f82dd0..fd7e22eec1 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -21554,8 +21554,6 @@ unlink(f) test(2335.1, isoyear(as.IDate("2019-12-30")), 2020L) # End of year edge case test(2335.2, isoyear(as.IDate("2016-01-01")), 2015L) # Start of year edge case test(2335.3, isoyear(as.IDate("2023-08-15")), 2023L) # Normal mid-year case -test(2335.4, - isoyear(as.IDate(c("2019-12-30", "2016-01-01", "2023-08-15"))), - c(2020L, 2015L, 2023L)) +test(2335.4, isoyear(as.IDate(c("2019-12-30", "2016-01-01", "2023-08-15"))),c(2020L, 2015L, 2023L)) test(2335.5, isoyear("2019-12-30"), 2020L) test(2335.6, isoyear(as.Date("2019-12-30")), 2020L) From 995392e4a236ed7e230a2d5b0c39c323a397ed42 Mon Sep 17 00:00:00 2001 From: venom1204 Date: Tue, 22 Jul 2025 14:23:33 +0000 Subject: [PATCH 3/5] modified statement --- NEWS.md | 2 +- man/IDateTime.Rd | 18 +++++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/NEWS.md b/NEWS.md index a6fa7ed31e..02cf0280f1 100644 --- a/NEWS.md +++ b/NEWS.md @@ -67,7 +67,7 @@ 14. `fcoalesce()` and `setcoalesce()` gain `nan` argument to control whether `NaN` values should be treated as missing (`nan=NA`, the default) or non-missing (`nan=NaN`), [#4567](https://github.com/Rdatatable/data.table/issues/4567). This provides full compatibility with `nafill()` behavior. Thanks to @ethanbsmith for the feature request and @Mukulyadav2004 for the implementation. -15. New function `isoyear()` has been added as a complement to `isoweek()`, returning the year that corresponds to the ISO 8601 week. For example, `isoyear("2019-12-30")` is 2020. Closes [#7154](https://github.com/Rdatatable/data.table/issues/7154). Thanks to @ben-schwen and @MichaelChirico for the suggestion and @venom1204 for the fix. +15. New function `isoyear()` has been implemented as a complement to `isoweek()`, returning the ISO 8601 year corresponding to a given date. For example, `isoyear("2019-12-30")` returns 2020. [#7154](https://github.com/Rdatatable/data.table/issues/7154). Thanks to @ben-schwen and @MichaelChirico for the suggestion and @venom1204 for the implementation. ### BUG FIXES diff --git a/man/IDateTime.Rd b/man/IDateTime.Rd index e67ca732d2..7cdb48f8d3 100644 --- a/man/IDateTime.Rd +++ b/man/IDateTime.Rd @@ -288,15 +288,15 @@ round(seqtimes, "hours") trunc(seqtimes, "hours") # Examples for isoyear() and isoweek() -d1 <- as.IDate("2019-12-30") -year(d1) # Returns 2019 -isoweek(d1) # Returns 1 -isoyear(d1) # Correctly returns 2020 - -d2 <- as.IDate("2016-01-01") -year(d2) # Returns 2016 -isoweek(d2) # Returns 53 -isoyear(d2) # Correctly returns 2015 +d1 = as.IDate("2019-12-30") +year(d1) +isoweek(d1) +isoyear(d1) + +d2 = as.IDate("2016-01-01") +year(d2) +isoweek(d2) +isoyear(d2) } \keyword{utilities} From fa4805a028c4fc7dddf3710bb93e884ac8f5f406 Mon Sep 17 00:00:00 2001 From: venom1204 Date: Tue, 22 Jul 2025 19:53:53 +0530 Subject: [PATCH 4/5] Update man/IDateTime.Rd Co-authored-by: Benjamin Schwendinger <52290390+ben-schwen@users.noreply.github.com> --- man/IDateTime.Rd | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/man/IDateTime.Rd b/man/IDateTime.Rd index 7cdb48f8d3..f1e5190afe 100644 --- a/man/IDateTime.Rd +++ b/man/IDateTime.Rd @@ -189,9 +189,7 @@ which specify that the first week of the year is the one containing the first Th This convention ensures that week boundaries align consistently with year boundaries, accounting for both year transitions and varying day counts per week. -Similarly, \code{isoyear()} returns the year corresponding to the ISO week, -which is essential for correct analysis as this may differ from the calendar -year for dates near the beginning or end of a year. +Similarly, \code{isoyear()} returns the ISO 8601 year corresponding to the ISO week. } From e6d65121d5380a44a4d8241f0fb42ddcf2b67344 Mon Sep 17 00:00:00 2001 From: Benjamin Schwendinger <52290390+ben-schwen@users.noreply.github.com> Date: Tue, 22 Jul 2025 17:21:46 +0200 Subject: [PATCH 5/5] Update NEWS.md --- NEWS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 02cf0280f1..f6e86d20f1 100644 --- a/NEWS.md +++ b/NEWS.md @@ -67,7 +67,7 @@ 14. `fcoalesce()` and `setcoalesce()` gain `nan` argument to control whether `NaN` values should be treated as missing (`nan=NA`, the default) or non-missing (`nan=NaN`), [#4567](https://github.com/Rdatatable/data.table/issues/4567). This provides full compatibility with `nafill()` behavior. Thanks to @ethanbsmith for the feature request and @Mukulyadav2004 for the implementation. -15. New function `isoyear()` has been implemented as a complement to `isoweek()`, returning the ISO 8601 year corresponding to a given date. For example, `isoyear("2019-12-30")` returns 2020. [#7154](https://github.com/Rdatatable/data.table/issues/7154). Thanks to @ben-schwen and @MichaelChirico for the suggestion and @venom1204 for the implementation. +15. New function `isoyear()` has been implemented as a complement to `isoweek()`, returning the ISO 8601 year corresponding to a given date, [#7154](https://github.com/Rdatatable/data.table/issues/7154). Thanks to @ben-schwen and @MichaelChirico for the suggestion and @venom1204 for the implementation. ### BUG FIXES