I stumbled upon an unexpected behaviour of QNX v4.25G (Proc32 v4.25Q) - it
seemingly continues to handle the time properly at least for some more
days past 19/01/2038.
Here is an example:
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <tzfile.h>
#define NEED_MJD 1
#ifdef NEED_MJD
define JULIAN_OFFSET (1721059 - 2400000) /* use MJD */
#else
define JULIAN_OFFSET 1721059 /* full Julian Days */
#endif
typedef unsigned long jday;
jday
Tm2Julian( struct tm Tm )
{
jday JulDay;
int Year; / full four-digit year /
int Year1; / Year, less 1 */
Year = Tm->tm_year + TM_YEAR_BASE;
Year1 = Year - 1; /* year, less 1 /
JulDay = Year * 365; / approx days /
JulDay += (Year+3) / 4; / leap years /
JulDay -= Year1 / 100; / fix for 100 /
JulDay += Year1 / 400; / fix for 400 /
JulDay += Tm->tm_yday; / ordinal day /
JulDay += JULIAN_OFFSET; / fix */
return JulDay;
}
time_t
mjd2unixtime( unsigned long jday )
{
time_t unixtime;
// if( 40587 <= jday & jday <= 90297 ) /* apparently works /
if( 40587 <= jday & jday <= 65442 ) / garateed to work /
{
unixtime = ( jday - 40587 ) * 606024;
return( unixtime);
}
return -1; / there is no time_t representation for dates /
} / before 01/01/1970 and after 19/01/2038 */
void
main( void )
{
jday jd;
time_t t_o_d;
char buf[26];
struct tm tmbuf;
jd = 40586; /* 31/12/1969 /
t_o_d = mjd2unixtime( jd );
_gmtime( &t_o_d, &tmbuf );
printf( “MJD(%d)==%.24s GMT, time_t==0x%lx\n”, jd, _asctime( &tmbuf, buf
), t_o_d );
jd = 40587; / 01/01/1970 /
t_o_d = mjd2unixtime( jd );
_gmtime( &t_o_d, &tmbuf );
printf( “MJD(%d)==%.24s GMT, time_t==0x%lx\n”, jd, _asctime( &tmbuf, buf
), t_o_d );
jd = 45000; / 31/01/1982 /
t_o_d = mjd2unixtime( jd );
_gmtime( &t_o_d, &tmbuf );
printf( “MJD(%d)==%.24s GMT, time_t==0x%lx\n”, jd, _asctime( &tmbuf, buf
), t_o_d );
jd = 53005; / 01/01/2004 /
t_o_d = mjd2unixtime( jd );
_gmtime( &t_o_d, &tmbuf );
printf( “MJD(%d)==%.24s GMT, time_t==0x%lx\n”, jd, _asctime( &tmbuf, buf
), t_o_d );
jd = 53963; / 16/08/2006 /
t_o_d = mjd2unixtime( jd );
_gmtime( &t_o_d, &tmbuf );
printf( “MJD(%d)==%.24s GMT, time_t==0x%lx)\n”, jd, _asctime( &tmbuf, buf
), t_o_d );
jd = 65442; / 19/01/2038 /
t_o_d = mjd2unixtime( jd );
_gmtime( &t_o_d, &tmbuf );
printf( “MJD(%d)==%.24s GMT, time_t==0x%lx\n”, jd, _asctime( &tmbuf, buf
), t_o_d );
jd = 65443; / 20/01/2038 /
t_o_d = mjd2unixtime( jd );
_gmtime( &t_o_d, &tmbuf );
printf( “MJD(%d)==%.24s GMT, time_t==0x%lx\n”, jd, _asctime( &tmbuf, buf
), t_o_d );
jd = 90296; / 06/02/2106 /
t_o_d = mjd2unixtime( jd );
_gmtime( &t_o_d, &tmbuf );
printf( “MJD(%d)==%.24s GMT, time_t==0x%lx\n”, jd, _asctime( &tmbuf, buf
), t_o_d );
jd = 90297; / 07/02/2106 */
t_o_d = mjd2unixtime( jd );
_gmtime( &t_o_d, &tmbuf );
printf( “MJD(%d)==%.24s GMT, time_t==0x%lx\n”, jd, _asctime( &tmbuf, buf
), t_o_d );
}
Somewhere closer to 2106 the date starts to be off by one day - instead of
07/02/2106 it shows 06/02/2106.
Please comment!
Tony.