Grey Alien wrote:
Quote:
>I need to convert timestamps that are given as the number of seconds
>that have elapsed since midnight UTC of January 1, 1970,
>
>It seems all of the std C functions expect positive offsets from this
>date and are incapable of working on dates preceeding the epoch
That is implementation dependent. From N1124:
"7.23.1 ... 4 The range and precision of times representable in
clock_t and time_t are implementation-defined."
"7.23.2.4 ... 2 The time function determines the current calendar
time. The encoding of the value is unspecified."
Quote:
>...does anyone know how I can extract the century,
>year, month, day, hour, min, second from a time_t value that represents
>a timestamp BEFORE midnight UTC of January 1, 1970?
How were these time_t values produced? The code snippet below works
under 4.1.2 / Ubuntu 7.04.
May be you can port to your system the time functions from another
system that covers the time span you need.
Failing that, books such as "Calendrical calculations" will give you
all the information you need.
(
http://emr.cs.uiuc.edu/home/reingold...ok/index.shtml)
------------------------------------
$ cat time-test.c
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
* "... Armstrong landed the lunar module Eagle
* on the surface of the Moon at 4:17:42 p.m.
* Eastern Daylight Time, July 20, 1969"
*
*/
int main(void)
{
time_t landing;
struct tm man_on_the_moon;
struct tm *tmptr;
/* from struct tm to time_t */
man_on_the_moon.tm_sec = 42;
man_on_the_moon.tm_min = 17;
man_on_the_moon.tm_hour = 16;
man_on_the_moon.tm_mday = 20;
man_on_the_moon.tm_mon = 6;
man_on_the_moon.tm_year = 69;
man_on_the_moon.tm_isdst = 0;
landing = mktime(&man_on_the_moon);
if (landing == (time_t)-1)
{
puts("tm --time_t, conversion failed");
return EXIT_FAILURE;
}
puts("tm --time_t, conversion OK");
/* from time_t to struct tm */
memset(&man_on_the_moon, 0, sizeof(man_on_the_moon));
tmptr = gmtime(&landing);
if (tmptr == NULL)
{
puts("time_t --tm, conversion failed");
return EXIT_FAILURE;
}
man_on_the_moon = *tmptr;
puts("time_t --tm, conversion OK");
puts(asctime(&man_on_the_moon));
return EXIT_SUCCESS;
}
$ time-test
tm --time_t, conversion OK
time_t --tm, conversion OK
Sun Jul 20 16:17:42 1969
Roberto Waltman
[ Please reply to the group,
return address is invalid ]