From donaldcallen at gmail.com  Mon Feb  4 16:07:06 2008
From: donaldcallen at gmail.com (Donald Allen)
Date: Mon, 4 Feb 2008 10:07:06 -0500
Subject: [Rd] strftime fails on POSIXct objects (PR#10695)
In-Reply-To: <47A7260B.6070702@biostat.ku.dk>
References: <20080204135512.48ED7283416A@mail.pubhealth.ku.dk>
	<47A7260B.6070702@biostat.ku.dk>
Message-ID: <d484362e0802040707u2d1815a0uf611da1721ef0fd5@mail.gmail.com>

On Feb 4, 2008 9:49 AM, Peter Dalgaard <P.Dalgaard at biostat.ku.dk> wrote:
> donaldcallen at gmail.com wrote:
> > R 2.6.1 on a Thinkpad T60 running up-to-date Gentoo:
> >
> > Despite the documentation, which says:
> >
> >      'strftime' is an alias for 'format.POSIXlt', and 'format.POSIXct'
> >      first converts to class '"POSIXlt"' by calling 'as.POSIXlt'.  Note
> >      that only that conversion depends on the time zone.
> >
> > strftime fails on POSIXct objects:
> >
> >
> I think the author of those lines would point out that they do NOT imply
> that it shouldn't fail.... In fact, why would you expect
> format.POSIXlt(x) to work on anything but POSIXlt objects?

Ah, you are quite right -- I mis-read the documentation.

Thanks --
/Don Allen




>
>     -p
>
>
> >> foo <- as.POSIXct(strptime(x='2007-09-22', format='%Y-%m-%d'))
> >> strftime(x=foo, format='%Y-%m-%d')
> >>
> > Error in strftime(x = foo, format = "%Y-%m-%d") : wrong class
> >
> > It's pretty clear why, given the first two lines of the function:
> >
> >> strftime
> >>
> > function (x, format = "", usetz = FALSE, ...)
> > {
> >     if (!inherits(x, "POSIXlt"))
> >         stop("wrong class")
> >     if (format == "") {
> >         times <- unlist(unclass(x)[1:3])
> >         secs <- x$sec
> >         secs <- secs[!is.na(secs)]
> >         np <- getOption("digits.secs")
> >         if (is.null(np))
> >             np <- 0
> >         else np <- min(6, np)
> >         if (np >= 1) {
> >             for (i in (1:np) - 1) if (all(abs(secs - round(secs,
> >                 i)) < 1e-06)) {
> >                 np <- i
> >                 break
> >             }
> >         }
> >         format <- if (all(times[!is.na(times)] == 0))
> >             "%Y-%m-%d"
> >         else if (np == 0)
> >             "%Y-%m-%d %H:%M:%S"
> >         else paste("%Y-%m-%d %H:%M:%OS", np, sep = "")
> >     }
> >     .Internal(format.POSIXlt(x, format, usetz))
> > }
> > <environment: namespace:base>
> >
> >
> > /Don Allen
> >
> > ______________________________________________
> > R-devel at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-devel
> >
>
>
> --
>    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
>
>
>



[Rd] strftime fails on POSIXct objects (PR#10695)
Donald Allen