Dear All, I would like to learn the proper way to set the default time zone so I get the correct date for my files. The code below is non-reproducible (sorry) because it is based on a file on my system, but I hope someone will be able to help me anyway. I have a file that was last modified on 4/21/2015:> file.info("E:/snap/q/snap_q_q1_" %+% endPeriod %+% ".txt")$mtime[1] "2015-04-21 20:26:33 EDT" When I convert that to a date, I gives me 2015-04-22. I read about timezones and saw that there are two possible places to set the default values: One as a system variable and one as an option. To be safe I set both:> Sys.setenv(TZ='America/New_York') > Sys.getenv("TZ")[1] "America/New_York"> options(tz='America/New_York') > getOption("tz")[1] "America/New_York"> as.Date(file.info("E:/snap/q/snap_q_q1_" %+% endPeriod %+% ".txt")$mtime)[1] "2015-04-22" But as you can see R still gives me the wrong date. I can get the correct date as follows:> as.Date(file.info("E:/snap/q/snap_q_q1_" %+% endPeriod %+% ".txt")$mtime, tz='America/New_York')[1] "2015-04-21" But my question is why is the as.Date function not using the timezone I have set? Thank you in advance, Roger *************************************************************** This message and any attachments are for the named person's use only. This message may contain confidential, proprietary or legally privileged information. No right to confidential or privileged treatment of this message is waived or lost by an error in transmission. If you have received this message in error, please immediately notify the sender by e-mail, delete the message, any attachments and all copies from your system and destroy any hard copies. You must not, directly or indirectly, use, disclose, distribute, print or copy any part of this message or any attachments if you are not the intended recipient.
On Thu, Apr 23, 2015 at 8:01 AM, Bos, Roger <roger.bos at rothschild.com> wrote:> Dear All, > > I would like to learn the proper way to set the default time zone so I get > the correct date for my files. The code below is non-reproducible (sorry) > because it is based on a file on my system, but I hope someone will be able > to help me anyway. > > I have a file that was last modified on 4/21/2015: > > > file.info("E:/snap/q/snap_q_q1_" %+% endPeriod %+% ".txt")$mtime > [1] "2015-04-21 20:26:33 EDT" > > When I convert that to a date, I gives me 2015-04-22. I read about > timezones and saw that there are two possible places to set the default > values: One as a system variable and one as an option. To be safe I set > both: > > > Sys.setenv(TZ='America/New_York') > > Sys.getenv("TZ") > [1] "America/New_York" > > options(tz='America/New_York') > > getOption("tz") > [1] "America/New_York" > > as.Date(file.info("E:/snap/q/snap_q_q1_" %+% endPeriod %+% > ".txt")$mtime) > [1] "2015-04-22" > > But as you can see R still gives me the wrong date. I can get the correct > date as follows: > > > as.Date(file.info("E:/snap/q/snap_q_q1_" %+% endPeriod %+% > ".txt")$mtime, tz='America/New_York') > [1] "2015-04-21" > > But my question is why is the as.Date function not using the timezone I > have set? > > Thank you in advance, > Roger > >?Doing a ?file.info told me that the mtime variable is a POSIXct value. Doing a ?as.Date told me that when a POSIXct value is given to it, the time zone defaults to GMT, _not_ to the local time. That is my interpretation of the documentation.? <quote> The ?as.Date? methods accept character strings, factors, logical ?NA? and objects of classes ?"POSIXlt"? and ?"POSIXct"?. (The last is converted to days by ignoring the time after midnight in the representation of the time in specified time zone, default UTC.) Also objects of class ?"date"? (from package ?date?) and ?"dates"? (from package ?chron?). Character strings are processed as far as necessary for the format specified: any trailing characters are ignored. </quote> ?What I would do is: as.Date(file.info("..."),tz=getOption("tz"))? -- If you sent twitter messages while exploring, are you on a textpedition? He's about as useful as a wax frying pan. 10 to the 12th power microphones = 1 Megaphone Maranatha! <>< John McKown [[alternative HTML version deleted]]
In-line below On 23/04/2015 14:01, Bos, Roger wrote:> Dear All, > > I would like to learn the proper way to set the default time zone so I get the correct date for my files. The code below is non-reproducible (sorry) because it is based on a file on my system, but I hope someone will be able to help me anyway. > > I have a file that was last modified on 4/21/2015: > >> file.info("E:/snap/q/snap_q_q1_" %+% endPeriod %+% ".txt")$mtime > [1] "2015-04-21 20:26:33 EDT" > > When I convert that to a date, I gives me 2015-04-22. I read about timezones and saw that there are two possible places to set the default values: One as a system variable and one as an option. To be safe I set both: > >> Sys.setenv(TZ='America/New_York') >> Sys.getenv("TZ") > [1] "America/New_York" >> options(tz='America/New_York') >> getOption("tz") > [1] "America/New_York" >> as.Date(file.info("E:/snap/q/snap_q_q1_" %+% endPeriod %+% ".txt")$mtime) > [1] "2015-04-22" > > But as you can see R still gives me the wrong date. I can get the correct date as follows: > >> as.Date(file.info("E:/snap/q/snap_q_q1_" %+% endPeriod %+% ".txt")$mtime, tz='America/New_York') > [1] "2015-04-21" > > But my question is why is the as.Date function not using the timezone I have set?Because it has a tz= parameter which sets its time zone. I think you need to write your own wrapper function to pick up your preferred timezone.> > Thank you in advance, > Roger > > > *************************************************************** > This message and any attachments are for the named person's use only. > This message may contain confidential, proprietary or legally privileged > information. No right to confidential or privileged treatment > of this message is waived or lost by an error in transmission. > If you have received this message in error, please immediately > notify the sender by e-mail, delete the message, any attachments and all > copies from your system and destroy any hard copies. You must > not, directly or indirectly, use, disclose, distribute, > print or copy any part of this message or any attachments if you are not > the intended recipient. > > > ______________________________________________ > 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. >-- Michael http://www.dewey.myzen.co.uk/home.html