本文介绍了结合POSIXct给错误的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个尝试使用 Reduce 的日期列表,并意识到组合向量时小时数正在更改。下面是一个示例:

I have a list of dates that I was trying to use Reduce on and realized that the hours were being changed when I combined the vectors. Here's an example:

x = structure(1315714440, tzone = "UTC", class = c("POSIXct", "POSIXt"))
y = structure(1325832660, tzone = "UTC", class = c("POSIXct", "POSIXt"))
x
[1] "2011-09-11 04:14:00 UTC"
y
[1] "2012-01-06 06:51:00 UTC"
c(x,y)
[1] "2011-09-11 00:14:00 EDT" "2012-01-06 01:51:00 EST"

为什么会这样呢?

谢谢!

推荐答案

c.POSIXct 删除时区属性。从?c.POSIXct

因此,请遵循 c(x,y ),则可以使用 attr 恢复原始的 UTC 时区:

Thus, following your c(x,y), you may restore the original UTC time zone using attr:

xy <- c(x, y)
attr(xy, "tzone") <- "UTC"
xy
# [1] "2011-09-11 04:14:00 UTC" "2012-01-06 06:51:00 UTC"






Ripley的更多背景信息:


More background by Ripley here:

c(a,b)吗?

如果所有
都使用 c()保留时区,我们认为对象,但主要问题是记录了 c()可以删除
属性:

"We considered having c() retain the timezone if it was common to all theobjects, but the main issue was that c() was documented to removeattributes:

因此,有时删除和保留属性会使
感到困惑。

So, sometimes removing and sometimes retaining attributes was going tobe confusing.

但是无论如何,文档(?c.POSIXct )很清楚:

But in any case, the documentation (?c.POSIXct) is clear:

因此,建议的方法是,如果知道所需的
,则添加 tzone 属性就是这样。 POSIXct 对象是绝对时间:时区
仅影响它们的转换方式(包括打印字符)。

So the recommended way is to add a "tzone" attribute if you know whatyou want it to be. POSIXct objects are absolute times: the timezonemerely affects how they are converted (including to character for printing)."

作为, rbind 可以作为解决方法:

As noted by @aosmith, rbind can be used as work-around:

这篇关于结合POSIXct给错误的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 13:12