将Unix时间戳读为Posixct
我需要阅读Unix的时间戳。例子: 6月1 15:22:41 EDT 2022
我尝试使用TZ参数在AS.Posixct的时区中阅读。
time.char <- "Wed Jun 1 15:22:41 EDT 2022"
time.char <- sub("^... ","",time.char)
tz <- sub(".+ ([^ ]+) [0-9]{4}$","\\1",time.char)
timedate <- sub("(.+) [^ ]+ ([0-9]{4})$","\\1 \\2",time.char)
time.posixct <- as.POSIXct(timedate,format="%b %d %H:%M:%S %Y",tz=tz)
## I thought I was done
print(time.posixct)
[1] "2022-06-01 15:22:41 EDT"
## But the timezone isn't real. UTC is used.
time.now <- Sys.time()
print(time.now)
[1] "2022-06-01 15:27:25 EDT"
time.now-time.posixct
Time difference of 4.079089 hours
问题似乎是我需要做TZ =“ America/new_york”。如何使用UNIX提供的三个字母的时区代码?
I need to read timestamps from unix. Example:
Wed Jun 1 15:22:41 EDT 2022
I tried to read in the timezone in as.POSIXct using the tz argument.
time.char <- "Wed Jun 1 15:22:41 EDT 2022"
time.char <- sub("^... ","",time.char)
tz <- sub(".+ ([^ ]+) [0-9]{4}quot;,"\\1",time.char)
timedate <- sub("(.+) [^ ]+ ([0-9]{4})quot;,"\\1 \\2",time.char)
time.posixct <- as.POSIXct(timedate,format="%b %d %H:%M:%S %Y",tz=tz)
## I thought I was done
print(time.posixct)
[1] "2022-06-01 15:22:41 EDT"
## But the timezone isn't real. UTC is used.
time.now <- Sys.time()
print(time.now)
[1] "2022-06-01 15:27:25 EDT"
time.now-time.posixct
Time difference of 4.079089 hours
The problem seems to be I need to do tz="America/New_York". How can I make use of the three-letter time zones codes provided by unix?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以检查这是您要的输出吗?
由
Can you check if this is the output you're asking for?
Created on 2022-06-02 by the reprex package (v2.0.1)