简体   繁体   English

R将年月日时当地标准时间转换为UTC

[英]R convert year-month-day-hour local standard time to UTC

Background info (see question at bottom): I received a dataset of hourly averaged observations collected by instruments at hundreds of sites in different time zones every hour for the past 10 years. 背景信息(请参阅底部的问题):在过去10年中,我收到了每小时在不同时区的数百个站点中由仪器收集的每小时平均观测值的数据集。 The instruments are never adjusted for daylight savings time, so all times in the dataset are in local standard time. 仪器永远不会针对夏令时进行调整,因此数据集中的所有时间均为当地标准时间。 The hourly reported values are averages of measurements made every minute for the previous hour. 每小时报告的值是前一小时每分钟进行一次测量的平均值。 Year, month, day, and hour are reported in separate columns. 年,月,日和小时在单独的列中报告。 The hours go from 1:24 instead of 0:23. 时间从1:24而不是0:23开始。 I want to create a new column containing the UTC datetime. 我想创建一个包含UTC日期时间的新列。

The data table below is a sample dataset with my most recent solution, as far as it goes. 到目前为止,下面的数据表是我最新解决方案的样本数据集。 For many frustrating hours for two weeks, I have experimented with strptime, chron, POXITcl, and POXITlt, and scoured stackoverflow and other sources to try and understand what the solution would be. 在两个星期的令人沮丧的几个小时中,我尝试了strptime,chron,POXITcl和POXITlt,并搜寻了stackoverflow和其他资源来尝试了解解决方案。 I'm never sure of what's going on in my attempts at conversion (except when I'm sure it's wrong, which is most of the time!). 我永远不确定转换尝试的过程(除非我确定这是错误的,这通常是大多数时间!)。

I'm not sure that the datetime column I've created is the correct intermediate step I should be using, either, or how to get from that to UTC time that R will handle correctly. 我不确定我创建的datetime列是我应该使用的正确中间步骤,还是不确定如何将R正确处理到该时间。 I inserted the character "T" between data and time in my datetime column to force the column to remain as character, otherwise unexpected things happen. 我在datetime列中的数据和时间之间插入了字符“ T”,以强制将该列保留为字符,否则发生意外情况。 For example, my computer operating system timezone is America/Toronto, and 例如,我的计算机操作系统时区为America / Toronto,并且

as.POSIXct(mydata$datetime, format="%Y-%m-%dT%H:%M %z")

converts 2013-01-01T01:00-0800 to 2013-01-01 04:00:00 . 2013-01-01T01:00-0800转换为2013-01-01 04:00:00 The above command is seems to be converting to my machine's timezone, not UTC. 上面的命令似乎正在转换为我机器的时区,而不是UTC。 So, if I change the R environment time zone, without changing the computer operating system time zone, before running the command 因此,如果在运行命令之前更改了R环境时区而不更改计算机操作系统时区,

Sys.setenv(TZ = "GMT")
mydata$dateUTC <- as.POSIXct(mydata $datetime, format="%Y-%m-%dT%H:%M %z") 
Sys.unsetenv("TZ")

then the above command converts 2013-01-01T01:00-0800 to 2013-01-01 09:00:00 which appears to be the UTC time I'm looking for. 然后上述命令将2013-01-01T01:00-0800转换为2013-01-01 09:00:00 ,这似乎是我要查找的UTC时间。

I'm not too worried about hour24, because it seems that whatever method is used, the date is automatically increased to the next day and the hour changed to 00:00 (eg, 2013-01-01 24:00 becomes 2013-01-02 00:00). 我不太担心hour24,因为似乎无论使用什么方法,日期都会自动增加到第二天,并且小时更改为00:00(例如,2013-01-01 24:00变为2013-01 -02 00:00)。

When converting from UTC to local time, I'm not too worried about the fact that the date on which times change from Standard time to Daylight Savings time can, and has changed over the years. 从UTC转换为当地时间时,我不太担心以下事实:时间从标准时间更改为夏令时可以更改,并且多年来已经更改。 Given the correct UTC time and Olson timezone, if I use the IANA timezone database this should be automatically taken care of (I think). 给定正确的UTC时间和Olson时区,如果我使用IANA时区数据库,则应该自动进行处理(我认为)。

Question 1: Using R, how should I convert year-month-day-hour reported in local standard time all year to UTC time? 问题1:如何使用R将全年以当地标准时间报告的年-月-日-小时转换为UTC时间?

Question 2: Using R, How should I convert from UTC time to local standard time (without converting to DST in localities that use DST for civil time)? 问题2:使用R,我应该如何从UTC时间转换为本地标准时间(在不使用DST作为民用时间的地区中转换为DST)?

Question 3: Using R, how should I convert from UTC time to local time, taking into account daylight saving time? 问题3:使用R,考虑到夏令时,我应该如何从UTC时间转换为本地时间?

Question 4: In order to convert from UTC to local time, I will need the timezone names from the IANA database. 问题4:为了从UTC转换为本地时间,我需要IANA数据库中的时区名称。 Is there some way I can pull this in from somewhere on the web, given the latitude and longitude for each site? 给定每个站点的经度和纬度,是否可以通过某种方式将其从网络上的某个地方提取出来?

filename = mydata
    site    year  month day hourend UTCoffset      datetime         obs
    2001    2015    1   1   22:00   -0200   2013-01-01T22:00-0200   1356
    2001    2015    1   1   23:00   -0200   2013-01-01T23:00-0300   1593
    2001    2015    1   1   24:00   -0200   2013-01-01T24:00-0200   946
    2001    2015    1   2   01:00   -0200   2013-01-02T01:00-0200   271
    2001    2015    1   2   02:00   -0200   2013-01-02T02:00-0200   665
    3001    2015    1   1   22:00   -0350   2013-01-01T22:00-0350   548
    3001    2015    1   1   23:00   -0350   2013-01-01T23:00-0350   936
    3001    2015    1   1   24:00   -0350   2013-01-01T24:00-0350   1938
    3001    2015    1   2   01:00   -0350   2013-01-02T01:00-0350   952
    3001    2015    1   2   02:00   -0350   2013-01-02T02:00-0350   1584
    4001    2015    1   1   22:00   -0400   2013-01-01T22:00-0400   1837
    4001    2015    1   1   23:00   -0400   2013-01-01T23:00-0400   1275
    4001    2015    1   1   24:00   -0400   2013-01-01T24:00-0400   382
    4001    2015    1   2   01:00   -0400   2013-01-02T01:00-0400   837
    4001    2015    1   2   02:00   -0400   2013-01-02T02:00-0400   592
    5001    2015    1   1   22:00   -0500   2013-01-01T22:00-0500   392
    5001    2015    1   1   23:00   -0500   2013-01-01T23:00-0500   15
    5001    2015    1   1   24:00   -0500   2013-01-01T24:00-0500   403
    5001    2015    1   2   01:00   -0500   2013-01-02T01:00-0500   993
    5001    2015    1   2   02:00   -0500   2013-01-02T02:00-0500   1287
    6001    2015    1   1   22:00   -0600   2013-01-01T22:00-0600   738
    6001    2015    1   1   23:00   -0600   2013-01-01T23:00-0600   992
    6001    2015    1   1   24:00   -0600   2013-01-01T24:00-0600   1392
    6001    2015    1   2   01:00   -0600   2013-01-02T01:00-0600   189
    6001    2015    1   2   02:00   -0600   2013-01-02T02:00-0600   1282
    7001    2015    1   1   22:00   -0700   2013-01-01T22:00-0700   839
    7001    2015    1   1   23:00   -0700   2013-01-01T23:00-0700   742
    7001    2015    1   1   24:00   -0700   2013-01-01T24:00-0700   942
    7001    2015    1   2   01:00   -0700   2013-01-02T01:00-0700   882
    7001    2015    1   2   02:00   -0700   2013-01-02T02:00-0700   993
    8001    2015    1   1   22:00   -0800   2013-01-01T22:00-0800   1140
    8001    2015    1   1   23:00   -0800   2013-01-01T23:00-0800   1532
    8001    2015    1   1   24:00   -0800   2013-01-01T24:00-0800   1834
    8001    2015    1   2   01:00   -0800   2013-01-02T01:00-0800   1732
    8001    2015    1   2   02:00   -0800   2013-01-02T02:00-0800   954

您可以在R中检出“ Lubridate”软件包。那里的strptime函数对您的情况很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM