donaldcallen at gmail.com
2008-Feb-04 13:55 UTC
[Rd] strftime fails on POSIXct objects (PR#10695)
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:> strftimefunction (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
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
Apparently Analagous Threads
- Timezone issue with strftime/strptime and %z and %Z
- segfault during build of 2.1.0 on RH9; print.POSIXct implicated (PR#7827)
- (PR#7826) segfault during build of 2.1.0 on RH9; print.POSIXct
- print.POSIXct doesn't seem to use tz argument, as per its example
- Infrequent but steady NULL-pointer caused segfault in as.POSIXlt.POSIXct (R 3.4.4)