Samuel Brown
2012-Jul-02 16:24 UTC
[R] Undocumented behavior around daylight savings time?
Apologies for the intrusion. I am a lurker on list. I have been working to convert a digitized signal from a matlab file into R for analysis and other applications. R.matlab is working fine, and it is easy to convert the matlab date-time number (days since year 0) into R date-time numbers (seconds since 1970-01-01). Unfortunately, when I cast the R date-time number into POSIXct format it seems to adjust silently by one hour to reflect daylight savings time, but I have been unable to suppress that behavior. (The problem is that the matlab date is already in MDT, and I don't want to have to write my own code to suppress the added hour only when local DST rules apply.) To use the example of the R date-time number 1340717324, this is the behavior I observe:> as.POSIXct(1340717324,origin='1970-01-01')[1] "2012-06-26 14:28:44 MDT"> as.POSIXct(1340717324,origin='1970-01-01',tz='')[1] "2012-06-26 14:28:44 MDT"> as.POSIXct(1340717324,origin='1970-01-01',tz='America/Denver')[1] "2012-06-26 14:28:44 MDT"> as.POSIXct(1340717324,origin='1970-01-01',tz='MST')[1] "2012-06-26 13:28:44 MST"> as.POSIXct(1340717324,origin='1970-01-01',tz='UTC')[1] "2012-06-26 13:28:44 UTC" I was ultimately able to solve the problem by casting into and out of a character string, but that seems risky/error prone. rdates='%Y-%m-%d %H:%M:%S'>as.POSIXct(strptime(as.character(as.POSIXct(1340717324,origin='1970-01-01',tz='UTC')),format=rdates)) [1] "2012-06-26 13:28:44 MDT" I have read the various help entries and even investigated the lubridates package, but none indicate why exactly the extra hour is being added or how to suppress it. (Note that I tried as.POSIXlt with various settings of isdst, none of which worked). I'm using "R version 2.13.1 (2011-07-08)" on "x86_64-apple-darwin9.8.0". -- ---------------- Samuel Brown [[alternative HTML version deleted]]
Jeff Newmiller
2012-Jul-02 17:09 UTC
[R] Undocumented behavior around daylight savings time?
Set your default local timezone (at least while converting to POSIXt types: Sys.setenv(TZ="Etc/GMT+7") --------------------------------------------------------------------------- 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 --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. Samuel Brown <samuelbrown at gmail.com> wrote:>Apologies for the intrusion. I am a lurker on list. > >I have been working to convert a digitized signal from a matlab file >into R >for analysis and other applications. R.matlab is working fine, and it >is >easy to convert the matlab date-time number (days since year 0) into R >date-time numbers (seconds since 1970-01-01). > >Unfortunately, when I cast the R date-time number into POSIXct format >it >seems to adjust silently by one hour to reflect daylight savings time, >but >I have been unable to suppress that behavior. (The problem is that the >matlab date is already in MDT, and I don't want to have to write my own >code to suppress the added hour only when local DST rules apply.) > >To use the example of the R date-time number 1340717324, this is the >behavior I observe: > >> as.POSIXct(1340717324,origin='1970-01-01') > >[1] "2012-06-26 14:28:44 MDT" > >> as.POSIXct(1340717324,origin='1970-01-01',tz='') > >[1] "2012-06-26 14:28:44 MDT" > >> as.POSIXct(1340717324,origin='1970-01-01',tz='America/Denver') > >[1] "2012-06-26 14:28:44 MDT" > >> as.POSIXct(1340717324,origin='1970-01-01',tz='MST') > >[1] "2012-06-26 13:28:44 MST" > >> as.POSIXct(1340717324,origin='1970-01-01',tz='UTC') > >[1] "2012-06-26 13:28:44 UTC" >I was ultimately able to solve the problem by casting into and out of a >character string, but that seems risky/error prone. > >rdates='%Y-%m-%d %H:%M:%S' >> >as.POSIXct(strptime(as.character(as.POSIXct(1340717324,origin='1970-01-01',tz='UTC')),format=rdates)) >[1] "2012-06-26 13:28:44 MDT" > >I have read the various help entries and even investigated the >lubridates >package, but none indicate why exactly the extra hour is being added or >how >to suppress it. (Note that I tried as.POSIXlt with various settings of >isdst, none of which worked). > >I'm using "R version 2.13.1 (2011-07-08)" on >"x86_64-apple-darwin9.8.0". > > > >-- > >---------------- >Samuel Brown > > [[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.
Apparently Analagous Threads
- daylight saving / time zone issues with as.POSIXlt/as.POSIXct (PR#10392)
- DateTime Math in R - POSIXct
- daylight saving / time zone issues with as.POSIXlt/as.POSIXct (PR#10393)
- [vpim] Some small rrule.rb changes
- Entering times around the start of daylight savings time