Skip to content

Commit 69b33aa

Browse files
committed
added news entry
1 parent d962514 commit 69b33aa

3 files changed

Lines changed: 28 additions & 28 deletions

File tree

NEWS.md

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

5959
12. `print.data.table()` now truncates long character columns and list-column summaries by default to avoid horizontal console overflow, [#7718](https://github.com/Rdatatable/data.table/issues/7718). When `datatable.prettyprint.char` is `NULL` (the default), the truncation limit is now dynamically calculated based on the available console width. Use `options(datatable.prettyprint.char=Inf)` for the old default behavior (never truncate). Thanks @tdhock for the report and @venom1204 for the fix.
6060

61+
13. `as.IDate()` and `as.ITime()` now preserve names, matching base `as.Date()` behavior, [#7252](https://github.com/Rdatatable/data.table/issues/7252). Thanks @DavisVaughan for the report and @venom1204 for the PR.
62+
6163
### Notes
6264

6365
1. {data.table} now depends on R 3.5.0 (2018).

R/IDateTime.R

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55

66
as.IDate = function(x, ...) UseMethod("as.IDate")
77

8+
copy_names = function(ans, nm) {
9+
if (!is.null(nm)) setattr(ans, "names", nm)
10+
ans
11+
}
12+
813
as.IDate.default = function(x, ..., tz = attr(x, "tzone", exact=TRUE)) {
914
if (is.null(tz)) tz = "UTC"
1015
if (is.character(x)) {
@@ -23,34 +28,30 @@ as.IDate.numeric = function(x, origin = "1970-01-01", ...) {
2328
# Since R 3.1.0 improved class()<- and data.table's oldest oldest supported R is now 3.1.0, we can use class<- again
2429
# structure() contains a match() and replace for specials, which we don't need.
2530
# class()<- ensures at least 1 shallow copy as appropriate is returned.
26-
if (!is.null(nm)) setattr(x, "names", nm)
27-
x
31+
copy_names(x, nm)
2832
} else {
29-
# only call expensive as.IDate.character if we have to
3033
ans = as.IDate(origin, ...) + as.integer(x)
31-
if (!is.null(nm)) setattr(ans, "names", nm)
32-
ans
34+
copy_names(ans, nm)
3335
}
3436
}
3537

3638
as.IDate.Date = function(x, ...) {
3739
nm = names(x)
38-
x = as.integer(x) # if already integer, x will be left unchanged as the original input
39-
class(x) = c("IDate", "Date") # class()<- will copy if as.integer() did not create, and may not if it did we hope
40-
if (!is.null(nm)) setattr(x, "names", nm)
41-
x # always return a new object
40+
x = as.integer(x)
41+
class(x) = c("IDate", "Date")
42+
copy_names(x, nm)
4243
}
4344

4445
as.IDate.POSIXct = function(x, tz = attr(x, "tzone", exact=TRUE), ...) {
4546
if (is_utc(tz)) {
4647
ans = as.integer(as.numeric(x) %/% 86400L)
4748
setattr(ans, "class", c("IDate", "Date"))
48-
setattr(ans, "names", names(x))
49-
ans
49+
copy_names(ans, names(x))
5050
} else {
5151
as.IDate(as.Date(x, tz = tz %||% '', ...))
5252
}
5353
}
54+
5455
as.IDate.IDate = function(x, ...) x
5556

5657
as.Date.IDate = function(x, ...) {
@@ -166,17 +167,15 @@ as.ITime.numeric = function(x, ms = 'truncate', ...) {
166167
nm = names(x)
167168
secs = clip_msec(x, ms) %% 86400L # the %% here ensures a local copy is obtained; the truncate as.integer() may not copy
168169
setattr(secs, "class", "ITime")
169-
if (!is.null(nm)) setattr(secs, "names", nm)
170-
secs
170+
copy_names(secs, nm)
171171
}
172172

173173
as.ITime.character = function(x, format, ...) {
174174
nm = names(x)
175175
x = unclass(x)
176176
if (!missing(format)) {
177177
ans = as.ITime(strptime(x, format = format, ...), ...)
178-
if (!is.null(nm)) setattr(ans, "names", nm)
179-
return(ans)
178+
return(copy_names(ans, nm))
180179
}
181180
# else allow for mixed formats, such as test 1189 where seconds are caught despite varying format
182181
y = strptime(x, format = "%H:%M:%OS", ...)
@@ -198,17 +197,15 @@ as.ITime.character = function(x, format, ...) {
198197
}
199198
}
200199
ans = as.ITime(y, ...)
201-
if (!is.null(nm)) setattr(ans, "names", nm)
202-
ans
200+
copy_names(ans, nm)
203201
}
204202

205203
as.ITime.POSIXlt = function(x, ms = 'truncate', ...) {
206204
nm = names(x)
207205
secs = clip_msec(x$sec, ms)
208206
ans = with(x, secs + min * 60L + hour * 3600L)
209207
setattr(ans, "class", "ITime")
210-
if (!is.null(nm)) setattr(ans, "names", nm)
211-
ans
208+
copy_names(ans, nm)
212209
}
213210

214211
as.ITime.times = function(x, ms = 'truncate', ...) {

inst/tests/tests.Rraw

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21671,12 +21671,13 @@ test(2375.5, print(data.table(id=1L, score=99.1, txt="abcdefghijklmnopqrstuvwxyz
2167121671
test(2375.6, print(data.table(x=rep("ABCDEFGHIJKLMNOPQRSTUVWXYZ", 1e6)), topn=1), output="1000000: ABCDEFGHIJKLM...", options=list(width=25, datatable.prettyprint.char=NULL))
2167221672

2167321673
# #7252 as.IDate()/as.ITime preserve names
21674-
test(2376.1, names(as.IDate(c(a = "2019-01-01"))), "a")
21675-
test(2376.2, names(c(a = as.IDate("2019-01-01"))), "a")
21676-
test(2376.3, names(as.ITime(c(a = "12:00:00"))), "a")
21677-
test(2376.4, names(as.IDate(structure(as.POSIXct("2019-01-01 12:00:00"), names = "a"))), "a")
21678-
test(2376.5, names(as.ITime(structure(3600, names = "a"))), "a")
21679-
test(2376.6, names(as.IDate(c(a = 18000))), "a")
21680-
test(2376.7, names(as.IDate(c(a = 1), origin = "2020-01-01")), "a")
21681-
test(2376.8, names(as.ITime(c(a = "12-00-00"), format = "%H-%M-%S")), "a")
21682-
test(2376.9, names(as.IDate(as.POSIXct(c(a = "2019-01-01"), tz="UTC"))), "a")
21674+
test(2376.01, names(as.IDate(c(a = "2019-01-01"))), "a")
21675+
test(2376.02, names(c(a = as.IDate("2019-01-01"))), "a")
21676+
test(2376.03, names(as.ITime(c(a = "12:00:00"))), "a")
21677+
test(2376.04, names(as.IDate(structure(as.POSIXct("2019-01-01 12:00:00"), names = "a"))), "a")
21678+
test(2376.05, names(as.ITime(structure(3600, names = "a"))), "a")
21679+
test(2376.06, names(as.IDate(c(a = 18000))), "a")
21680+
test(2376.07, names(as.IDate(c(a = 1), origin = "2020-01-01")), "a")
21681+
test(2376.08, names(as.ITime(c(a = "12-00-00"), format = "%H-%M-%S")), "a")
21682+
test(2376.09, names(as.IDate(as.POSIXct(c(a = "2019-01-01"), tz="UTC"))), "a")
21683+
test(2376.10, names(as.IDate(as.POSIXct(c(a = "2019-01-01"), tz="America/New_York"))), "a")

0 commit comments

Comments
 (0)