问题
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.
回答1:
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 ...
来源:https://stackoverflow.com/questions/16821557/weird-posixct-error