将Unix时间戳读为Posixct

发布于 2025-02-03 18:22:52 字数 695 浏览 2 评论 0原文

我需要阅读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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

極樂鬼 2025-02-10 18:22:52

您可以检查这是您要的输出吗?

library(tidyverse)
library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union

time.char <- tibble(time = "Wed Jun  1 15:22:41 EDT 2022")

time.char %>%
  mutate(time = str_replace_all(time, "  ", " ")) %>%
  separate(
    time,
    into = c("abb_weekday", "month", "day", "time", "tz", "year"),
    sep = " "
  ) %>%
  mutate(
    time_conc = paste0(year, "-", month, "-", day,
                       " ", time),
    time_adj = as.POSIXct(
      time_conc,
      tz = "America/New_York",
      tryFormats = c("%Y-%b-%d %H:%M:%OS")
    )
  )
#> # A tibble: 1 × 8
#>   abb_weekday month day   time     tz    year  time_conc     time_adj           
#>   <chr>       <chr> <chr> <chr>    <chr> <chr> <chr>         <dttm>             
#> 1 Wed         Jun   1     15:22:41 EDT   2022  2022-Jun-1 1… 2022-06-01 15:22:41

Can you check if this is the output you're asking for?

library(tidyverse)
library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union

time.char <- tibble(time = "Wed Jun  1 15:22:41 EDT 2022")

time.char %>%
  mutate(time = str_replace_all(time, "  ", " ")) %>%
  separate(
    time,
    into = c("abb_weekday", "month", "day", "time", "tz", "year"),
    sep = " "
  ) %>%
  mutate(
    time_conc = paste0(year, "-", month, "-", day,
                       " ", time),
    time_adj = as.POSIXct(
      time_conc,
      tz = "America/New_York",
      tryFormats = c("%Y-%b-%d %H:%M:%OS")
    )
  )
#> # A tibble: 1 × 8
#>   abb_weekday month day   time     tz    year  time_conc     time_adj           
#>   <chr>       <chr> <chr> <chr>    <chr> <chr> <chr>         <dttm>             
#> 1 Wed         Jun   1     15:22:41 EDT   2022  2022-Jun-1 1… 2022-06-01 15:22:41

Created on 2022-06-02 by the reprex package (v2.0.1)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文