I am trying to import some data from Sql Server 2008 into R, using RODBC with:
db <- odbcDriverConnect(connection = "Driver={SQL Server Native Client 10.0};Server=server; Database=db;Trusted_Connection=yes;")
results <- sqlQuery(db, "select timestamp from table where some-restriction")
The data is stored in a column of type “datetime”. All timestamps are in UTC, however my system timezone is CET. R converts all timestamps to values of type “POSIXct” “POSIXt” e.g:
“2011-01-01 07:24:12 CET”
“2011-01-01 08:35:10 CET”
“2011-01-01 09:02:50 CET”
timestamps are correct, timezone is wrong. Is seems to me that since timezone is not explicitly specified, R assigns to all timestamps my local timezone.
Is there any way the timezone of the data can be specified, so timezone information would be correct?
Pre R 3.1.0, and when this answer was originally written:
For objects of class
POSIXlt, you could modify thetzoneattribute of the variable directly after importing the data:If your data is of class
POSIXctthis will change the data by the timezone offset, so convert toPOSIXltfirst by wrapping in anas.POSIXlt():eg:
Since R 3.1.0 this behaviour has changed to use a component of the POSIXlt object rather than an attribute, and is obliquely referenced in the news by:
So now you would just use
tm$zone <- "UTC"