简体   繁体   中英

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. 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. I want to create a new column containing the UTC datetime.

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. 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. 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. For example, my computer operating system timezone is America/Toronto, and

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

converts 2013-01-01T01:00-0800 to 2013-01-01 04:00:00 . The above command is seems to be converting to my machine's timezone, not UTC. So, if I change the R environment time zone, without changing the computer operating system time zone, before running the command

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.

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).

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. Given the correct UTC time and Olson timezone, if I use the IANA timezone database this should be automatically taken care of (I think).

Question 1: Using R, how should I convert year-month-day-hour reported in local standard time all year to UTC time?

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)?

Question 3: Using R, how should I convert from UTC time to local time, taking into account daylight saving time?

Question 4: In order to convert from UTC to local time, I will need the timezone names from the IANA database. 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函数对您的情况很有用。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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