0

I want to convert a character sequence of hourly french times into POSIXct.
This sequence includes a dst clock change at 02:00 (CEST => CET) which causes duplicated output times.

char_dates <- c("2017-10-29 01:00:00",
                "2017-10-29 02:00:00",
                "2017-10-29 02:00:00",
                "2017-10-29 03:00:00")
ymd_hms(char_dates, tz = "Europe/Paris")

Output:

"2017-10-29 01:00:00 CEST" "2017-10-29 02:00:00 CET"  "2017-10-29 02:00:00 CET"  "2017-10-29 03:00:00 CET" 

Desired output (note the 2nd timezone):

"2017-10-29 01:00:00 CEST" "2017-10-29 02:00:00 CEST"  "2017-10-29 02:00:00 CET"  "2017-10-29 03:00:00 CET" 

What is a good solution to achieve this ?
We can assume the dates are sorted, or that the user can provide a boolean vector (dst: yes/no) to tell lubridate which timezone to choose.

jlesuffleur
  • 1,113
  • 1
  • 7
  • 19

1 Answers1

0

Check if there are duplicates (from the end) and subtract 3600 (one hour in seconds) for the duplicate values. If you have a vector of Boolean, use that instead of duplicated(temp, fromLast = TRUE).

char_dates <- c("2017-10-29 01:00:00",
                "2017-10-29 02:00:00",
                "2017-10-29 02:00:00",
                "2017-10-29 03:00:00")
temp = ymd_hms(char_dates, tz = "Europe/Paris")
temp[duplicated(temp, fromLast = TRUE)] = temp[duplicated(temp, fromLast = TRUE)] - 3600
temp
#[1] "2017-10-29 01:00:00 CEST" "2017-10-29 02:00:00 CEST" "2017-10-29 02:00:00 CET" 
#[4] "2017-10-29 03:00:00 CET" 
d.b
  • 32,245
  • 6
  • 36
  • 77