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

Peter Dalgaard P.Dalgaard at biostat.ku.dk
Mon Feb 4 15:49:47 CET 2008


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



More information about the R-devel mailing list