简体   繁体   English

将数字(integer64类)UNIX时间戳转换为日期时间类

[英]Convert numeric (integer64 class) UNIX timestamp to date time class

Similar Q&A everywhere, but none helped me to overcome the following error (I am trying to convert unix time to date-time format): 到处都有类似的问答,但没有一个能帮助我克服以下错误(我正在尝试将Unix时间转换为日期时间格式):

> cur196$time
integer64
  [1] 1566204590000 1566204585000 1566204580000 1566204570000 1566204560000 1566204550000 1566204531000 1566204525000 1566204521000 1566204501000
 [11] 1566204495000 1566204491000 1566204481000 1566204464000 1566204461000 1566204451000 1566204441000 1566204434000 1566204431000 1566204420000
 [21] ...
>   cur196$time <- as.POSIXct(cur196$time, origin = "1970-01-01", tz = "GMT")
Error in as.POSIXct.default(cur196$time, origin = "1970-01-01", tz = "GMT") : 
  do not know how to convert 'cur196$time' to class “POSIXct”

EDIT: 编辑:

> dput(head(cur196$time))
structure(c(7.73807882277875e-312, 7.73807879807547e-312, 7.73807877337218e-312, 
7.73807872396562e-312, 7.73807867455905e-312, 7.73807862515249e-312
), class = "integer64")

EDIT 2: 编辑2:

@zx8754 thank you very much for changing the title and thus pointing out the real problem - unix time stamps are in miliseconds and thus to large for conversion. @ zx8754非常感谢您更改标题,并指出了实际的问题-Unix时间戳以毫秒为单位,因此转换时就很大。

The problem is that your data has the class integer64 from the bit64 package. 问题是,你的数据有类integer64bit64包。 You need to convert it to a normal integer with as.integer() , while the bit64 package is loaded. 您需要在加载bit64软件包时使用as.integer()将其转换为普通整数。 Then you can use as.POSIXct() on it. 然后,您可以在其上使用as.POSIXct()

But probably you should look into how you imported the data and why it is saved that way in the first place. 但是可能您应该首先研究如何导入数据以及为什么以这种方式保存数据。 Does it need to be a 64-bit integer? 是否需要为64位整数?

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

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