[Rd] POSIX time anomaly (PR#7317)

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Oct 28 08:24:35 CEST 2004


This is intentional: why did you send a bug report?  Please read the 
section on BUGS in the FAQ and tell us how this contradicts the 
documentation.

If you want a particular output format, you need to specify it:

x <- strptime("10/5/2004 00:00:00 CDT", "%m/%d/%Y %H:%M:%S %Z")
format(x, "%Y-%m-%d %H:%M:%S")
[1] "2004-10-05 00:00:00"

but print() is allowed to suppress 0's.  Did you bother to read the code:

> format.POSIXlt
function (x, format = "", usetz = FALSE, ...)
{
    if (!inherits(x, "POSIXlt"))
        stop("wrong class")
    if (format == "") {
        times <- unlist(unclass(x)[1:3])
        format <- if (all(times[!is.na(times)] == 0))
            "%Y-%m-%d"
        else "%Y-%m-%d %H:%M:%S"
    }
    .Internal(format.POSIXlt(x, format, usetz))
}

?

On Thu, 28 Oct 2004 mcintosh at research.telcordia.com wrote:

> Full_Name: Allen McIntosh
> Version: 2.0.0
> OS: RedHat 9.0
> Submission from: (NULL) (67.80.175.118)
> 
> 
> The POSIX time printing routine gives strange results when asked to print a time
> that is exactly midnight:
> 
> TZ=CST6CDT R -q --no-save
> > strptime("10/5/2004 00:00:01 CDT", "%m/%d/%Y %H:%M:%S %Z")
> [1] "2004-10-05 00:00:01"
> > strptime("10/5/2004 00:00:00 CDT", "%m/%d/%Y %H:%M:%S %Z")
> [1] "2004-10-05"
> > strptime("10/4/2004 24:00:00 CDT", "%m/%d/%Y %H:%M:%S %Z")
> [1] NA 
> 
> The first time is OK.  The second is missing the HH:MM:SS.
> I'm OK with the last one being NA, 

We don't need your permission: this is as documented.

> just did it to see if that was the way that
> the code wanted midnight.
> 
> Get the underlying # seconds:
> 
> > zz <- as.POSIXct(strptime("10/5/2004 00:00:00 CDT", "%m/%d/%Y %H:%M:%S %Z"))
> > attr(zz,"class") <- NULL
> > zz
> [1] 1096952400
> attr(,"tzone")
> [1] ""
> > 
> 
> and (just to see if the problem is glibc or something):
> $ cat ct.c
> #include <time.h>
> #include <stdio.h>
> main() {
>         time_t z = 1096952400;
>         printf("%s\n", ctime(&z));
>         return 0;
> }
> $ gcc -o ct ct.c
> $ TZ=CST6CDT ./ct
> Tue Oct  5 00:00:00 2004

What has ctime() to do with this?

> This problem also observed with R 1.6.0 and R 1.8.1 (RedHat 7.3)
> The timezone doesn't seem to matter - this is just the first date that tripped
> over this problem.


-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-devel mailing list