将UTC时间转换为R中的本地标准时间 [英] Converting UTC time to local standard time in R

查看:913
本文介绍了将UTC时间转换为R中的本地标准时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将UTC时间转换为本地标准时间。我发现许多功能转换为本地夏令时,但我没有成功获得标准时间。现在,我有以下代码转换为在特定时区的本地日光时间:

I am trying to convert UTC time to local standard time. I have found many functions which convert to Local Daylight Time, but I was not successful in getting the standard time. Right now, I have the following code which converts to local daylight time at my specific timezone:

pb.date <- as.POSIXct(date,tz="UTC")
format(pb.date, tz="timeZone",usetz=TRUE)

我会感谢任何帮助。

推荐答案

首先,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".)

这篇关于将UTC时间转换为R中的本地标准时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆