erich.neuwirth at univie.ac.at
2006-Sep-01 09:27 UTC
[Rd] Date conversion with as.POSIXct and as.POSIXlt (PR#9196)
Full_Name: Erich Neuwirth Version: 2.3.1 OS: Windows XP, Linux Submission from: (NULL) (131.130.135.167) Converting Sys.Date() to a POSIX compliant time type in different ways produces inconsistent results:> Sys.date()[1] "2006-09-01"> as.POSIXct(Sys.Date())[1] "2006-09-01 02:00:00 CEST"> as.POSIXlt(Sys.Date())[1] "2006-09-01"> as.POSIXct(as.POSIXlt(Sys.Date()))[1] "2006-09-01 01:00:00 CEST" Applying as.POSIXct directly or first applying as.POSIXlt and then applying as.POSIXct produces different results. This happens on Linux with timezone "CEST"> Sys.time()[1] "2006-09-01 11:03:36 CEST" and on Windows with timezone "W. Europe Daylight Time"> Sys.time()[1] "2006-09-01 11:11:15 W. Europe Daylight Time"
Peter Dalgaard
2006-Sep-01 10:08 UTC
[Rd] Date conversion with as.POSIXct and as.POSIXlt (PR#9196)
erich.neuwirth at univie.ac.at writes:> Full_Name: Erich Neuwirth > Version: 2.3.1 > OS: Windows XP, Linux > Submission from: (NULL) (131.130.135.167) > > > Converting Sys.Date() to a POSIX compliant time type in different ways > produces inconsistent results: > > Sys.date() > [1] "2006-09-01" > > as.POSIXct(Sys.Date()) > [1] "2006-09-01 02:00:00 CEST" > > as.POSIXlt(Sys.Date()) > [1] "2006-09-01" > > as.POSIXct(as.POSIXlt(Sys.Date())) > [1] "2006-09-01 01:00:00 CEST" > > Applying as.POSIXct directly or first applying as.POSIXlt and then > applying as.POSIXct produces different results. > > This happens on Linux with timezone "CEST" > > Sys.time() > [1] "2006-09-01 11:03:36 CEST" > and on Windows with timezone "W. Europe Daylight Time" > > Sys.time() > [1] "2006-09-01 11:11:15 W. Europe Daylight Time"Or,> as.POSIXct(Sys.Date()) - as.POSIXlt(Sys.Date())Time difference of 1 hours> as.POSIXct(Sys.time()) - as.POSIXlt(Sys.time())Time difference of -0.0003409386 secs Whereas> Sys.putenv("TZ"="GMT") > as.POSIXct(Sys.Date()) - as.POSIXlt(Sys.Date())Time difference of 0 secs and> Sys.putenv("TZ"="EST") > as.POSIXct(Sys.Date()) - as.POSIXlt(Sys.Date())Time difference of -5 hours So the difference is that as.POSIXct works in GMT whereas as.POSIXlt works in the local timezone w/o DST corrections, the point of the latter likely being that> as.POSIXlt(as.Date("2006-7-1"))-as.POSIXlt(as.Date("2006-1-1"))Time difference of 181 days comes out as an integer number of days. BTW, the habit of format.POSIXlt dropping off the time when it is exactly 01:00 in CEST (i.e. would have been midnight, save for Daylight Savings Time) is a bit confusing too... I'm not sure any of this is actual bugs (Brian will tell us soon enough), but it does appear at least to be underdocumented. -- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
Prof Brian Ripley
2006-Sep-01 10:21 UTC
[Rd] Date conversion with as.POSIXct and as.POSIXlt (PR#9196)
You misunderstand the role of timezones. as.POSIXct(Sys.Date()) is in UTC, as documented. as.POSIXlt(Sys.Date()) does not have a timezone (POSIXlt objects do not need one). as.POSIXct(as.POSIXlt(Sys.Date())) means as.POSIXct(as.POSIXlt(Sys.Date()), tz="") which is asking to interpret this in the local timezone. I've expanded the documentation, but I see no sign you had studied it so that it unlikely to help you. On Fri, 1 Sep 2006, erich.neuwirth at univie.ac.at wrote:> Full_Name: Erich Neuwirth > Version: 2.3.1 > OS: Windows XP, Linux > Submission from: (NULL) (131.130.135.167) > > > Converting Sys.Date() to a POSIX compliant time type in different ways > produces inconsistent results: > > Sys.date() > [1] "2006-09-01" > > as.POSIXct(Sys.Date()) > [1] "2006-09-01 02:00:00 CEST" > > as.POSIXlt(Sys.Date()) > [1] "2006-09-01" > > as.POSIXct(as.POSIXlt(Sys.Date())) > [1] "2006-09-01 01:00:00 CEST" > > Applying as.POSIXct directly or first applying as.POSIXlt and then > applying as.POSIXct produces different results. > > This happens on Linux with timezone "CEST" > > Sys.time() > [1] "2006-09-01 11:03:36 CEST" > and on Windows with timezone "W. Europe Daylight Time" > > Sys.time() > [1] "2006-09-01 11:11:15 W. Europe Daylight Time" > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
Gabor Grothendieck
2006-Sep-01 10:25 UTC
[Rd] Date conversion with as.POSIXct and as.POSIXlt (PR#9196)
Note that there is some discussion in the help desk article in R News 4/1 and a table of tested conversions at the end of that article. If x is of class "Date" then for converting from Date to POSIXct the table suggests as.POSIXct(format(x)) or as.POSIXct(format(x), tz = "GMT") according to whether you want the current time zone or GMT. On 9/1/06, erich.neuwirth at univie.ac.at <erich.neuwirth at univie.ac.at> wrote:> Full_Name: Erich Neuwirth > Version: 2.3.1 > OS: Windows XP, Linux > Submission from: (NULL) (131.130.135.167) > > > Converting Sys.Date() to a POSIX compliant time type in different ways > produces inconsistent results: > > Sys.date() > [1] "2006-09-01" > > as.POSIXct(Sys.Date()) > [1] "2006-09-01 02:00:00 CEST" > > as.POSIXlt(Sys.Date()) > [1] "2006-09-01" > > as.POSIXct(as.POSIXlt(Sys.Date())) > [1] "2006-09-01 01:00:00 CEST" > > Applying as.POSIXct directly or first applying as.POSIXlt and then > applying as.POSIXct produces different results. > > This happens on Linux with timezone "CEST" > > Sys.time() > [1] "2006-09-01 11:03:36 CEST" > and on Windows with timezone "W. Europe Daylight Time" > > Sys.time() > [1] "2006-09-01 11:11:15 W. Europe Daylight Time" > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >