R 从标准 UTC 时区更改为多个本地时区

R Change from stardard UTC time zone to multiple local time zones

假设我有一个 df,其中包含对调查的一系列回复。每个响应都有一个 UTC 时间戳。我也碰巧知道每个填写调查的人的本地时区。

例如:

 df <-  data.frame(day = c("2018-12-06 15:40:29", "2018-12-06 15:25:28", 
"2018-12-06 15:25:28", "2018-12-06 14:09:09"), time_zone = c("EST", "PST", "CST", "EST"))

df$day <- as.POSIXct(df$day, tz = "UTC")

我想让所有这些日期反映参加调查的人在当地的时间。所以我尝试了以下方法:

df %>% 
  mutate(time_start = format(day, tz = time_zone))

但是我得到了Error in mutate_impl(.data, dots) : Evaluation error: invalid 'tz' value.

我也尝试过使用具有 GTM 格式时区的数据框

df<-  data.frame(day = c("2018-12-06 15:40:29", "2018-12-06 15:25:28", 
"2018-12-06 15:25:28", "2018-12-06 14:09:09"), 
time_zone = c("GMT-5", "GMT-6", "GMT-7", "GMT-8"))

是否有办法将一系列标准时间更改为本地时间?

这里有多个问题:

  1. format(以及其他与时间相关的函数)只为 tz;
  2. 采用长度为 1 的参数
  3. R识别的时区不包括流行的"CST""PST"

要解决第一个问题,使用 Mapmapply 就足够了。

不幸的是,第二个需要更多的研究。 "PST" 之类的时区虽然在其他国家或地区至少在美国很流行,但它们不是有效的时区字符串(参考:CCTZ,一个用于在时区之间进行转换的 C++ 库,says so). Neither are "GMT-7", et al, though the latter can be faked by prepending with Etc/,如: "Etc/GMT-7". 或者你可以选择 "America/New_York""US/Eastern".

df$time_zone <- c("US/Eastern", "US/Pacific", "US/Central", "US/Eastern")
df
#                   day  time_zone
# 1 2018-12-06 15:40:29 US/Eastern
# 2 2018-12-06 15:25:28 US/Pacific
# 3 2018-12-06 15:25:28 US/Central
# 4 2018-12-06 14:09:09 US/Eastern
mapply(format, df$day, tz = "GMT")
# [1] "2018-12-06 15:40:29" "2018-12-06 15:25:28" "2018-12-06 15:25:28"
# [4] "2018-12-06 14:09:09"
mapply(format, df$day, tz = df$time_zone)
# [1] "2018-12-06 10:40:29" "2018-12-06 07:25:28" "2018-12-06 09:25:28"
# [4] "2018-12-06 09:09:09"

R 时区的所有可立即识别的格式都可以在 594 元素向量中找到:

str(OlsonNames())
#  chr [1:592] "Africa/Abidjan" "Africa/Accra" "Africa/Addis_Ababa" ...
#  - attr(*, "Version")= chr "2018e"
set.seed(2)
sample(OlsonNames(), size=8)
# [1] "America/El_Salvador"  "Etc/GMT+8"            "Atlantic/Madeira"    
# [4] "America/Creston"      "Pacific/Port_Moresby" "Pacific/Ponape"      
# [7] "America/Atka"         "GB-Eire"             
grep("US/", OlsonNames(), value = TRUE)
#  [1] "US/Alaska"         "US/Aleutian"       "US/Arizona"       
#  [4] "US/Central"        "US/East-Indiana"   "US/Eastern"       
#  [7] "US/Hawaii"         "US/Indiana-Starke" "US/Michigan"      
# [10] "US/Mountain"       "US/Pacific"        "US/Pacific-New"   
# [13] "US/Samoa"         

在此示例中,您将看到可以使用的备选方案之一:"Etc/GMT+8"。请注意,+ 位于本初子午线的 西侧 ,因此

mapply(format, df$day, tz = "US/Eastern")
# [1] "2018-12-06 10:40:29" "2018-12-06 10:25:28" "2018-12-06 10:25:28"
# [4] "2018-12-06 09:09:09"
mapply(format, df$day, tz = "Etc/GMT+5")
# [1] "2018-12-06 10:40:29" "2018-12-06 10:25:28" "2018-12-06 10:25:28"
# [4] "2018-12-06 09:09:09"

注意买者:使用"US/Eastern"应该在适当的时候考虑夏令时; "Etc/GMT+5" 没有,我相信。