Weird POSIXct error

久未见 提交于 2021-01-28 04:26:41

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!