Skip to content

Commit 4e97f03

Browse files
added isoyear funtion (#7197)
* added isoyear * test * modified statement * Update man/IDateTime.Rd Co-authored-by: Benjamin Schwendinger <[email protected]> * Update NEWS.md --------- Co-authored-by: Benjamin Schwendinger <[email protected]>
1 parent 428ba9a commit 4e97f03

File tree

5 files changed

+28
-3
lines changed

5 files changed

+28
-3
lines changed

NAMESPACE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ if (getRversion() >= "3.6.0") {
153153

154154
# IDateTime support:
155155
export(as.IDate,as.ITime,IDateTime)
156-
export(second,minute,hour,yday,wday,mday,week,isoweek,month,quarter,year,yearmon,yearqtr)
156+
export(second,minute,hour,yday,wday,mday,week,isoweek,isoyear,month,quarter,year,yearmon,yearqtr)
157157

158158
S3method("[", ITime)
159159
S3method("+", IDate)

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@
6767

6868
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.
6969

70+
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.
71+
7072
### BUG FIXES
7173

7274
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.

R/IDateTime.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ isoweek = function(x) as.integer(format(as.IDate(x), "%V"))
355355
# nearest_thurs = as.IDate(7L * (as.integer(x + 3L) %/% 7L))
356356
# year_start = as.IDate(format(nearest_thurs, '%Y-01-01'))
357357
# 1L + (nearest_thurs - year_start) %/% 7L
358-
358+
isoyear = function(x) as.integer(format(as.IDate(x), "%G"))
359359

360360
month = function(x) convertDate(as.IDate(x), "month")
361361
quarter = function(x) convertDate(as.IDate(x), "quarter")

inst/tests/tests.Rraw

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21549,3 +21549,11 @@ f = tempfile()
2154921549
writeLines(c('a', rep('0x1.ffffp0', 10000L), '0x1.ff\x9fp0', rep('0x1.ffffp0', 20000L)), f)
2155021550
test(2334, names(fread(f)), "a")
2155121551
unlink(f)
21552+
21553+
# Tests for new isoyear() helper (complement to isoweek) #7154
21554+
test(2335.1, isoyear(as.IDate("2019-12-30")), 2020L) # End of year edge case
21555+
test(2335.2, isoyear(as.IDate("2016-01-01")), 2015L) # Start of year edge case
21556+
test(2335.3, isoyear(as.IDate("2023-08-15")), 2023L) # Normal mid-year case
21557+
test(2335.4, isoyear(as.IDate(c("2019-12-30", "2016-01-01", "2023-08-15"))),c(2020L, 2015L, 2023L))
21558+
test(2335.5, isoyear("2019-12-30"), 2020L)
21559+
test(2335.6, isoyear(as.Date("2019-12-30")), 2020L)

man/IDateTime.Rd

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
\alias{mday}
3939
\alias{week}
4040
\alias{isoweek}
41+
\alias{isoyear}
4142
\alias{month}
4243
\alias{quarter}
4344
\alias{year}
@@ -92,6 +93,7 @@ wday(x)
9293
mday(x)
9394
week(x)
9495
isoweek(x)
96+
isoyear(x)
9597
month(x)
9698
quarter(x)
9799
year(x)
@@ -187,6 +189,8 @@ which specify that the first week of the year is the one containing the first Th
187189
This convention ensures that week boundaries align consistently with year boundaries,
188190
accounting for both year transitions and varying day counts per week.
189191
192+
Similarly, \code{isoyear()} returns the ISO 8601 year corresponding to the ISO week.
193+
190194
}
191195
192196
\value{
@@ -200,7 +204,7 @@ accounting for both year transitions and varying day counts per week.
200204
\code{itime} in \code{IDate} and \code{ITime} format.
201205
202206
\code{second}, \code{minute}, \code{hour}, \code{yday}, \code{wday},
203-
\code{mday}, \code{week}, \code{month}, \code{quarter},
207+
\code{mday}, \code{week}, \code{isoweek}, \code{isoyear}, \code{month}, \code{quarter},
204208
and \code{year} return integer values
205209
for second, minute, hour, day of year, day of week,
206210
day of month, week, month, quarter, and year, respectively.
@@ -281,6 +285,17 @@ round(seqdates, "months")
281285
round(seqtimes, "hours")
282286
trunc(seqtimes, "hours")
283287

288+
# Examples for isoyear() and isoweek()
289+
d1 = as.IDate("2019-12-30")
290+
year(d1)
291+
isoweek(d1)
292+
isoyear(d1)
293+
294+
d2 = as.IDate("2016-01-01")
295+
year(d2)
296+
isoweek(d2)
297+
isoyear(d2)
298+
284299
}
285300
\keyword{utilities}
286301

0 commit comments

Comments
 (0)