18 digit timestamp to regular date format

Viewed 224

I've got a dataset with 18 digit timestamps.

How can I transform those to a regular date-format, like "%Y-%m-%d %H:%M:%0S6"?

For reproduction one time stamp is: 637201135513320448


I tried using the as.POSIXct() function like mentioned here, but it returns NA. I think it's not made for this many digits. I found a hint on c# here, where someone had a similar problem. But I am not able to solve this on R.

2 Answers

If this is indeed a C# type timestamp you first need to convert it to the right values for as.POSIXct to work:

timestamp <- 637201135513320448
epoch0.Csharp <- 621355968000000000 # value of Unix Time Origin in C# timestamp

timestamp.conv <- (timestamp - epoch0.Csharp) / 1e7 # C# has 10000 ticks per millisecond == 1e7 ticks per second which is the unit of the unix timestamp

dateTime <- as.POSIXct(timestamp.conv,origin="1970-01-01")

dateTime 
[1] "2020-03-18 08:32:31 CET"

Based on the direction by Julian_Hn(credits) here is a function using lubridate:

library(lubridate)

my_18digit_converter <- function(x){
  y <- (x - 621355968000000000) / 1e7
  lubridate::as_datetime(y)
}

my_18digit_converter(x)

Output:

> my_18digit_converter(x)
[1] "2020-03-18 07:32:31 UTC"
Related