当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


R lubridate tz 获取/设置日期时间的时区部分

方便地获取和设置日期时间的时区。

tz<-force_tz() 的别名,它保留本地时间,创建不同的时间点。如果您想保持瞬间相同,但更改打印表示,请使用with_tz()

用法

tz(x)

tz(x) <- value

参数

x

日期时间向量,通常属于 POSIXctPOSIXlt 类。

value

时区的新值。

长度为 1 的字符向量。空字符串 ( "" ) 表示您当前的时区。

为了向后兼容,日期、 NA 或字符向量的时区为 "UTC"

有效时区

时区存储在系统特定的数据库中,因此不能保证每个系统上的时区都相同(但是,它们通常非常相似,除非您的系统非常过时)。您可以使用 OlsonNames() 查看完整列表。

也可以看看

有关底层 tzone 属性的说明,请参阅DateTimeClasses

例子

x <- y <- ymd_hms("2012-03-26 10:10:00", tz = "UTC")
tz(x)
#> [1] "UTC"

# Note that setting tz() preserved the clock time, which implies
# that the actual instant in time is changing
tz(y) <- "Pacific/Auckland"
y
#> [1] "2012-03-26 10:10:00 NZDT"
x - y
#> Time difference of 13 hours

# This is the same as force_tz()
force_tz(x, "Pacific/Auckland")
#> [1] "2012-03-26 10:10:00 NZDT"

# Use with_tz() if you want to change the time zone, leave
# the instant in time the same
with_tz(x, "Pacific/Auckland")
#> [1] "2012-03-26 23:10:00 NZDT"
源代码:R/accessors-tz.r

相关用法


注:本文由纯净天空筛选整理自Hadley Wickham等大神的英文原创作品 Get/set time zone component of a date-time。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。