I have some questions and comments on timezones. Problem 1. # get current time in current time zone> (now <- Sys.time())[1] "2003-07-29 18:23:58 Eastern Daylight Time" # convert this to GMT> (now.gmt <- as.POSIXlt(now,tz="GMT"))[1] "2003-07-29 22:23:58 GMT" # take difference> now-now.gmtTime difference of -5 hours Note that the difference between the times displayed by the first two R expressions is -4 hours. Why does the last expression return -5 hours? Problem 2. Why do the two expressions below give different answers? I take the difference between two dates in GMT and then repeat it in the current time zone (EDT). # days since origin in GMT> julian(as.POSIXct("2003-06-29",tz="GMT"),origin=as.POSIXct("1899-12-30",tz="GMT"))Time difference of 37801 days # days since origin in current timezone> julian(as.POSIXct("2003-06-29"),origin=as.POSIXct("1899-12-30"))Time difference of 37800.96 days I thought this might be daylight savings time related but even with standard time I get:> julian(as.POSIXct("2003-06-29",tz="EST"),origin=as.POSIXct("1899-12-30",tz="EST"))Time difference of 37800.96 days Problem 3. What is the general strategy of dealing with dates, as opposed to datetimes, in R? I have had so many problems and a great deal of frustration, mostly related to timezones. The basic problem is that various aspects of the date such as the year, the month, the day of the month, the day of the week can be different depending on the timezone you use. This is highly undesirable since I am not dealing with anything more granular than a day yet timezones, which are completely extraneous to dates and by all rights should not have to enter into my problems, keep fowling me up. A lesser problem is that I find myself using irrelevant constants such as the number of seconds in a day which you would think would be something I would not have to deal with since I am concerned with daily data. With R have nifty object oriented features I think a good project would be to implement a class in the base that handled dates without times (or timezones!) P.S. I have sent an earlier version of this but did not see it posted so if both get posted please ignore the prior one since this one has more info in it.
I share your concerns regarding Problems 1 and 2. However, I am unable to provide help on those at this moment. As for Problem 3, an alternative for the time being would be to use another package such as chron or date, although it would be preferable to use the classes of the base package if possible. Sorry I can't be more helpful. Jerome On July 30, 2003 09:19 pm, Gabor Grothendieck wrote:> I have some questions and comments on timezones. > > Problem 1. > > # get current time in current time zone > > > (now <- Sys.time()) > > [1] "2003-07-29 18:23:58 Eastern Daylight Time" > > # convert this to GMT > > > (now.gmt <- as.POSIXlt(now,tz="GMT")) > > [1] "2003-07-29 22:23:58 GMT" > > # take difference > > > now-now.gmt > > Time difference of -5 hours > > Note that the difference between the times displayed by the first two > R expressions is -4 hours. Why does the last expression return > -5 hours? > > > Problem 2. Why do the two expressions below give different answers? > I take the difference between two dates in GMT and then repeat it in the > current time zone (EDT). > > # days since origin in GMT > > > julian(as.POSIXct("2003-06-29",tz="GMT"),origin=as.POSIXct("1899-12-30 > >",tz="GMT")) > > Time difference of 37801 days > > # days since origin in current timezone > > > julian(as.POSIXct("2003-06-29"),origin=as.POSIXct("1899-12-30")) > > Time difference of 37800.96 days > > > I thought this might be daylight savings time related but even with > > standard time I get: > > julian(as.POSIXct("2003-06-29",tz="EST"),origin=as.POSIXct("1899-12-30 > >",tz="EST")) > > Time difference of 37800.96 days > > > Problem 3. What is the general strategy of dealing with dates, as > opposed to datetimes, in R? > > I have had so many problems and a great deal of frustration, mostly > related to timezones. > > The basic problem is that various aspects of the date such as the year, > the month, the day of the month, the day of the week can be different > depending on the timezone you use. This is highly undesirable since > I am not dealing with anything more granular than a day yet timezones, > which are completely extraneous to dates and by all rights should not > have to enter into my problems, keep fowling me up. > > A lesser problem is that I find myself using irrelevant constants such > as the number of seconds in a day which you would think would be > something I would not have to deal with since I am concerned with daily > data. > > With R have nifty object oriented features I think a good project would > be to implement a class in the base that handled dates without times > (or timezones!) > > P.S. I have sent an earlier version of this but did not see it posted > so if both get posted please ignore the prior one since this one has > more info in it. > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Hi Gabor, I believe I have an answer to your first two inquiries. Both have something to do with the daylight vs. standard times. If I understand correctly, "GMT" has only standard time. Consider this example.> (now <- Sys.time())[1] "2003-08-01 15:03:38 PDT"> (now.gmt <- as.POSIXlt(now,tz="GMT"))[1] "2003-08-01 22:03:38 GMT"> now-now.gmtTime difference of -8 hours One might rather expect -7 hours, but if you add 6 months to both date objects, then we have:> (now2 <- Sys.time()+60*60*24*366/2)[1] "2004-01-31 14:03:38 PST"> (now2.gmt <- as.POSIXlt(now2,tz="GMT"))[1] "2004-01-31 22:03:38 GMT"> now2-now2.gmtTime difference of -8 hours Which is correct. Note the change from "15:03:38 PDT" to "14:03:38 PST". I hope this helped you understand what was happening and how to use the POSIX classes for your needs. Cheers, Jerome On July 30, 2003 09:19 pm, Gabor Grothendieck wrote:> I have some questions and comments on timezones. > > Problem 1. > > # get current time in current time zone > > > (now <- Sys.time()) > > [1] "2003-07-29 18:23:58 Eastern Daylight Time" > > # convert this to GMT > > > (now.gmt <- as.POSIXlt(now,tz="GMT")) > > [1] "2003-07-29 22:23:58 GMT" > > # take difference > > > now-now.gmt > > Time difference of -5 hours > > Note that the difference between the times displayed by the first two > R expressions is -4 hours. Why does the last expression return > -5 hours? > > > Problem 2. Why do the two expressions below give different answers? > I take the difference between two dates in GMT and then repeat it in the > current time zone (EDT). > > # days since origin in GMT > > > julian(as.POSIXct("2003-06-29",tz="GMT"),origin=as.POSIXct("1899-12-30 > >",tz="GMT")) > > Time difference of 37801 days > > # days since origin in current timezone > > > julian(as.POSIXct("2003-06-29"),origin=as.POSIXct("1899-12-30")) > > Time difference of 37800.96 days > > > I thought this might be daylight savings time related but even with > > standard time I get: > > julian(as.POSIXct("2003-06-29",tz="EST"),origin=as.POSIXct("1899-12-30 > >",tz="EST")) > > Time difference of 37800.96 days > > > Problem 3. What is the general strategy of dealing with dates, as > opposed to datetimes, in R? > > I have had so many problems and a great deal of frustration, mostly > related to timezones. > > The basic problem is that various aspects of the date such as the year, > the month, the day of the month, the day of the week can be different > depending on the timezone you use. This is highly undesirable since > I am not dealing with anything more granular than a day yet timezones, > which are completely extraneous to dates and by all rights should not > have to enter into my problems, keep fowling me up. > > A lesser problem is that I find myself using irrelevant constants such > as the number of seconds in a day which you would think would be > something I would not have to deal with since I am concerned with daily > data. > > With R have nifty object oriented features I think a good project would > be to implement a class in the base that handled dates without times > (or timezones!) > > P.S. I have sent an earlier version of this but did not see it posted > so if both get posted please ignore the prior one since this one has > more info in it. > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Part of the problem is that 'now' is POSIXct and 'now.gmt' is POSIXlt. If you use as.POSIXct, you get the right answer.> (now <- Sys.time())[1] "2003-08-03 18:29:38 EDT"> str(now)`POSIXct', format: chr "2003-08-03 18:29:38"> (now.gmt <- as.POSIXlt(now,tz="GMT"))[1] "2003-08-03 22:29:38 GMT"> str(now.gmt)`POSIXlt', format: chr "2003-08-03 22:29:38"> (now.gmt <- as.POSIXct(now,tz="GMT"))[1] "2003-08-03 18:29:38 EDT"> str(now.gmt)`POSIXct', format: chr "2003-08-03 18:29:38"> now-now.gmtTime difference of 0 secs>__________________________________________________________ James Holtman "What is the problem you are trying to solve?" Executive Consultant -- Office of Technology, Convergys james.holtman at convergys.com (513) 723-2929 Jerome Asselin <jerome at hivnet.ubc.ca To: ggrothendieck at volcanomail.com, > r-help at stat.math.ethz.ch Sent by: cc: r-help-bounces at stat.m Subject: Re: [R] timezones ath.ethz.ch 07/31/2003 12:30 I share your concerns regarding Problems 1 and 2. However, I am unable to provide help on those at this moment. As for Problem 3, an alternative for the time being would be to use another package such as chron or date, although it would be preferable to use the classes of the base package if possible. Sorry I can't be more helpful. Jerome On July 30, 2003 09:19 pm, Gabor Grothendieck wrote:> I have some questions and comments on timezones. > > Problem 1. > > # get current time in current time zone > > > (now <- Sys.time()) > > [1] "2003-07-29 18:23:58 Eastern Daylight Time" > > # convert this to GMT > > > (now.gmt <- as.POSIXlt(now,tz="GMT")) > > [1] "2003-07-29 22:23:58 GMT" > > # take difference > > > now-now.gmt > > Time difference of -5 hours > > Note that the difference between the times displayed by the first two > R expressions is -4 hours. Why does the last expression return > -5 hours? > > > Problem 2. Why do the two expressions below give different answers? > I take the difference between two dates in GMT and then repeat it in the > current time zone (EDT). > > # days since origin in GMT > > > julian(as.POSIXct("2003-06-29",tz="GMT"),origin=as.POSIXct("1899-12-30 > >",tz="GMT")) > > Time difference of 37801 days > > # days since origin in current timezone > > > julian(as.POSIXct("2003-06-29"),origin=as.POSIXct("1899-12-30")) > > Time difference of 37800.96 days > > > I thought this might be daylight savings time related but even with > > standard time I get: > > julian(as.POSIXct("2003-06-29",tz="EST"),origin=as.POSIXct("1899-12-30 > >",tz="EST")) > > Time difference of 37800.96 days > > > Problem 3. What is the general strategy of dealing with dates, as > opposed to datetimes, in R? > > I have had so many problems and a great deal of frustration, mostly > related to timezones. > > The basic problem is that various aspects of the date such as the year, > the month, the day of the month, the day of the week can be different > depending on the timezone you use. This is highly undesirable since > I am not dealing with anything more granular than a day yet timezones, > which are completely extraneous to dates and by all rights should not > have to enter into my problems, keep fowling me up. > > A lesser problem is that I find myself using irrelevant constants such > as the number of seconds in a day which you would think would be > something I would not have to deal with since I am concerned with daily > data. > > With R have nifty object oriented features I think a good project would > be to implement a class in the base that handled dates without times > (or timezones!) > > P.S. I have sent an earlier version of this but did not see it posted > so if both get posted please ignore the prior one since this one has > more info in it. > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help______________________________________________ R-help at stat.math.ethz.ch mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help -- "NOTICE: The information contained in this electronic mail ...{{dropped}}
Thanks to Jerome Asselin (jerome at hivnet.ubc.ca) and Patrick Connolly (p.connolly at hortresearch.co.nz) for your work on this problem. We collectively were able to find two bugs, made easier by using our three different machines over 3 different time zones. See the following for details: https://www.stat.math.ethz.ch/pipermail/r-devel/2003-August/027165.html https://www.stat.math.ethz.ch/pipermail/r-devel/2003-August/027166.html Strategies for avoiding or partially avoiding timezone problems include the use of the chron package or the use of GMT for everything.
Maybe Matching Threads
- Date conversion with as.POSIXct and as.POSIXlt (PR#9196)
- daylight saving / time zone issues with as.POSIXlt/as.POSIXct (PR#10392)
- From POSIXct to numeric and back with time zone
- Understanding POSIXct creation on different OSes.
- patch for axis.POSIXct (related to timezones)