我正在尝试计算日期之间的时间长度,其中一些日期是从gmail中获取的,因此在PDT(America / Los_Angeles)或PST(同一时区的夏令时)中声明,具体取决于时间那一年。由于我的其他数据是在中欧时间(欧洲/苏黎世),我想将PDT和PST日期转换为CET(CEST)。
我使用以下命令将日期转换为适当的tz:
DF$sendback_date1[!is.na(DF$sendback_date1)] <-
force_tz(DF$sendback_date1[!is.na(DF$sendback_date1)],
tz = "Europe/Zurich")
现在问题:在我的mac上,force_tz命令产生以下错误消息:
Fehler in as.POSIXlt.character(time) : character string is not in a
standard unambiguous format
我看到这个命令在linux上运行。我错过了什么吗? Mac是否为时区使用其他名称?
感谢您的帮助!!
为了让我的问题更加清晰,我在这里发布了一个可重现的例子:
library(lubridate)
DF <- structure(list(sendback_date1 = c("12.05.31 11:12", "12.07.03 11:33", "12.04.24 09:59", "12.07.02 18:37", "12.07.02 20:02", "", "12.04.24 15:00", "", "12.03.02 13:37", "12.03.26 10:23", "", "12.04.24 09:20", "", "12.03.26 14:14", "12.03.02 10:20", "12.04.23 17:30", "12.05.30 11:03", "", "12.05.24 08:51", "12.03.30 10:02", "12.03.19 12:44"), sendback_date2 = c("", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""), send_date = c("12/05/22 01/17/10", "12/05/22 01/17/14", "12/04/23 07/27/29", "12/03/01 12/08/28", "12/03/23 07/46/59", "12/03/23 07/47/00", "12/04/23 07/27/30", "12/05/22 01/17/17", "12/03/01 12/08/30", "12/04/23 19/17/00", "12/03/01 21/29/00", "12/03/23 16/18/00", "12/05/22 10/00/00", "12/03/23 07/47/01", "12/03/01 12/08/32", "12/04/23 07/27/32", "12/05/22 01/17/20", "12/04/23 07/27/35", "12/05/22 01/17/21", "12/03/23 07/47/03", "12/03/01 12/08/34"), Daylight = c("PDT", "PDT", "PDT", "PST", "PDT", "PDT", "PDT", "PDT", "PST", "", "", "", "", "PDT", "PST", "PDT", "PDT", "PDT", "PDT", "PDT", "PST")), .Names = c("sendback_date1", "sendback_date2", "send_date", "Daylight"), row.names = 60:80, class = "data.frame")
DF[DF == ""] <- NA
DF$send_date <- gsub("^\\s+|\\s+$", "", DF$send_date)
DF
DF$sendback_date1[!is.na(DF$sendback_date1)] <- ymd_hm(DF$sendback_date1[!is.na(DF$sendback_date1)], tz = "Europe/Zurich")
DF$sendback_date2[!is.na(DF$sendback_date2)] <- ymd_hm(DF$sendback_date2[!is.na(DF$sendback_date2)], tz = "Europe/Zurich")
DF$send_date[!is.na(DF$send_date) & !is.na(DF$Daylight)] <- ymd_hms(DF$send_date[!is.na(DF$send_date) & !is.na(DF$Daylight)], tz = "America/Los_Angeles")
DF$send_date[!is.na(DF$send_date) & !is.na(DF$Daylight)] <- with_tz(DF$send_date[!is.na(DF$send_date) & !is.na(DF$Daylight)], tz = "Europe/Zurich")
DF$send_date[!is.na(DF$send_date) & is.na(DF$Daylight)] <- ymd_hms(DF$send_date[!is.na(DF$send_date) & is.na(DF$Daylight)], tz = "Europe/Zurich")
dput(head(DF)) structure(list(sendback_date1 = c("12.05.22 13:57", "12.04.24 09:17",
"12.04.10 13:26", "12.03.09 10:09", "12.03.02 12:39", "12.05.22 12:33" ),
sendback_date2 = c("12.05.31 11:55", NA, NA, NA, NA, NA),
sendback_date3 = c(NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_), send_date = c("12/05/22 01/16/28",
"12/04/23 07/26/52", "12/03/23 07/46/30", "12/03/01 12/07/59",
"12/03/01 12/08/00", "12/05/22 01/16/32"), Daylight = c("PDT",
"PDT", "PDT", "PST", "PST", "PDT")), .Names = c("sendback_date1", "sendback_date2",
"sendback_date3", "send_date", "Daylight"), row.names = c(NA, 6L), class = "data.frame")
> str(DF)
'data.frame': 1004 obs. of 5 variables:
$ sendback_date1: chr "12.05.22 13:57" "12.04.24 09:17" "12.04.10 13:26" "12.03.09 10:09" ...
$ sendback_date2: chr "12.05.31 11:55" NA NA NA ...
$ sendback_date3: chr NA NA NA NA ...
$ send_date : chr "12/05/22 01/16/28" "12/04/23 07/26/52" "12/03/23 07/46/30" "12/03/01 12/07/59" ...
$ Daylight : chr "PDT" "PDT" "PDT" "PST" ...