简体   繁体   English

将 UNIX 纪元转换为日期对象

[英]Convert UNIX epoch to Date object

I'm plotting and performing calculations on uniformly distributed time series.我正在对均匀分布的时间序列进行绘制和计算。 The timestamps are currently stored as integers representing the number of seconds since the UNIX epoch (eg 1352068320 ), but Date objects seem more appropriate for plotting.时间戳当前存储为整数,表示自 UNIX 时代以来的秒数(例如1352068320 ),但Date对象似乎更适合绘图。 How can I do the conversion?我该如何进行转换?

I've read ?Date , ?as.Date and ??epoch , but seem to have missed that information.我读过?Date?as.Date??epoch ,但似乎错过了这些信息。

Go via POSIXct and you want to set a TZ there -- here you see my (Chicago) default:通过去POSIXct ,你要设置TZ存在-在这里你看到我(芝加哥)默认:

R> val <- 1352068320
R> as.POSIXct(val, origin="1970-01-01")
[1] "2012-11-04 22:32:00 CST"
R> as.Date(as.POSIXct(val, origin="1970-01-01"))
[1] "2012-11-05" 
R> 

Edit: A few years later, we can now use the anytime package:编辑:几年后,我们现在可以使用随时包:

R> library(anytime)
R> anytime(1352068320)
[1] "2012-11-04 16:32:00 CST"
R> anydate(1352068320)
[1] "2012-11-04"
R> 

Note how all this works without any format or origin arguments .注意所有这些是如何在没有任何格式或原点参数的情况下工作的

With library(lubridate) , numeric representations of date and time saved as the number of seconds since 1970-01-01 00:00:00 UTC, can be coerced into dates with as_datetime() :使用library(lubridate) ,日期和时间的数字表示保存为自 1970-01-01 00:00:00 UTC 以来的秒数,可以使用as_datetime()强制转换为日期:

lubridate::as_datetime(1352068320)

[1] "2012-11-04 22:32:00 UTC"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM