diff --git a/NEWS.md b/NEWS.md index d670627825..c586401d87 100644 --- a/NEWS.md +++ b/NEWS.md @@ -8,6 +8,8 @@ ## BUG FIXES +1. A NULL timezone on POSIXct was interpreted by `as.IDate` and `as.ITime` as UTC rather than the session's default timezone (`tz=""`) , [#4085](https://github.com/Rdatatable/data.table/issues/4085). + ## NOTES diff --git a/R/IDateTime.R b/R/IDateTime.R index c719b20bb9..265d57ffb3 100644 --- a/R/IDateTime.R +++ b/R/IDateTime.R @@ -33,7 +33,7 @@ as.IDate.Date = function(x, ...) { } as.IDate.POSIXct = function(x, tz = attr(x, "tzone", exact=TRUE), ...) { - if (is.null(tz)) tz = "UTC" + if (is.null(tz)) tz='' if (tz %chin% c("UTC", "GMT")) { (setattr(as.integer(as.numeric(x) %/% 86400L), "class", c("IDate", "Date"))) # %/% returns new object so can use setattr() on it; wrap with () to return visibly } else @@ -138,7 +138,7 @@ as.ITime.default = function(x, ...) { } as.ITime.POSIXct = function(x, tz = attr(x, "tzone", exact=TRUE), ...) { - if (is.null(tz)) tz = "UTC" + if (is.null(tz)) tz='' if (tz %chin% c("UTC", "GMT")) as.ITime(unclass(x), ...) else as.ITime(as.POSIXlt(x, tz = tz, ...), ...) } diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index d287a39856..80f2bc25e1 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -16392,6 +16392,14 @@ test(2122.2, DT, data.table(V3=5:6)) dt = data.table(SomeNumberA=c(1,1,1),SomeNumberB=c(1,1,1)) test(2123, dt[, .(.N, TotalA=sum(SomeNumberA), TotalB=sum(SomeNumberB)), by=SomeNumberA], data.table(SomeNumberA=1, N=3L, TotalA=1, TotalB=3)) +# system timezone is not usually UTC, so as.ITime.POSIXct shouldn't assume so, #4085 +oldtz=Sys.getenv('TZ') +Sys.setenv(TZ='Asia/Jakarta') # UTC+7 +t0 = as.POSIXct('2019-10-01') +test(2124.1, format(as.ITime(t0)), '00:00:00') +test(2124.2, format(as.IDate(t0)), '2019-10-01') +Sys.setenv(TZ=oldtz) + ################################### # Add new tests above this line #