0

For some strange reasons, I'm having problems converting the date 2008-10-26 02:00:00 to POSIXct in R. I have been working on millions of dates and it seems that I always stumble across the same problem with dates ranging from 2008-10-26 02:00:00 to 2008-10-26 02:59:59 (anything with 2008-10-26 2pm).

For example, it works normally with:

> as.POSIXct("2008-10-26 03:00")
[1] "2008-10-26 03:00:00 +05"

But with 2008-10-26 02:00:00:

> as.POSIXct("2008-10-26 02:00")
[1] "2008-10-26 +04"

The dates were originally in a data frame in the format %Y-%m-%dT%H:%M:%SZ. I used the code measurement_date = as.POSIXct(df_txt$V1, format = "%Y-%m-%dT%H:%M:%SZ") to convert all the dates to POSIXct, and in the data frame I'm left with NAs, as shown:

> df[80960:80970,]
      study_id    measurement_date oxy_conc ph salinity  temp
80960       87 2008-10-26 01:50:00       NA NA       NA 10.38
80961       87                <NA>       NA NA       NA 10.37
80962       87                <NA>       NA NA       NA 10.36
80963       87                <NA>       NA NA       NA 10.36
80964       87                <NA>       NA NA       NA 10.35
80965       87                <NA>       NA NA       NA 10.33
80966       87                <NA>       NA NA       NA 10.33
80967       87 2008-10-26 03:00:00       NA NA       NA 10.34
80968       87 2008-10-26 03:10:00       NA NA       NA 10.35
80969       87 2008-10-26 03:20:00       NA NA       NA 10.34
80970       87 2008-10-26 03:30:00       NA NA       NA 10.36

I even tried working around the problem through calculations. For example, in the code below I tried adding 10 minutes to 2008-10-26 01:50, but even here it fails:

> as.POSIXct("2008-10-26 01:50")+60*10
[1] "2008-10-26 03:00:00 +05"

Any help will be greatly appreciated. Thank you very much!

Darren LSH
  • 35
  • 7
  • 2
    DST change in your local timezone perhaps? Those are *local* dates. Your conversion actually broke the data. `YYYY-MM-DDTHH:mm:ssZ` is the ISO8601 format and `Z` means the time is UTC. It's an unambiguous format. By modifying this you converted the dates to local time – Panagiotis Kanavos Aug 25 '20 at 14:46
  • 1
    this is almost certainly a duplicate: try searching SO answers for "time zone [r]" ? – Ben Bolker Aug 25 '20 at 14:48
  • To fix this, first of all *revert the change to the original ISO8601 date literals*. Use `strptime` to parse them – Panagiotis Kanavos Aug 25 '20 at 14:48
  • Maybe `as.POSIXlt("2008-10-26 03:00",tz = 'GMT')` – Duck Aug 25 '20 at 14:52
  • I had no idea this had to do with timezones. Initially thought it was a bug. Thanks everyone! – Darren LSH Aug 25 '20 at 14:55

0 Answers0