【问题标题】:as.POSIXct is mishandling timestamp with leading zero in the milliseconds portion [duplicate]as.POSIXct 错误处理时间戳,毫秒部分前导零 [重复]
【发布时间】:2021-09-11 05:04:24
【问题描述】:

as.POSIXct 似乎是错误处理时间戳,其中字符串的毫秒部分有前导 0,例如“.043”

time_043_millis <- "20210909-20:05:10.043"    #doesnt work
time_143_millis <- "20210909-20:05:10.143"    #works

转换以“.043”毫秒结尾的时间戳时,结果错误

as.POSIXct(time_043_millis, format = "%Y%m%d-%H:%M:%S.%OS")

#Returns:
#[1] "2021-09-09 20:05:43 AEST"

# its messed up, the seconds portion should be 10, and the 43 milliseconds as a fraction of a second, but its taken 43 to be seconds portion??

转换以“.143”毫秒结尾的时间戳时,结果正确。


as.POSIXct(time_143_millis, format = "%Y%m%d-%H:%M:%S.%OS")

#Returns:
#[1] "2021-09-09 20:05:10 AEST"

# This time its working correctly.. why??

我在 windows 系统和 linux 系统上测试过同样的行为。

对此有什么解释吗?

这是一个错误吗?

来自 Linux 和 windows 系统的 R 会话信息包含在下面的代码块中...

提前致谢。

#Windows System

> sessionInfo()
R version 4.0.2 (2020-06-22)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows Server 2012 R2 x64 (build 9600)

Matrix products: default

locale:
[1] LC_COLLATE=English_Australia.1252  LC_CTYPE=English_Australia.1252    LC_MONETARY=English_Australia.1252
[4] LC_NUMERIC=C                       LC_TIME=English_Australia.1252    

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

loaded via a namespace (and not attached):
 [1] colorspace_1.4-1 scales_1.1.1     compiler_4.0.2   R6_2.4.1         tools_4.0.2      tinytex_0.29    
 [7] xfun_0.21        lifecycle_0.2.0  munsell_0.5.0    rlang_0.4.6     


Linux system:
> sessionInfo()
R version 4.1.0 (2021-05-18)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Red Hat Enterprise Linux

Matrix products: default
BLAS/LAPACK: /usr/lib64/libopenblasp-r0.3.3.so

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8     LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                  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] lubridate_1.7.10 forcats_0.5.1    stringr_1.4.0    dplyr_1.0.7      purrr_0.3.4      readr_1.4.0      tidyr_1.1.3      tibble_3.1.2     ggplot2_3.3.4    tidyverse_1.3.1 

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.6        cellranger_1.1.0  pillar_1.6.1      compiler_4.1.0    dbplyr_2.1.1      tools_4.1.0       digest_0.6.27     evaluate_0.14     jsonlite_1.7.2   
[10] lifecycle_1.0.0   gtable_0.3.0      pkgconfig_2.0.3   rlang_0.4.11      reprex_2.0.0      rstudioapi_0.13   DBI_1.1.1         cli_2.5.0         yaml_2.2.1       
[19] haven_2.4.1       xfun_0.24         xml2_1.3.2        withr_2.4.2       httr_1.4.2        knitr_1.33        fs_1.5.0          generics_0.1.0    vctrs_0.3.8      
[28] hms_1.1.0         grid_4.1.0        tidyselect_1.1.1  glue_1.4.2        R6_2.5.0          fansi_0.5.0       readxl_1.3.1      rmarkdown_2.9     farver_2.1.0     
[37] modelr_0.1.8      magrittr_2.0.1    backports_1.2.1   scales_1.1.1      ellipsis_0.3.2    htmltools_0.5.1.1 rvest_1.0.0       assertthat_0.2.1  colorspace_2.0-1 
[46] labeling_0.4.2    utf8_1.2.1        stringi_1.7.3     munsell_0.5.0     broom_0.7.7       crayon_1.4.1     

【问题讨论】:

    标签: r linux posixct


    【解决方案1】:

    您使用的格式不正确。试试 -

    time_043_millis <- "20210909-20:05:10.043"    
    time_143_millis <- "20210909-20:05:10.143" 
    
    as.POSIXct(time_043_millis, format = "%Y%m%d-%H:%M:%OS", tz = 'UTC')
    #[1] "2021-09-09 20:05:10 UTC"
    
    as.POSIXct(time_143_millis, format = "%Y%m%d-%H:%M:%OS", tz = 'UTC')
    #[1] "2021-09-09 20:05:10 UTC"
    

    【讨论】:

    • 谢谢,直到今天我才有机会重新测试。你能指出这是记录在哪里吗?
    • ?strptimeFurther, for strptime %OS will input seconds including fractional seconds
    【解决方案2】:

    我们可以使用ymd_hms

    library(lubridate)
     ymd_hms(time_043_millis)
    [1] "2021-09-09 20:05:10 UTC"
    ymd_hms(time_143_millis)
    [1] "2021-09-09 20:05:10 UTC"
    

    数据

    time_043_millis <- "20210909-20:05:10.043"    
    time_143_millis <- "20210909-20:05:10.143" 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-30
      • 2021-06-19
      • 2016-03-04
      • 2015-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多