[datatable-help] Losing timezone of POSIX datetimes

Karl Ove Hufthammer karl at huftis.org
Tue Dec 13 09:29:05 CET 2011


Data tables seems to be losing the time zone of POSIX variables.
I believed this should be considered a bug. Here’s a simple example:

-------------
# Create and date and time from some numbers
makeUTCDateTime=function(hourmin) {
    td=paste("2011-12-13 ", hourmin, ":", hourmin, sep="")
    td=as.POSIXct(td, tz="UTC")
    data.frame(UTCDateTime=td)
}

# Create a data.table containing some numbers
d=data.table(hourmin=10:14)

# Direct use of makeUTCDateTime
x=makeUTCDateTime(d$hourmin)
x                           # Correct time
str(unclass(x$UTCDateTime)) # Correct timezone


# Using a data.table to run makeUTCDateTime
d=data.table(hourmin=10:14)
y=d[,makeUTCDateTime(hourmin), by=hourmin]
y                           # Wrong time?
str(unclass(y$UTCDateTime)) # No timezone specified (local time zone
                            # assumed when displaying the datetime)
-------------

When using makeUTCDateTime directly to create a data.frame, we get 
the correct (UTC) datetime:

> x
          UTCDateTime
1 2011-12-13 10:10:00
2 2011-12-13 11:11:00
3 2011-12-13 12:12:00
4 2011-12-13 13:13:00
5 2011-12-13 14:14:00
> str(unclass(x$UTCDateTime))
 atomic [1:5] 1323771000 1323774660 1323778320 1323781980 1323785640
 - attr(*, "tzone")= chr "UTC"

But when using data.table functionality, we get a POSIXct variable without a 
‘tzone’ attribute, which is then interpreted to be in local time (CET in 
this case) when displaying the result:

> y
     hourmin         UTCDateTime
[1,]      10 2011-12-13 11:10:00
[2,]      11 2011-12-13 12:11:00
[3,]      12 2011-12-13 13:12:00
[4,]      13 2011-12-13 14:13:00
[5,]      14 2011-12-13 15:14:00
> str(unclass(y$UTCDateTime))
 num [1:5] 1323771000 1323774660 1323778320 1323781980 1323785640

System details:

> sessionInfo()
R version 2.14.0 Patched (2011-11-01 r57534)
Platform: x86_64-unknown-linux-gnu (64-bit)

locale:
 [1] LC_CTYPE=nn_NO.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=nn_NO.UTF-8        LC_COLLATE=nn_NO.UTF-8    
 [5] LC_MONETARY=nn_NO.UTF-8    LC_MESSAGES=nn_NO.UTF-8   
 [7] LC_PAPER=C                 LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=nn_NO.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] data.table_1.7.6

loaded via a namespace (and not attached):
[1] tools_2.14.0

-- 
Karl Ove Hufthammer



More information about the datatable-help mailing list