as.POSIXct gives an unexpected timezone

旧街凉风 提交于 2019-11-30 08:06:35

问题


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.


回答1:


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

> as.POSIXct.Date
function (x, ...) 
.POSIXct(unclass(x) * 86400)
<environment: namespace:base>



回答2:


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.




回答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"))



回答4:


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"


来源:https://stackoverflow.com/questions/6548601/as-posixct-gives-an-unexpected-timezone

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!