简体   繁体   English

将时间戳/区域转换为R日期时间

[英]Converting timestamp/Zone to R datetime

I have the following timestamp: "03-APR-06 12.41.00.000000000 PM US/CENTRAL" and need to convert it to an R compatible timestamp. 我有以下时间戳: "03-APR-06 12.41.00.000000000 PM US/CENTRAL" ,需要将其转换为R兼容的时间戳。

I have tried: 我努力了:

structure(df2$ACTION_IN_DTTM_TZ,class=c('POSIXct'))
parse_date_time(df2$ACTION_IN_DTTM_TZ, "abdHMSzY")
strptime(df2$ACTION_IN_DTTM_TZ,format='%d/%b/%Y:%H:%M:%S:%p:%Z')

and all of them generated NAs 他们都产生了NA

structure(df2$ACTION_IN_DTTM_TZ,class=c('POSIXct'))
parse_date_time(df2$ACTION_IN_DTTM_TZ, "abdHMSzY")
strptime(df2$ACTION_IN_DTTM_TZ,format='%d/%b/%Y:%H:%M:%S:%p:%Z')

I would like this: 2012-08-10 04:42:47 我想要这样: 2012-08-10 04:42:47

Any suggestions are greatly appreciated! 任何建议,不胜感激! Thank you! 谢谢!

I did something like: 我做了类似的事情:

a <-  "03-APR-06 12.41.00.000000000 PM US/CENTRAL"
b <- (substr(a,1,18))
c <- as.POSIXct(b,format='%d-%b-%y %H.%M.%S')

Package lubridate provides functionality for this. 软件包lubridate提供了功能。 I extracted the timezone with a simple str_extract command. 我用一个简单的str_extract命令提取了时区。

library(lubridate)
library(stringr)
timestamp <- "03-APR-06 12.41.00.000000000 PM US/CENTRAL"
as_datetime(timestamp,tz=str_extract(timestamp,"\\S*$"))
[1] "2003-04-06 00:41:00 CST"

#without lubridate
strptime(strsplit(timestamp," \\S*$")[[1]][1],format="%y-%b-%d %I.%M.%S.%OS %p",tz=str_extract(timestamp,"\\S*$"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM