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

时间:2015-10-26 11:23:37

标签: r timezone posixct

我已阅读this,并说as.POSIXct始终是UTC内部。难怪我得到了

public function __construct($contacts))
{
$this->vnumOfHoles = $numOfHoles;
}

我可以使用> 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,我无法在绘制内容与时间时使用它。我如何用当地时间绘图?

1 个答案:

答案 0 :(得分:1)

试了一下这个有用了:

> 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"