且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将UTC时间转换为R中的本地标准时间

更新时间:2023-02-06 16:52:36

首先,POSIXct日期时间总是UCT在内部 print.POSIXt format.POSIXt 方法将适当地使TZ从内部表示的输出移位: p>

First, POSIXct date-times are always UCT internally. The print.POSIXt and format.POSIXt methods will appropriately make the TZ shift on output from their internal representations:

pb.date <- as.POSIXct(Sys.Date())
Sys.Date()
#[1] "2015-07-09"

所以这是当前日期的午夜格林威治:

So that was midnight of the current date in Greenwich:

format(pb.date, tz="America/Los_Angeles",usetz=TRUE)
#[1] "2015-07-08 17:00:00 PDT"

当在格林威治的午夜,这是美国左岸前一天的5点钟夏令时。您需要为您的TZ(和您的操作系统)使用正确的字符值,而这两个值都是未指定的。

When it's midnight in Greenwich, it's 5PM Daylight Time in the previous day on the Left Coast of the US. You need to use the correct character values for your TZ (and your OS) both of which at the moment are unspecified.

美国太平洋时区距格林威治标准时间晚8小时您可以使用标准/日光不相关的时区:

The US Pacific timezone is 8 hours behind GMT so you can use a timezone that is Standard/Daylight-agnostic:

> format(pb.date,usetz=TRUE, tz="Etc/GMT+8")
[1] "2015-07-08 16:00:00 GMT+8"

(注意+与后面反转, - 带前进)。

(Notice the reversal of + with "behind" and - with "ahead".)