以下数据框来自qazxsw poi。我在日期时间使用了qazxsw poi而没有参数。我住在UTC + 1时区。
dput
我也使用过forced_tz
但是当我从日期时间(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"
,tz= "Europe/Paris"
等)中取出时,他们会松开他们的时区并再次获得UTC。是否可以设置一次时区,然后转移到我创建的所有每个新的日期时间组件?
问题是day
似乎返回month
,所以它不再是year()
对象。
这是numeric
的默认方法:
date
所以,例如:
year()
请注意,我用year.default <- function(x)
as.POSIXlt(x, tz = tz(x))$year + 1900
强制当前的y <- as.POSIXlt("2018-01-03 09:10:58 CET", tz = Sys.timezone())$year + 1900
#y
#[1] 2018
。
但:
tz
所以当你调用Sys.timezone()
时,因为它是数字,所以它没有class(y)
#[1] "numeric"
属性,默认情况下它给出了tz(y)
。
tz
一个简单的解决方案是为自己设置时区:
"UTC"
所以现在# example:
# tz(123)
# [1] "UTC"
工作:
attr(y, "tzone") <- Sys.timezone()
y
#[1] 2018
#attr(,"tzone")
#[1] "Europe/Berlin"
我建议反对这个,但你也可以修改tz
的默认方法:
tz(y)
[1] "Europe/Berlin"
所以现在你有一个tz()的“自定义”版本,只要输入不是正确的日期格式,它就会返回当前时区。