From P.Dalgaard at biostat.ku.dk  Mon Feb  4 15:49:47 2008
From: P.Dalgaard at biostat.ku.dk (Peter Dalgaard)
Date: Mon, 04 Feb 2008 15:49:47 +0100
Subject: [Rd] strftime fails on POSIXct objects (PR#10695)
In-Reply-To: <20080204135512.48ED7283416A@mail.pubhealth.ku.dk>
References: <20080204135512.48ED7283416A@mail.pubhealth.ku.dk>
Message-ID: <47A7260B.6070702@biostat.ku.dk>

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?

    -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)
Peter Dalgaard