将UNIX纪元转换为日期对象

时间:2022-05-13 11:26:09

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 (e.g. 1352068320), but Date objects seem more appropriate for plotting. How can I do the conversion?

我在均匀分布的时间序列上绘制和执行计算。时间戳目前存储为表示UNIX纪元以来秒数的整数(例如1352068320),但是日期对象似乎更适合用于绘图。如何进行转换?

I've read ?Date, ?as.Date and ??epoch, but seem to have missed that information.

我读过约会,?。日期和? ?但似乎漏掉了那个信息。

1 个解决方案

#1


152  

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:

编辑:几年后,我们现在可以使用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.

注意所有这些如何工作,没有任何格式或源参数。

#1


152  

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:

编辑:几年后,我们现在可以使用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.

注意所有这些如何工作,没有任何格式或源参数。