Doug Fir
Doug Fir

Reputation: 21282

Convert a unixtime into a datetime that keeps milliseconds

I'm working with some timestamps:

pageviews$timestamp %>% head()
[1] "1605859226452" "1605859226461" "1605859248803" "1605859261112" "1605859283839" "1605859471370"

I need to convert these to datetimes and preserve to the most granular level possible, I think milliseconds.

Tried earlier using as.POSIXct:

strtime_to_dt <- function(x) {
  as.POSIXct(as.numeric(x) / 1000, origin = "1960-01-01", formats = "%Y-%m-%d %H:%M:%OS")
}

x <- pageviews$timestamp %>% head() # same data as above
> strtime_to_dt(x)
[1] "2010-11-20 08:00:26 UTC" "2010-11-20 08:00:26 UTC" "2010-11-20 08:00:48 UTC" "2010-11-20 08:01:01 UTC" "2010-11-20 08:01:23 UTC"
[6] "2010-11-20 08:04:31 UTC"

This returns datetimes at the second level. How can I get it at the millisecond level?

(Would be great if doable in tidyverse or lubridate, but I'll take anything)

EDIT based on comments some timestamps:

 x
      timestamp
1 1605859226452
2 1605859226461
3 1605859248803
4 1605859261112
5 1605859283839
6 1605859471370

Updated function:

strtime_to_dt <- function(x) {format(strtime_to_dt(x), "%F %H:%M:%OS3")}

strtime_to_dt(x)
Error: C stack usage  7969908 is too close to the limit

Adding sessionInfo()

sessionInfo()
R version 4.0.3 (2020-10-10)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04 LTS

Matrix products: default
BLAS/LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.8.so

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=C              LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] DBI_1.1.0              jsonlite_1.7.1         googleAnalyticsR_0.8.0 lubridate_1.7.9        forcats_0.5.0         
 [6] stringr_1.4.0          dplyr_1.0.2            purrr_0.3.4            readr_1.4.0            tidyr_1.1.2           
[11] tibble_3.0.4           ggplot2_3.3.2          tidyverse_1.3.0       

loaded via a namespace (and not attached):
 [1] tinytex_0.27      tidyselect_1.1.0  xfun_0.19         haven_2.3.1       gargle_0.5.0      colorspace_1.4-1  vctrs_0.3.4      
 [8] generics_0.1.0    utf8_1.1.4        blob_1.2.1        rlang_0.4.8       pillar_1.4.6      withr_2.3.0       glue_1.4.2       
[15] bit64_4.0.5       dbplyr_2.0.0      modelr_0.1.8      readxl_1.3.1      lifecycle_0.2.0   munsell_0.5.0     gtable_0.3.0     
[22] cellranger_1.1.0  rvest_0.3.6       memoise_1.1.0     curl_4.3          fansi_0.4.1       broom_0.7.2       Rcpp_1.0.5       
[29] openssl_1.4.3     backports_1.2.0   scales_1.1.1      fs_1.5.0          bit_4.0.4         googleAuthR_1.3.1 askpass_1.1      
[36] hms_0.5.3         digest_0.6.27     stringi_1.5.3     grid_4.0.3        odbc_1.3.0        cli_2.1.0         tools_4.0.3      
[43] magrittr_1.5      pacman_0.5.1      crayon_1.3.4      pkgconfig_2.0.3   ellipsis_0.3.1    xml2_1.3.2        reprex_0.3.0     
[50] assertthat_0.2.1  httr_1.4.2        rstudioapi_0.11   R6_2.5.0          compiler_4.0.3  

Upvotes: 1

Views: 486

Answers (1)

akrun
akrun

Reputation: 887511

We can do

format(strtime_to_dt(as.character(x$timestamp)), "%F %H:%M:%OS3") 
#[1] "2010-11-20 02:00:26.451" "2010-11-20 02:00:26.460" "2010-11-20 02:00:48.802" "2010-11-20 02:01:01.111" "2010-11-20 02:01:23.838"
#[6] "2010-11-20 02:04:31.369"    

data

x <- structure(list(timestamp = c(1605859226452, 1605859226461, 1605859248803, 
1605859261112, 1605859283839, 1605859471370)), class = "data.frame", row.names = c("1", 
"2", "3", "4", "5", "6"))

Upvotes: 1

Related Questions