在 R 中格式化日期和时区

formatting date and timezone in R

在我的 Rmarkdown 文档中,我想在 header 中显示日期 时区,同时省略实际时间。

目前,当前代码显示日期但忽略了时区:

date: "`r format(Sys.Date(), '%d %B, %Y')`" 

> format(Sys.Date(), '%d %B, %Y')
[1] "19 March, 2018"

> Sys.time()
[1] "2018-03-19 22:22:11 EDT"

但是,如果我在 Sys.time() 上使用 strsplit(),则时区被省略:

   > strsplit(as.character(Sys.time()), " ")
[[1]]
[1] "2018-03-19" "22:25:05" 

同样,此解决方案在控制台中有效,但在 Rmarkdown 中抛出错误:

paste(format(Sys.Date(), '%d %B, %Y'), tz(Sys.Date()))

Warning: Error in tz: could not find function "tz"

在 Rmarkdown 中格式化 Sys.time()Sys.date() 以使显示仅显示为这样的正确方法是什么:

"2018-03-19 EDT"

使用 format 和时区规范(%Z 或 %z)

format(Sys.time(), "%Y-%m-%d %Z")