1

我无法让 RPOSIXlt在所需的时区格式化对象。POSIXct按预期工作。这是一个错误还是我错过了什么?

date.str = "2015-12-09 13:30"
from = "Europe/London"
to = "America/Los_Angeles"

lt = as.POSIXlt(date.str, tz=from)
format(lt, tz=to, usetz=TRUE)
#[1] "2015-12-09 13:30:00 GMT"

ct = as.POSIXct(date.str, tz=from)
format(ct, tz=to, usetz=TRUE)
#[1] "2015-12-09 05:30:00 PST"

tzone属性是一样的:

attributes(ct)$tzone
#[1] "Europe/London"
attributes(lt)$tzone
#[1] "Europe/London"

解决方案

正如@nicola 所指出的,format.POSIXlt没有tz参数。POSIXlt要在另一个时区打印日期,可以先使用lubridate包将POSIXlt对象转换为所需的时区:

require(lubridate)
lt.changed = with_tz(lt, tz=to)
format(lt.changed, usetz=TRUE)
#[1] "2015-12-09 05:30:00 PST"
4

0 回答 0