Dear R users I am puzzled by the following result from R script. I am trying to convert local time to UTC time. Time zone is -5, therefore I used the following approach. Below is the script.> Corrected_SA_data$date_time[k-1][1] "2007-03-11 01:00:00"> Corrected_SA_data$TZ[k-1][1] -5> Corrected_SA_data$date_time[k-1]-Corrected_SA_data$TZ[k-1]*3600[1] "2007-03-11 07:00:00 MDT" I was expecting this last value to be something like "2007-03-11 06:00:00 UTC" Please correct me if I ma wrong. On the other hand I have> Corrected_SA_data$date_time[k][1] "2007-03-11 02:00:00"> Corrected_SA_data$TZ[k][1] -5> Corrected_SA_data$date_time[k]-Corrected_SA_data$TZ[k]*3600[1] NA I am not sure why I am getting NA. Thank you for your help. Alemu [[alternative HTML version deleted]]
I would use the 'lubridate' package for this: > z <- Sys.time() > z [1] "2014-12-07 15:43:50 EST" > require(lubridate) > with_tz(z, "UTC") [1] "2014-12-07 20:43:50 UTC" Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it. On Sun, Dec 7, 2014 at 2:25 PM, Alemu Tadesse <alemu.tadesse at gmail.com> wrote:> Dear R users > > I am puzzled by the following result from R script. I am trying to convert > local time to UTC time. Time zone is -5, therefore I used the following > approach. > > Below is the script. > > Corrected_SA_data$date_time[k-1] > [1] "2007-03-11 01:00:00" > > Corrected_SA_data$TZ[k-1] > [1] -5 > > Corrected_SA_data$date_time[k-1]-Corrected_SA_data$TZ[k-1]*3600 > [1] "2007-03-11 07:00:00 MDT" > > I was expecting this last value to be something like "2007-03-11 06:00:00 > UTC" > > Please correct me if I ma wrong. > > On the other hand I have > > Corrected_SA_data$date_time[k] > [1] "2007-03-11 02:00:00" > > Corrected_SA_data$TZ[k] > [1] -5 > > Corrected_SA_data$date_time[k]-Corrected_SA_data$TZ[k]*3600 > [1] NA > > I am not sure why I am getting NA. > > Thank you for your help. > > Alemu > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
You have not provided a reproducible example, so anything I say could be wrong just due to misinterpretation. Please read [1] for suggestions on making your examples reproducible, particularly regarding the use of dput to provide example data. You have also posted in HTML format, which can cause additional scrambling of communications on this list.>From the notation you are using, I would guess that "Corrected_SA_data" isa data frame containing columns "date_time" and "TZ" at a minimum. The "date_time" column could be a vector of class POSIXlt or POSIXct... except that I cannot reproduce such an object that doesn't print some kind of timezone indicator in its output. What version of R are you using? Note that such an object contains a tz attribute already, so unless you have already made sure that the date_time column knows about that timezone, they are probably unrelated to each other. Note that any POSIXct object is internally represented as a specific instant of time. The tz attribute is only used to control how that time will be displayed. For example: testtime3 <- as.POSIXct( rawtime1, tz="America/New_York" ) format( testtime3, tz="Etc/GMT", usetz=TRUE ) If you want the output to omit the timezone information you can omit the usetz argument: format( testtime3, tz="Etc/GMT" ) If you have not yet, you should read [2]. Note that three-letter timezone indicators (even though they are given in OUTPUT!) are at best unreliable for use in specifying timezones in R... read ?timezones. [1] http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example [2] http://www.r-project.org/doc/Rnews/Rnews_2004-1.pdf On Sun, 7 Dec 2014, Alemu Tadesse wrote:> Dear R users > > I am puzzled by the following result from R script. I am trying to convert > local time to UTC time. Time zone is -5, therefore I used the following > approach. > > Below is the script. >> Corrected_SA_data$date_time[k-1] > [1] "2007-03-11 01:00:00" >> Corrected_SA_data$TZ[k-1] > [1] -5 >> Corrected_SA_data$date_time[k-1]-Corrected_SA_data$TZ[k-1]*3600 > [1] "2007-03-11 07:00:00 MDT" > > I was expecting this last value to be something like "2007-03-11 06:00:00 > UTC" > > Please correct me if I ma wrong. > > On the other hand I have >> Corrected_SA_data$date_time[k] > [1] "2007-03-11 02:00:00" >> Corrected_SA_data$TZ[k] > [1] -5 >> Corrected_SA_data$date_time[k]-Corrected_SA_data$TZ[k]*3600 > [1] NA > > I am not sure why I am getting NA. > > Thank you for your help. > > Alemu > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >--------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k
Thank you very much Jeff. Below is the data I used:> Corrected_dataSA_LST SA_GHI_mean 61759 3/11/2007 1:00 0.00000 67517 3/11/2007 2:00 0.00000 70017 3/11/2007 3:00 0.00000 70524 3/11/2007 4:00 0.00000 71061 3/11/2007 5:00 0.00000 71638 3/11/2007 6:00 0.00000 72113 3/11/2007 7:00 20.20873 72629 3/11/2007 8:00 123.14231 73274 3/11/2007 9:00 306.97343>test1<-as.POSIXct(Corrected_data$SA_LST,format="%m/%d/%Y %H:%M",tz="EST5EDT")>test1[1] "2007-03-11 01:00:00 EST" NA "2007-03-11 03:00:00 EDT" "2007-03-11 04:00:00 EDT" [5] "2007-03-11 05:00:00 EDT" "2007-03-11 06:00:00 EDT" "2007-03-11 07:00:00 EDT" "2007-03-11 08:00:00 EDT" [9] "2007-03-11 09:00:00 EDT"> format(test1, tz="Etc/GMT", usetz=TRUE )[1] "2007-03-11 06:00:00 GMT" NA "2007-03-11 07:00:00 GMT" "2007-03-11 08:00:00 GMT" [5] "2007-03-11 09:00:00 GMT" "2007-03-11 10:00:00 GMT" "2007-03-11 11:00:00 GMT" "2007-03-11 12:00:00 GMT" [9] "2007-03-11 13:00:00 GMT">As you can see the "2007-03-11 02:00:00 EST" is not recognized and returned as NA. This may be due to DST, which I do not know how to handle in R. Best, Alemu On Sun, Dec 7, 2014 at 1:59 PM, Jeff Newmiller <jdnewmil at dcn.davis.ca.us> wrote:> You have not provided a reproducible example, so anything I say could be > wrong just due to misinterpretation. Please read [1] for suggestions on > making your examples reproducible, particularly regarding the use of dput > to provide example data. You have also posted in HTML format, which can > cause additional scrambling of communications on this list. > > From the notation you are using, I would guess that "Corrected_SA_data" is > a data frame containing columns "date_time" and "TZ" at a minimum. The > "date_time" column could be a vector of class POSIXlt or POSIXct... except > that I cannot reproduce such an object that doesn't print some kind of > timezone indicator in its output. What version of R are you using? Note > that such an object contains a tz attribute already, so unless you have > already made sure that the date_time column knows about that timezone, they > are probably unrelated to each other. > > Note that any POSIXct object is internally represented as a specific > instant of time. The tz attribute is only used to control how that time > will be displayed. For example: > > testtime3 <- as.POSIXct( rawtime1, tz="America/New_York" ) > format( testtime3, tz="Etc/GMT", usetz=TRUE ) > > If you want the output to omit the timezone information you can omit the > usetz argument: > > format( testtime3, tz="Etc/GMT" ) > > If you have not yet, you should read [2]. Note that three-letter timezone > indicators (even though they are given in OUTPUT!) are at best unreliable > for use in specifying timezones in R... read ?timezones. > > [1] http://stackoverflow.com/questions/5963269/how-to-make- > a-great-r-reproducible-example > [2] http://www.r-project.org/doc/Rnews/Rnews_2004-1.pdf > > > On Sun, 7 Dec 2014, Alemu Tadesse wrote: > > Dear R users >> >> I am puzzled by the following result from R script. I am trying to convert >> local time to UTC time. Time zone is -5, therefore I used the following >> approach. >> >> Below is the script. >> >>> Corrected_SA_data$date_time[k-1] >>> >> [1] "2007-03-11 01:00:00" >> >>> Corrected_SA_data$TZ[k-1] >>> >> [1] -5 >> >>> Corrected_SA_data$date_time[k-1]-Corrected_SA_data$TZ[k-1]*3600 >>> >> [1] "2007-03-11 07:00:00 MDT" >> >> I was expecting this last value to be something like "2007-03-11 06:00:00 >> UTC" >> >> Please correct me if I ma wrong. >> >> On the other hand I have >> >>> Corrected_SA_data$date_time[k] >>> >> [1] "2007-03-11 02:00:00" >> >>> Corrected_SA_data$TZ[k] >>> >> [1] -5 >> >>> Corrected_SA_data$date_time[k]-Corrected_SA_data$TZ[k]*3600 >>> >> [1] NA >> >> I am not sure why I am getting NA. >> >> Thank you for your help. >> >> Alemu >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide http://www.R-project.org/ >> posting-guide.html >> and provide commented, minimal, self-contained, reproducible code. >> >> > ------------------------------------------------------------ > --------------- > Jeff Newmiller The ..... ..... Go Live... > DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live > Go... > Live: OO#.. Dead: OO#.. Playing > Research Engineer (Solar/Batteries O.O#. #.O#. with > /Software/Embedded Controllers) .OO#. .OO#. rocks...1k > ------------------------------------------------------------ > --------------- >[[alternative HTML version deleted]]