Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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. I need to preserve the fractional seconds as well. How do I go from that to something like in 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.

like image 520
sfactor Avatar asked Mar 16 '23 15:03

sfactor


2 Answers

It looks like the timezone column is the timezone offset, in milliseconds. 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. You should also set the tz to "UTC" so no DST adjustments will be made to your POSIXct object.

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"
like image 136
Joshua Ulrich Avatar answered Mar 18 '23 04:03

Joshua Ulrich


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)
like image 36
SabDeM Avatar answered Mar 18 '23 05:03

SabDeM