4

When I try to coerce a POSIXct date-time to a Date using as.Date, it seems to return wrong date.

I suspect it has got something to do with the time zone. I tried the tz argument in as.Date, but it didn't give the expected date.

# POSIXct returns day of month 24  
data$Time[3]
# [1] "2020-03-24 00:02:00 IST"

class(data$Time[3])
# [1] "POSIXct" "POSIXt"

# coerce to Date, returns 23 
as.Date(data$Time[3])
# [1] "2020-03-23"

# try the time zone argument, without luck
as.Date(data$Time[3], tz = "IST")
# [1] "2020-03-23"
# Warning message:
# In as.POSIXlt.POSIXct(x, tz = tz) : unknown timezone 'IST' 

Sys.timezone()
# [1] "Asia/Calcutta"

Any ideas what may be going wrong here?

Henrik
  • 65,555
  • 14
  • 143
  • 159
curious_cat
  • 805
  • 2
  • 9
  • 24

2 Answers2

5

Using the setup in the Note at the end we can use any of these:

# same date as print(x) shows
as.Date(as.character(x))
## [1] "2020-03-24"

# use the time zone stored in x (or system time zone if that is "")
as.Date(x, tz = attr(x, "tzone"))
## [1] "2020-03-24"

# use system time zone
as.Date(x, tz = "")
## [1] "2020-03-24"

# use system time zone
as.Date(x, tz = Sys.timezone())
## [1] "2020-03-24"

# use indicated time zone
as.Date(x, tz = "Asia/Calcutta")
## [1] "2020-03-24"

Note

We have assumed this setup.

Sys.setenv(TZ = "Asia/Calcutta")
x <- structure(1584988320, class = c("POSIXct", "POSIXt"), tzone = "")

R.version.string
## [1] "R version 4.0.2 Patched (2020-06-24 r78745)"
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
  • Thanks! The output is `structure(1584988320, class = c("POSIXct", "POSIXt"), tzone = "")` and yes the idiom that you posted seems to work: `as.Date(data$Time[3], tz = "Asia/Calcutta")` – curious_cat Jul 05 '20 at 17:07
  • This works well too! `as.Date(x, tz = attr(x, "tzone"))` Many thanks! – curious_cat Jul 05 '20 at 17:08
  • `Sys.timezone()` gave `[1] "Asia/Calcutta"` Edited the question accordingly. – curious_cat Jul 05 '20 at 17:16
  • The first idiom `as.Date(x, tz = check_tzones(x))` seems to be failing. `check_tzones(data$Time[3])` gives `NULL` – curious_cat Jul 05 '20 at 17:20
  • Yes, I couldn't get that one to work either. I have revised the answer to only include the ones that worked and to add a Note showing how to reproduce the input. – G. Grothendieck Jul 05 '20 at 17:23
  • @G.Grothendieck Thanks for a nice answer. Would you mind adding relevant alternatives to this post: [Date conversion from POSIXct to Date in R](https://stackoverflow.com/questions/16557028/date-conversion-from-posixct-to-date-in-r), which I think is a canonical post for a closely related topic. Cheers. – Henrik Jul 05 '20 at 18:06
4

The clue is in the warning message. as.Date() doesn't know how to interpret IST as a timezone and so defaults to UTC. Assuming that IST is Indian Standard Time (rather than Irish Standard time) and that IST is UTC+5:30, as.Date() is giving the expected result, even if it is incorrect for your purposes.

Providing a date with a timezone expressed as an offset from UTC gives the desired result.

as.Date("2020-03-24 00:02:00 UTC+5:30")
[1] "2020-03-24"
Limey
  • 10,234
  • 2
  • 12
  • 32
  • Thank you! It is indeed Indian and not Irish! :) I assumed "IST" would do since the first posixCT output itself had an IST in there! Apparently not! Is there a list of which time zone abbreviations are acceptable? I trawled through the help docs but could not find one. – curious_cat Jul 05 '20 at 17:00
  • Now the problem is I already have a data frame `data` with those date values like `2020-03-24 00:02:00 IST` stored in `data$time` Do I have to string manipulate those to get the UTC+5:30 in there? Or....? Is the mistake at the spot where I created the data frame using `data$Time<-as.POSIXct(data$Time,format="%m-%d-%y %I:%M %p")` Should I be doing this differently so that the timezone gets encoded right there? – curious_cat Jul 05 '20 at 17:02