将unix秒(毫秒)转换为POSIXct / POSIXlt [英] Converting unix seconds in milliseconds to POSIXct/POSIXlt
问题描述
为什么在R中将unix时间戳转换为datetime对象时,我会看到一个区别?
Why do I see a difference when I convert a unix timestamp to datetime object in R?
> as.POSIXlt(1268736919, origin="1970-01-01", tz="America/New_York")
[1] "2010-03-16 06:55:19 EDT"
> as.POSIXct(1268736919, origin="1970-01-01", tz="America/New_York")
[1] "2010-03-16 11:55:19 EDT"
POSIXlt的结果实际上是正确的。
The result from POSIXlt is actually correct.
另外,是否有
谢谢
推荐答案
p>帮助页面实际上提示了一个区别:
The help page actually hints at a difference:
Value:
‘as.POSIXct’ and ‘as.POSIXlt’ return an object of the appropriate
class. If ‘tz’ was specified, ‘as.POSIXlt’ will give an
appropriate ‘"tzone"’ attribute.
这个东西很精致 - 我认为有一个隐含的TZ转换发生在 as.POSIXct
。考虑
This stuff is finicky -- I think there is an implicit TZ conversion happening for as.POSIXct
. Consider that
R> print(as.numeric(as.POSIXct(as.POSIXlt(1268736919,
origin="1970-01-01"))), digits=10)
[1] 1268736919
R> print(as.numeric(as.POSIXct(1268736919, origin="1970-01-01")), digits=10)
[1] 1268758519
第二个(使用 as.POSIXct
)不返回原始输入。不幸的是,Brian D. Ripley似乎是唯一拥有所有细节的人。
the second one (using as.POSIXct
) does not return the original input. Unfortunately, Brian D. Ripley seems to be the only human having all the details here.
最后,你不能没有起源。但是你可以定义使用时代作为起源的包装(如这里),或者使用2000-01-01或...只要保持一致。
Lastly, you can't do it without the origin. But you could define wrappers that use the epoch as origin (as here) or use 2000-01-01 or ... Just keep it consistent.
这篇关于将unix秒(毫秒)转换为POSIXct / POSIXlt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!