R as.Date() and time zones

Here’s a fun quirk to watch out for when playing around in R with POSIX time values and converting them to dates.

I’ll start by creating a time variable, in the POSIXct class, and set it to use my current time zone, which is Pacific Daylight Time (PST8PDT).

mydatetime = as.POSIXct('2019-05-29 23:59', tz = 'PST8PDT')

If I then wanted to turn this time into a simple Date, dropping the time information, I would (naively) do the following:

as.Date(mydatetime)

and I’d get back the following result:

"2019-05-30"

which is a day later than my original time value, which was May 29 in the Pacific Daylight Time zone. That’s because R has helpfully decided that the POSIXct time value I supplied to as.Date() must obviously be the UTC time zone, which is how all POSIXct values are stored internally (with a time zone offset applied as needed).

To get the intended result, I need to tell as.Date() to use the correct time zone. I could either specify PST8PDT explicitly, or in this case, since my computer system’s time is set to PST8PDT, I can just use the tz=” argument, which means “use the current system time zone”.

as.Date(mydatetime, tz = '')
"2019-05-29"

In that case, I get the intended date.

The as.Date() function’s help page spells all of this out:

The as.Date methods accept character strings, factors, logical NA and objects of classes “POSIXlt” and “POSIXct”. (The last is converted to days by ignoring the time after midnight in the representation of the time in specified time zone, default UTC.)

but that would require actually reading the help file all the way through.