2

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.

mischva11
  • 2,811
  • 3
  • 18
  • 34
  • get timestamp 0 (as.POSIXct) to a date, find an online tool to give the .NET timestamp of such date. Now just remove to all timestamps this constant and you get POSIXct times – Giacomo Catenazzi Aug 10 '21 at 11:17

2 Answers2

3

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"
Julian_Hn
  • 2,086
  • 1
  • 8
  • 18
0

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"
TarJae
  • 72,363
  • 6
  • 19
  • 66