将 UNIX 纪元转换为日期对象 [英] Convert UNIX epoch to Date object

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

问题描述

我正在对均匀分布的时间序列进行绘图和计算.时间戳当前存储为整数,表示自 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.

推荐答案

Go via POSIXct 并且你想在那里设置一个 TZ -- here you see my (Chicago) 默认:

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> 

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

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.

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

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