I have a bunch of dates (in UTC) e.g.
> dates <- as.POSIXct(c('2019-01-01', '2019-07-01', '2019-08-01'), tz = 'UTC')
> dates
[1] "2019-01-01 UTC" "2019-07-01 UTC" "2019-08-01 UTC"
I want to know if each date in my vector is during GMT or BST.
So the above would give
"GMT" "BST" "BST"
Might not be the best way of doing it, but got what I wanted using
library(lubridate)
ifelse(hour(force_tz(as.POSIXct(as.Date(dates)), tz = 'Europe/London')) == 1, 'BST', 'GMT')
I have col of number of days since 1970-01-01 00:00:00 UTC that I want to convert to date and time UTC.
I tried this:
z <- Sys.time()
j=floor(unclass(z)/86400) # the number of days since 1970-01-01 (UTC)
as.POSIXct(j, origin = "1970-01-01")
[1] "1970-01-01 05:53:22 CET"
But it is not correct. any idea?
You have to pass to the function as.POSIXct the number of seconds, try this code
j<-17148.5625000000
as.POSIXct(j*24*60*60, origin = "1970-01-01", tz="UTC")
[1] "2016-12-13 13:30:00 UTC"
If you want only the day in output
as.Date(as.POSIXct(j*24*60*60, origin = "1970-01-01", tz="UTC"))
[1] "2016-12-13"
I want to join a list of time zones into one long vector. Append seems to convert the time zones into the system time zone? Why?
> times = list(as.POSIXct("2015-01-01", tz = 'UTC', origin = "1970-01-01"),
+ as.POSIXct("2015-01-02", tz = 'UTC', origin = "1970-01-01"))
> times
[[1]]
[1] "2015-01-01 UTC"
[[2]]
[1] "2015-01-02 UTC"
> do.call(append, times)
[1] "2014-12-31 19:00:00 EST" "2015-01-01 19:00:00 EST"
I can't use unlist as it strips the list of the POSIXct class. What is the alternative?
Okay so here is a subtle "quirk" in the r as.Date function converting from a POSIXct with a timezone, which I am wondering if it is a bug.
> as.POSIXct("2013-03-29", tz = "Europe/London")
[1] "2013-03-29 GMT"
> as.Date(as.POSIXct("2013-03-29", tz = "Europe/London"))
[1] "2013-03-29"
So far no problem, but.....
> as.POSIXct("2013-04-01", tz = "Europe/London")
[1] "2013-04-01 BST"
> as.Date(as.POSIXct("2013-04-01", tz = "Europe/London"))
[1] "2013-03-31"
Anybody seen this? Is this a bug or another quirk? April fools?
The default time zone for as.Date.POSIXct is "UTC" (see the help page). Try as.Date(as.POSIXct("2013-04-01", tz = "Europe/London"),tz = "Europe/London").
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.
This is because as.POSIXct.Date doesn't pass ... to .POSIXct.
> as.POSIXct.Date
function (x, ...)
.POSIXct(unclass(x) * 86400)
<environment: namespace:base>
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.
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"))
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"