将UNIX时代转换为Date对象 [英] Convert UNIX epoch to Date object

查看:75
本文介绍了将UNIX时代转换为Date对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在对均匀分布的时间序列进行绘图和计算.时间戳当前以整数形式存储,代表自UNIX纪元以来的秒数(例如1352068320),但是Date对象似乎更适合绘制.我该如何进行转换?

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?

我已经读过?Date?as.Date??epoch,但是似乎错过了这些信息.

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

推荐答案

通过POSIXct,您想要在其中设置TZ-在这里,您可以看到我的(芝加哥)默认值:

Go via POSIXct and you want to set a TZ there -- here you see my (Chicago) default:

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> 

几年后,我们现在可以随时使用 包:

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> 

请注意,在没有任何格式或原始参数的情况下,所有这些工作原理如何.

这篇关于将UNIX时代转换为Date对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆