简体   繁体   English

如何在R中转换unix时间戳(毫秒)和时区?

[英]How to convert unix timestamp (milliseconds) and timezone in R?

I have data which has two columns time and timezone that have the timestamp of events.我的数据有两列timetimezone ,其中包含事件的时间戳。 Examples are:例子是:

time               timezone
1433848856453      10800000

It seems like the timestamp has fractional seconds in the information as well.似乎时间戳在信息中也有小数秒。 I don't understand the timezone format but it must be an equivalent unix format.我不明白时区格式,但它必须是等效的 unix 格式。 I need to preserve the fractional seconds as well.我也需要保留小数秒。 How do I go from that to something like in R?我如何从那个变成类似于 R 的东西?

2015-01-01 13:34:56.45 UTC

Note: This human readable date is not the actual converted values of the unix timestamp shown.注意:这个人类可读的日期不是显示的 unix 时间戳的实际转换值。

It looks like the timezone column is the timezone offset, in milliseconds.看起来timezone列是时区偏移量,以毫秒为单位。 I assume that means the timezone column will adjust for daylight saving time manually我认为这意味着时区列将手动调整夏令时

So you should add the time and timezone columns before converting to POSIXct .因此,您应该在转换为POSIXct之前添加timetimezone列。 You should also set the tz to "UTC" so no DST adjustments will be made to your POSIXct object.您还应该将tz设置为"UTC"这样就不会对您的POSIXct对象进行 DST 调整。

R> time <- 1433848856453
R> timezone <- 10800000
R> options(digits.secs=3)
R> .POSIXct((time+timezone)/1000, tz="UTC")
[1] "2015-06-09 14:20:56.453 UTC"

I think this can be right for you.我认为这可能适合你。

aa <- 1433848856453
as.POSIXct(aa/1000, origin="1970-01-01")
[1] "2015-06-09 13:20:56.453 CEST"

Edit编辑

Now I've realized that my code is not reproducible, because of my personal settings, but this can fix it and let you achieve the goal.现在我意识到我的代码不可重现,因为我的个人设置,但这可以修复它并让您实现目标。

options(digits.secs=6)

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

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