防止意外的时区转换

Ken*_*ams 7 timezone r date

在R中,我有一堆我在GMT中测量的日期时间值.我一直遇到一些事故,其中一些功能或其他功能失去了我的价值观的时区,甚至失去了班级名称.即使是如此基本的功能c()unlist():

> dput(x)
structure(1317830532, class = c("POSIXct", "POSIXt"), tzone = "GMT")
> dput(c(x))
structure(1317830532, class = c("POSIXct", "POSIXt"))
> dput(list(x))
list(structure(1317830532, class = c("POSIXct", "POSIXt"), tzone = "GMT"))
> dput(unlist(list(x)))
1317830532
Run Code Online (Sandbox Code Playgroud)

如果这种情况发生在我最不想要的时候,我觉得我的头发远离真正的火星气候轨道器时刻.任何人都有任何策略,以确保他们的日期"保持"?

And*_*rie 6

此行为记录在?c,?DateTimeClasses并且?unlist:

来自?DateTimeClasses:

使用c" POSIXlt"对象将它们转换为当前时区," POSIXct"对象将删除任何" tzone"属性(即使它们都标有相同的时区).*

来自?c:

c 有时用于删除除名称之外的属性的副作用.*


也就是说,我的测试表明,尽管使用c或是,数据的完整性仍保持不变unlist.例如:

x <- structure(1317830532, class = c("POSIXct", "POSIXt"), 
                 tzone = "GMT")
y <- structure(1317830532+3600, class = c("POSIXct", "POSIXt"), 
                 tzone = "PST8PDT")
x
[1] "2011-10-05 16:02:12 GMT"

y
[1] "2011-10-05 10:02:12 PDT"

strftime(c(x, y), format="%Y/%m/%d %H:%M:%S", tz="GMT")
[1] "2011/10/05 16:02:12" "2011/10/05 17:02:12"

strftime(c(x, y), format="%Y/%m/%d %H:%M:%S", tz="PST8PDT")
[1] "2011/10/05 09:02:12" "2011/10/05 10:02:12"

strftime(unlist(y), format="%Y/%m/%d %H:%M:%S", tz="PST8PDT")
[1] "2011/10/05 10:02:12"
Run Code Online (Sandbox Code Playgroud)

如果您使用R来跟踪日期,您的火星漫游者应该没问题.


Bri*_*ggs 4

那么为什么不将 R 会话的时区设置为 GMT呢?如果某些内容转换为“当前”时区,它仍然是正确的。