The following data frame is from a dput
. I have used forced_tz
on the datatime with no argument. I live in UTC+1 time zone.
library(lubridate)
library(dplyr)
df <- structure(list(value = structure(c(1514967058, 1515148132, 1517472989, 1543844646, 1525085884, 1520584330, 1522838681, 1540379051, 1516707360, 1516705706),
class = c("POSIXct", "POSIXt"))),
.Names = "value",
row.names = c(NA, -10L),
class = c("tbl_df", "tbl", "data.frame"))
tz(df$value)
[1] ""
df2 <- df %>%
mutate(year=year(value))
> tz(df2$year)
[1] "UTC"
I have also used tz= "Europe/Paris"
but when I extact someting from the datetime (day
, month
and so on) they loose their time zone and get UTC again. Is it possible to set the time zone once and then get carried over to all every new datetime components that I create?
The problem is that
year()
seems to return anumeric
, so it's not anymore adate
object.This is the default method for
year()
:So, for example:
Notice that I forced the current
tz
withSys.timezone()
.But:
So when you call
tz(y)
, since it's numeric it doesn't have atz
attribute, and by default it's given"UTC"
.A simple solution is to set yourself the timezone:
So now
tz
works:I'd advise against this but you could also modify the default method of tz():
So now you have a "custom" version of
tz()
which returns the current timezone whenever the input is not a correct date format.