在R中阅读不明确的日期时间

2j4z5cfb  于 2023-03-27  发布在  其他
关注(0)|答案(2)|浏览(75)

请考虑以下几点:

> as.POSIXct('2021-11-07 01:30', tz='US/Central')
[1] "2021-11-07 01:30:00 CST"

注意我是如何得到CST的,如果我想要'CDT'呢?
在Python-pandas中,我可以使用ambiguous

In [5]: pd.Timestamp('2021-11-07 01:30:00').tz_localize('US/Central', ambiguous=False)
Out[5]: Timestamp('2021-11-07 01:30:00-0600', tz='US/Central')

In [6]: pd.Timestamp('2021-11-07 01:30:00').tz_localize('US/Central', ambiguous=True)
Out[6]: Timestamp('2021-11-07 01:30:00-0500', tz='US/Central')
idv4meu8

idv4meu81#

如果你使用clock'sdate_time_parse(),那么默认情况下会在不明确的日期时间上出错。你可以使用ambiguous来决定如何处理它们,这允许你选择最早或最晚的选项:

library(clock)

# Error by default
date_time_parse("2021-11-07 01:30", zone = "US/Central", format = "%Y-%m-%d %H:%M")
#> Error in `stop_clock()`:
#> ! Ambiguous time due to daylight saving time at location 1.
#> ℹ Resolve ambiguous time issues by specifying the `ambiguous` argument.

# CDT
date_time_parse("2021-11-07 01:30", zone = "US/Central", format = "%Y-%m-%d %H:%M", ambiguous = "earliest")
#> [1] "2021-11-07 01:30:00 CDT"

# CST
date_time_parse("2021-11-07 01:30", zone = "US/Central", format = "%Y-%m-%d %H:%M", ambiguous = "latest")
#> [1] "2021-11-07 01:30:00 CST"
hgc7kmma

hgc7kmma2#

通常显示“CST”

seq(as.POSIXct("2021-11-07 01:58", tz="US/Central"), 
  as.POSIXct("2021-11-07 02:03", tz="US/Central"), "min")
[1] "2021-11-07 01:58:00 CST" "2021-11-07 01:59:00 CST"
[3] "2021-11-07 02:00:00 CST" "2021-11-07 02:01:00 CST"
[5] "2021-11-07 02:02:00 CST" "2021-11-07 02:03:00 CST"

但是我们可以通过返回一个小时(3600秒)来欺骗它显示以前的“CDT”

seq(as.POSIXct("2021-11-07 01:58", tz="US/Central") - 3600, 
  as.POSIXct("2021-11-07 02:03", tz="US/Central") - 3600, "min")
[1] "2021-11-07 01:58:00 CDT" "2021-11-07 01:59:00 CDT"
[3] "2021-11-07 01:00:00 CST" "2021-11-07 01:01:00 CST"
[5] "2021-11-07 01:02:00 CST" "2021-11-07 01:03:00 CST"

就你而言

as.POSIXct("2021-11-07 01:30", tz="US/Central") - 3600
[1] "2021-11-07 01:30:00 CDT"

相关问题