as.POSIXct gives an unexpected timezone
Asked Answered
C

4

13

I'm trying to convert a yearmon date (from the zoo package) to a POSIXct in the UTC timezone. This is what I tried to do:

> as.POSIXct(as.yearmon("2010-01-01"), tz="UTC")
[1] "2010-01-01 01:00:00 CET"

I get the same when I convert a Date:

> as.POSIXct(as.Date("2010-01-01"),tz="UTC")
[1] "2010-01-01 01:00:00 CET"

The only way to get it to work is to pass a character as an argument:

> as.POSIXct("2010-01-01", tz="UTC")
[1] "2010-01-01 UTC"

I looked into the documentation of DateTimeClasses, tzset and timezones. My /etc/localtime is set to Europe/Amsterdam. I couldn't find a way to set the tz to UTC, other than setting the TZ environment variable:

> Sys.setenv(TZ="UTC")
> as.POSIXct(as.Date("2010-01-01"),tz="UTC")
[1] "2010-01-01 UTC"

Is it possible to directly set the timezone when creating a POSIXct from a yearmon or Date?

Edit:

I checked the functions as.POSIXct.yearmon. This one passes to the as.POSIXct.Date.

> zoo:::as.POSIXct.yearmon
function (x, tz = "", ...) 
as.POSIXct(as.Date(x), tz = tz, ...)
<environment: namespace:zoo>

So like Joshua says the timezone gets lost in the as.POSIXct.Date. For now I'll use Richies suggestion to set the tzone by hand using:

attr(x, "tzone") <- 'UTC'

This solves the issue of the lost tzone, which is only used for presentation and not internally like Grothendieck and Dwin suggested.

Cumulate answered 1/7, 2011 at 13:29 Comment(0)
S
7

This is because as.POSIXct.Date doesn't pass ... to .POSIXct.

> as.POSIXct.Date
function (x, ...) 
.POSIXct(unclass(x) * 86400)
<environment: namespace:base>
Sufism answered 1/7, 2011 at 13:45 Comment(0)
R
5

You are setting the timezone correctly in your code. The problem you are perceiving is only at the output stage. POSIX values are all referenced to UTC/GMT. Dates are assumed to be midnight times. Midnight UTC is 1 AM CET ( which is apparently where you are).

> as.POSIXct(as.yearmon("2010-01-01"), tz="UTC")
[1] "2009-12-31 19:00:00 EST"     # R reports the time in my locale's timezone
> dtval <- as.POSIXct(as.yearmon("2010-01-01"), tz="UTC")
> format(dtval, tz="UTC")         # report the date in UTC  note it is the correct date ... there
[1] "2010-01-01"
> format(dtval, tz="UTC", format="%Y-%m-%d ")
[1] "2010-01-01 "                 # use a format string
> format(dtval, tz="UTC", format="%Y-%m-%d %OS3")
[1] "2010-01-01 00.000"           # use decimal time

See ?strptime for many, many other format possibilities.

Repro answered 1/7, 2011 at 13:57 Comment(2)
as.POSIXct is ignoring the timezone because as.yearmon doesn't store the time. Try your first example with tz="CET. It will give the same rsult as if you'd specified UTC. See my answer for more explanation.Fornicate
OK. The problem then is that a date value passed to as.POSIXct will always be assumed to be midnight GMT. (And on second reading I see that you were reading the FM for me.) Sorry for being obtuse, again.Repro
F
3

In the help page ?as.POSIXct, for the tz argument it says

A timezone specification to be used for the conversion, if one is required. System-specific (see time zones), but ‘""’ is the current timezone, and ‘"GMT"’ is UTC (Universal Time, Coordinated).

Does as.POSIXct(as.yearmon("2010-01-01"), tz="GMT") work for you?


After more perusal of the documentation, in the details section we see:

Dates without times are treated as being at midnight UTC.

So in your example, the tz argument is ignored. If you use as.POSIXlt it is easier to see what happens with the timezone. The following should all give the same answer, with UTC as the timezone.

unclass(as.POSIXlt(as.yearmon("2010-01-01")))
unclass(as.POSIXlt(as.yearmon("2010-01-01"), tz = "UTC"))
unclass(as.POSIXlt(as.yearmon("2010-01-01"), tz = "GMT"))
unclass(as.POSIXlt(as.yearmon("2010-01-01"), tz = "CET"))

In fact, since you are using as.yearmon (which strips the time out) you will never get to set the timezone. Compare, e.g.,

unclass(as.POSIXlt(as.yearmon("2010-01-01 12:00:00"), tz = "CET"))
unclass(as.POSIXlt("2010-01-01 12:00:00", tz = "CET"))
Fornicate answered 1/7, 2011 at 13:34 Comment(2)
I didn't quite understand what is meant by "if one is required". When I try to convert to GMT I also still get CET [1] "2010-01-01 01:00:00 CET".Cumulate
Actually, the unclass(as.POSIXlt(x)) is maybe a little over the top. attr(x, "tzone") will suffice.Fornicate
B
3

This seems to be an oddity with the date/time "POSIXct" class methods. Try formatting the "Date" or "yearmon" variable first so that as.POSIXct.character rather than as.POSIXct.{Date, yearmon} is dispatched:

Date

> d <- as.Date("2010-01-01")
> as.POSIXct(format(d), tz = "UTC")
[1] "2010-01-01 UTC"

yearmon

> library(zoo)
> y <- as.yearmon("2010-01")
> as.POSIXct(format(y, format = "%Y-%m-01"), tz = "UTC")
[1] "2010-01-01 UTC"
> # or
> as.POSIXct(format(as.Date(y)), tz = "UTC")
[1] "2010-01-01 UTC"
Backbite answered 1/7, 2011 at 14:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.