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

donaldcallen at gmail.com donaldcallen at gmail.com
Mon Feb 4 14:55:12 CET 2008


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:

> 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



More information about the R-devel mailing list