Skip to content

POSIX time anomaly (PR#7317)

2 messages · mcintosh@research.telcordia.com, Brian Ripley

#
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
[1] "2004-10-05 00:00:01"
[1] "2004-10-05"
[1] NA 

The first time is OK.  The second is missing the HH:MM:SS.
I'm OK with the last one being NA, just did it to see if that was the way that
the code wanted midnight.

Get the underlying # seconds:
[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

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.
#
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:
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@research.telcordia.com wrote:

            
We don't need your permission: this is as documented.
What has ctime() to do with this?