How to convert unix timestamp (milliseconds) and timezone in R?
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"
Convert UNIX epoch to Date object
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>
Edit: 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.
Convert data frame with epoch timestamps to time-series with milliseconds in R
Your timestamps are in milliseconds. You need to convert them to seconds to be able to use them with as.POSIXct
. And there's no point in calling strptime
on a POSIXct vector.
Also, it's good practice to explicitly set the timezone, rather than leave it set to ""
.
df$datetime <- as.POSIXct(df$timestamp/1000, origin="1970-01-01", tz="UTC")
options(digits.secs=6)
df
# timestamp session datetime
# 1 1.42832e+12 A 2015-04-06 11:29:30.510
# 2 1.42832e+12 A 2015-04-06 11:29:57.217
# 3 1.42832e+12 B 2015-04-06 11:29:58.181
# 4 1.42832e+12 A 2015-04-06 11:30:03.326
# 5 1.42832e+12 A 2015-04-06 11:30:08.477
I'm not sure why you aren't seeing millisecond resolution...
R - converting a timestamp to a local time and date
You get the same output in all 3 cases because 1) and 3) are same (UTC and GMT) whereas 2) (DST) is not a valid tz
value.
If you don't mention the timezone value it should by default give you time in your local time zone.
as.POSIXct(as.numeric('1594598065352')/1000, origin="1970-01-01")
Alternatively, you can run OlsonNames()
in your console to get list of valid timezones in R. 'Etc/GMT-2'
seems to be the one for you.
as.POSIXct(as.numeric('1594598065352')/1000,origin="1970-01-01", tz = 'Etc/GMT-2')
#[1] "2020-07-13 01:54:25 +02"
how to extract year, tz-convert and get millisecond part from a nanotime timestamp?
I do all of this with data.table
because it is known that data.table
supports the underlying bit64
package and integer64
representation that is needed here. Other containers do not.
Code
library(nanotime)
library(data.table)
DT <- data.table(ts = c(nanotime('2011-12-05 08:30:00.000',format ="%Y-%m-%d %H:%M:%E9S", tz ="GMT"),
nanotime('2011-12-05 08:30:00.700',format ="%Y-%m-%d %H:%M:%E9S", tz ="GMT"),
nanotime('2011-12-05 08:30:00.825',format ="%Y-%m-%d %H:%M:%E9S", tz ="GMT")))
DT[, pt := as.POSIXct(ts)]
DT[, millis := as.numeric(pt - trunc(pt)) * 1e3]
Result
R> DT
ts pt millis
1: 2011-12-05T08:30:00.000000000+00:00 2011-12-05 02:30:00.000 0
2: 2011-12-05T08:30:00.700000000+00:00 2011-12-05 02:30:00.700 700
3: 2011-12-05T08:30:00.825000000+00:00 2011-12-05 02:30:00.825 825
R>
Timezone shifts are a different (and misunderstood) topic. You can do it to POSIXct
.
Note that all you have done here / asked for here was the millisecond resolution. So far no need was demonstrated for nanotime
. But what I showed you can work on nanoseconds -- I use it every day from data.table
.
Related Topics
Creating New Shape Palettes in Ggplot2 and Other R Graphics
Using Variable Value as Column Name in Data.Frame or Cbind
Loop Through a Series of Qplots
Rscript Could Not Find Function
R: Compare All the Columns Pairwise in Matrix
Update a Ggplot Using a for Loop (R)
R: Calculate Means for Subset of a Group
Split or Separate Uneven/Unequal Strings with No Delimiter
Fixing a Multiple Warning "Unknown Column"
R Define Dimensions of Empty Data Frame
Can Sparklyr Be Used with Spark Deployed on Yarn-Managed Hadoop Cluster
Joining Factor Levels of Two Columns
Extracting Data from Text Files
1-Dimensional Matrix Is Changed to a Vector in R
Change the Order of Stacked Fill Columns in Ggplot2
Update Subset of Values in a Dataframe Column