[Rd] Date conversion with as.POSIXct and as.POSIXlt
Erich Neuwirth
erich.neuwirth at univie.ac.at
Thu Sep 7 10:34:08 CEST 2006
After the discussions which really helped me understanding
the issues involved let me summarize some possibly surprising
behavior of as.POSIXlt and as.POSIXct and the print method of
class POSIXlt (the print method problem seems to be a Windows
only problem) in R 2.3.1
- as.POSIXlt and as.POSIXct accept but ignore tz parameters
when the argument object is of class Date. They do react to
tz parameters when the argument object is a string representing
a date.
> as.POSIXct(firstjan)
[1] "2006-01-01 01:00:00 CET"
> as.POSIXct(firstjan,tz="HST10")
[1] "2006-01-01 01:00:00 CET"
> as.POSIXlt(firstjan)
[1] "2006-01-01"
> as.POSIXlt(firstjan,tz="HST10")
[1] "2006-01-01"
> as.POSIXct(as.character(firstjan))
[1] "2006-01-01 CET"
> as.POSIXct(as.character(firstjan),tz="HST10")
[1] "2006-01-01 HST"
> as.POSIXlt(as.character(firstjan))
[1] "2006-01-01"
> as.POSIXlt(as.character(firstjan),tz="HST10")
[1] "2006-01-01 HST10"
Furthermore, for string arguments the timezone label for as.POSIXct
and as.POSIXlt is different in the example above ("HST" and "HST10")
- as.POSIXlt in R 2.4.0alpha (I could test this only on Windows)
seems to behave slightly differently, it attaches timezone UTC
for inputs of class Date.
> as.POSIXlt(firstjan)
[1] "2006-01-01 UTC"
> as.POSIXlt(firstjan,tz="HST10")
[1] "2006-01-01 UTC"
It still does not attach a timezone if the input is a string
representing a date (like in the first example).
- as.POSIXct will give different results for Date objects
and their string representations in non-UTC timezones
(the first example illustrates this fact)
- the print method for POSIXlt objects on Windows in some cases
will not display the correct timezone label, it will convert it
to a sometimes somewhat strange 3 letter string. The docs state
that timezone labels have to have a standardized format,
but (at least for me) it is unexpected that using Sys.timezone()
as tz argument gives a very strange timezone label.
See the following example
> Sys.time()
[1] "2006-09-05 23:05:48 W. Europe Daylight Time"
> as.POSIXct(Sys.time(),tz=Sys.timezone())
[1] "2006-09-05 23:06:06 W. Europe Daylight Time"
> as.POSIXlt(Sys.time(),tz=Sys.timezone())
[1] "2006-09-05 22:06:11 Eur"
--
Erich Neuwirth, University of Vienna
Faculty of Computer Science
Computer Supported Didactics Working Group
Visit our SunSITE at http://sunsite.univie.ac.at
Phone: +43-1-4277-39464 Fax: +43-1-4277-39459
More information about the R-devel
mailing list