Greetings - I'm battling POSIXct, as per the code below. My input is actually an XL file, but the weird results below correctly model what I am seeing in my program. Before I punt and use lubridate or timeDate, could anyone please help me understand why POSIXct forces my variable back to GMT? I suspect that I'm not properly coding the tzone value, but it does not throw an error as-is.> tstamp <- "2011-05-22 11:45:00 MDT"> mode(tstamp)[1] "character">> dateP <- as.POSIXct(tstamp, origin="1970-01-01", tzone="MDT")> mode(dateP)[1] "numeric"> dateP[1] "2011-05-22 11:45:00 MDT">> dateN <- as.numeric(dateP)> dateN[1] 1306086300>> dateP2 <- as.POSIXct(dateN, origin="1970-01-01", tzone="MDT")> dateP2[1] "2011-05-22 18:45:00 MDT" Many thanks. Galen Moore [[alternative HTML version deleted]]
On May 30, 2011, at 10:20 PM, Galen Moore wrote:> Greetings - > > > > I'm battling POSIXct, as per the code below. My input is actually > an XL > file, but the weird results below correctly model what I am seeing > in my > program. > > > > Before I punt and use lubridate or timeDate, could anyone please > help me > understand why POSIXct forces my variable back to GMT? > > > > I suspect that I'm not properly coding the tzone value, but it does > not > throw an error as-is. > > > > > >> tstamp <- "2011-05-22 11:45:00 MDT" > >> mode(tstamp) > > [1] "character" > >> > >> dateP <- as.POSIXct(tstamp, origin="1970-01-01", tzone="MDT") > >> mode(dateP) > > [1] "numeric" > >> dateP > > [1] "2011-05-22 11:45:00 MDT" > >> > >> dateN <- as.numeric(dateP) > >> dateN > > [1] 1306086300 >So now the internal representation is referenced to GMT>> > >> dateP2 <- as.POSIXct(dateN, origin="1970-01-01", tzone="MDT") > >> dateP2And you are telling the function to add as many hours offset as needed for the difference between GMT and MD T..... -- David.> > [1] "2011-05-22 18:45:00 MDT" >David Winsemius, MD West Hartford, CT
Perhaps because the timezone is specified as a character string and not a date-time object complete with timezone.>From the help filr for as.POSIXct.numeric:"origin: a date-time object, or something which can be coerced by as.POSIXct(tz="GMT") to such an object." Note the coercion. Bill Venables. -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Galen Moore Sent: Tuesday, 31 May 2011 12:20 PM To: r-help at r-project.org Subject: [R] DateTime Math in R - POSIXct Greetings - I'm battling POSIXct, as per the code below. My input is actually an XL file, but the weird results below correctly model what I am seeing in my program. Before I punt and use lubridate or timeDate, could anyone please help me understand why POSIXct forces my variable back to GMT? I suspect that I'm not properly coding the tzone value, but it does not throw an error as-is.> tstamp <- "2011-05-22 11:45:00 MDT"> mode(tstamp)[1] "character">> dateP <- as.POSIXct(tstamp, origin="1970-01-01", tzone="MDT")> mode(dateP)[1] "numeric"> dateP[1] "2011-05-22 11:45:00 MDT">> dateN <- as.numeric(dateP)> dateN[1] 1306086300>> dateP2 <- as.POSIXct(dateN, origin="1970-01-01", tzone="MDT")> dateP2[1] "2011-05-22 18:45:00 MDT" Many thanks. Galen Moore [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list 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.
Thank you, David. I am not subtracting the seconds to convert POSIXct's GMT to MDT, and don't understand why I should need to. Any hints, however, as to why dateP <- as.POSIXct(tstamp, origin="1970-01-01", tzone="MDT") returns a date in the correct tz in my first instance below, yet dateP2 <- as.POSIXct(dateN, origin="1970-01-01", tzone="MDT") decides to revert to GMT in my second instance below? Thanks, Galen -----Original Message----- From: David Winsemius [mailto:dwinsemius at comcast.net] Sent: Monday, May 30, 2011 20:31 To: galen.a.moore at gmail.com Cc: r-help at r-project.org Subject: Re: [R] DateTime Math in R - POSIXct On May 30, 2011, at 10:20 PM, Galen Moore wrote:> Greetings - > > > > I'm battling POSIXct, as per the code below. My input is actually an > XL file, but the weird results below correctly model what I am seeing > in my program. > > > > Before I punt and use lubridate or timeDate, could anyone please help > me understand why POSIXct forces my variable back to GMT? > > > > I suspect that I'm not properly coding the tzone value, but it does > not throw an error as-is. > > > > > >> tstamp <- "2011-05-22 11:45:00 MDT" > >> mode(tstamp) > > [1] "character" > >> > >> dateP <- as.POSIXct(tstamp, origin="1970-01-01", tzone="MDT") > >> mode(dateP) > > [1] "numeric" > >> dateP > > [1] "2011-05-22 11:45:00 MDT" > >> > >> dateN <- as.numeric(dateP) > >> dateN > > [1] 1306086300 >So now the internal representation is referenced to GMT>> > >> dateP2 <- as.POSIXct(dateN, origin="1970-01-01", tzone="MDT") > >> dateP2And you are telling the function to add as many hours offset as needed for the difference between GMT and MD T..... -- David.> > [1] "2011-05-22 18:45:00 MDT" >David Winsemius, MD West Hartford, CT