Re: [R] day, month, year functions

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Fri 18 Aug 2006 - 04:32:00 EST

On 8/17/06, Martin Maechler <maechler@stat.math.ethz.ch> wrote:
> >>>>> "Gregor" == Gregor Gorjanc <gregor.gorjanc@bfro.uni-lj.si>
> >>>>> on Fri, 11 Aug 2006 00:27:27 +0000 (UTC) writes:
>
> Gregor> Gabor Grothendieck <ggrothendieck <at> gmail.com> writes:
> >>
> >> Here are three ways:
> >>
> >> xx <- as.Date("2006-01-05")
> >>
> >> # 1. use as.POSIXlt
> >> as.POSIXlt(xx)$mday
> >> as.POSIXlt(xx)$mon + 1
> >> as.POSIXlt(xx)$year + 1900
> >>
> >> # 2. use format
> >> as.numeric(format(xx, "%d"))
> >> as.numeric(format(xx, "%m"))
> >> as.numeric(format(xx, "%Y"))
> >>
> >> # 3. use month.day.year in chron package
> >> library(chron)
> >> month.day.year(unclass(xx))$day
> >> month.day.year(unclass(xx))$month
> >> month.day.year(unclass(xx))$year
>
> Gregor> Hi,
>
> Gregor> it would really be great if there would be
>
> Gregor> sec(), min(), hour() day(), month(), year()
>
> Gregor> generic functions that would work on all "date" classes. Where
> Gregor> applicable of course. I imagine that argument to get out integer
> Gregor> or character would alse be nice.
>
> I disagree pretty strongly:
>
> - We definitely don't want min() to return minutes instead of
> minimum !
>
> - Why pollute the namespace with 6 (well, actualy 5!) new
> function names, when as.POSIXlt()
> *REALLY* is there exactly for this purpose ???
>
> I rather think the authors of each of the other old-fashioned
> "date" classes should provide as.POSIXlt() methods for their
> classes.
>
> Then, we'd have uniform interfaces, following's Gabor's "# 1."
> above.
>
> Martin Maechler, ETH Zurich

There are two problems:

  1. as.POSIXlt is not generic. (This problem may not be too important given that as.POSIXlt does handle "Date" and chron "dates" classes already but in terms of handling all potential classes its a limitation.)
  2. in the case of as.POSIXlt converting chron "dates" objects to POSIXlt there is a time zone consideration, as shown below, where today, August 17th in the Eastern Daylight Time zone, is displayed as August 16th using as.POSIXlt unless we use tz = "GMT"

> library(chron)
> # today is August 17th.
> Sys.Date()

[1] "2006-08-17"
> chron(unclass(Sys.Date()))

[1] 08/17/06
> Sys.time()

[1] "2006-08-17 14:28:19 Eastern Daylight Time"
> as.POSIXlt(Sys.Date())

[1] "2006-08-17"
> as.POSIXlt(chron(unclass(Sys.Date())))
[1] "2006-08-16 20:00:00 Eastern Daylight Time"
> as.POSIXlt(chron(unclass(Sys.Date())), tz = "GMT")
[1] "2006-08-17 GMT"
> R.version.string # Windows XP
[1] "Version 2.3.1 Patched (2006-06-04 r38279)"



R-help@stat.math.ethz.ch 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. Received on Fri Aug 18 04:37:27 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Fri 18 Aug 2006 - 18:20:56 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.