Señor O
Señor O

Reputation: 17432

Weird POSIXct error

For some reason, as.POSIXct interprets "2013-03-10 02:00:00.000" different from other valid datetimes in that format.

> as.POSIXct("2013-03-10 01:00:00.000") #Different time, same date
[1] "2013-03-10 01:00:00 PST"

> as.POSIXct("2013-03-11 02:00:00.000") #Same time, different date
[1] "2013-03-11 02:00:00 PDT"

> as.POSIXct("2013-03-10 02:00:00.000")
[1] "2013-03-10 PST"

I'm using the package RODBC to read this from a database, and it automatically converts this entire column of datetimes into POSIXct class. This causes the entire column to lose time information.

Upvotes: 2

Views: 174

Answers (1)

Ben Bolker
Ben Bolker

Reputation: 226981

This is a daylight savings time issue: apparently 2 AM on 2013-03-10 doesn't exist in that time zone. Nevertheless, it's mildly interesting (at least to me) that as.POSIXct doesn't complain, but silently returns a slightly odd answer. One problem may be that R typically uses system libraries for some of this stuff, and so is at the whim of the underlying libraries ...

Incorporating useful information from the comments: @JoshUlrich points out that you can get around this (provided that the original data are really in GMT) by using Sys.setenv(TZ="GMT") before importing the data, since RODBC uses the system-level timezone rather than allowing you to specify it ...

Upvotes: 2

Related Questions