Mike Wise
Mike Wise

Reputation: 22807

date_format timezone strangeness

I defined a date with a timezone, but when I print it out using the scales package date_format it gives me the time in UTC, which is awkward for my purpose.

> library(scales)
> st <- as.POSIXct("2015-10-31 00:00:00",tz="US/Pacific")
> st
[1] "2015-10-31 PDT"
> fmt <- date_format("%Y-%m-%d %R %Z")
> fmt(st)
[1] "2015-10-31 07:00 UTC"

Interestingly this works (so POSIXct seems to understand the timezone) - but does not give me enough control over the format:

> format(st,usetz=T)
[1] "2015-10-31 PDT"

This unreliability is hinted at in the help for ?date_format:

When %z or %Z is used for output with an object with an assigned time zone an attempt is made to use the values for that time zone — but it is not guaranteed to succeed.

So my question is, how do I make it succeed?

Suggesting workarounds is fine and may attact upvotes, but please understand the point of this question is that I want to obtain insight as to what is going on with date_format.

Upvotes: 2

Views: 711

Answers (1)

Roland
Roland

Reputation: 132576

The definition of date_format is very short:

function (format = "%Y-%m-%d", tz = "UTC") 
{
    function(x) format(x, format, tz = tz)
}

It should be obvious why the timezone is changed if you don't change the default.

Upvotes: 3

Related Questions