Вопрос

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 the tzone attribute of the variable directly after importing the data:

attr(results$timestamp,"tzone") <- "UTC"

If your data is of class POSIXct this will change the data by the timezone offset, so convert to POSIXlt first by wrapping in an as.POSIXlt():

results$timestamp <- as.POSIXlt(results$timestamp)

eg:

tm <- as.POSIXlt(Sys.time())
tm
[1] "2011-09-20 13:45:01 BST"
attr(tm,"tzone") <- "UTC"
tm
[1] "2011-09-20 13:45:01 UTC"

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:

Printing of date-times will make use of the time-zone abbreviation in use at the time, if known. For example, for Paris pre-1940 this could be LMT, PMT, WET or WEST. To enable this, the "POSIXlt" class has an optional component "zone" recording the abbreviation for each element.

So now you would just use tm$zone <- "UTC"

Другие советы

Probably easiest to change the timezone afterwards.

library(lubridate)
tz(results) <- "UTC"
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top