为什么转换时区不能用as.POSIXct..[R]



我看过这个,上面写着as。POSIXct在内部始终是UTC。难怪我得到了

> time1 = as.POSIXct('2015-10-25 10:15:13 UTC')
> time1
[1] "2015-10-25 10:15:13 EDT"
# missing tz causes coercion (not converting!) to computer's tz. 
# (I'm in EDT Boston and calculating some data in Dubai time)

> time1 = as.POSIXct('2015-10-25 10:15:13 UTC', tz = 'UTC', usetz = T)
> time1
[1] "2015-10-25 10:15:13 UTC"
# not work
> as.POSIXct(time1, tz = 'Asia/Dubai', usetz = T)
[1] "2015-10-25 10:15:13 UTC"
# works but the result is character
> format(time1, tz = 'Asia/Dubai', usetz = T)
[1] "2015-10-25 14:15:13 GST"
> class(format(time1, tz = 'Asia/Dubai', usetz = T))
[1] "character"

我可以用format,但它产生character,我不能用它来绘制某物与时间的关系。我如何与当地时间同步?

尝试了一些东西,这是有效的:

> library(lubridate)
> time1 = as.POSIXct('2015-10-25 10:15:13 UTC', tz = 'UTC', usetz = T)
> time1
[1] "2015-10-25 10:15:13 UTC"
> force_tz(time1 + 3600*4, tz = 'Asia/Dubai')
[1] "2015-10-25 14:15:13 GST"
> class(force_tz(time1 + 3600*4, tz = 'Asia/Dubai'))
[1] "POSIXct" "POSIXt" 

也可以与Rbase稍微长一点的语法(感谢@David Arenburg)

> time1 = as.POSIXct('2015-10-25 10:15:13 UTC', tz = 'UTC', usetz = T)
> time1
[1] "2015-10-25 10:15:13 UTC"
> as.POSIXct(format(time1, tz = 'Asia/Dubai', usetz = T), tz = 'Asia/Dubai', usetz = T)
[1] "2015-10-25 14:15:13 GST"

相关内容

  • 没有找到相关文章

最新更新