3

I am trying to calculate the length of time between dates some of which have been taken from gmail and are therefor stated in PDT(America/Los_Angeles) or PST (Daylight Saving time of the same time zone) depending on the time of the year. As the rest of my data is in Central European Time (Europe/Zurich) I want to convert the PDT and PST dates into CET(CEST).

I used the following command to convert the dates into the appropriate tz:

DF$sendback_date1[!is.na(DF$sendback_date1)] <-
    force_tz(DF$sendback_date1[!is.na(DF$sendback_date1)],
             tz = "Europe/Zurich")

Now the problem: On my mac the force_tz command produces the following error message:

Fehler in as.POSIXlt.character(time) :    character string is not in a
standard unambiguous format

I saw this command work on linux. Am I missing something? Does Mac use other names for the time zones?

Thanks for your help!!

To make my problem clearer, I post you a reproducible example, here:

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" ...
4

0 回答 0