#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <time.h>

#ident "$Id: y2038.c,v 1.4 2007/11/04 01:53:09 ian Exp $"
#ident "Copyright 1991, 1997 Ian F. Darwin, ian at darwinsys.com"

/*
 * A program to demonstrate the maximum useful time_t value on UNIX.
 * Note that both gmtime() and ctime() have to work correctly;
 * on several candidate systems current as of 1996/1997, ctime()
 * works beyond 2038, but gmtime() does not.
 */
int main(int argc, char **argv)
{
	time_t cur_second = time(0), 
		billion = 1000000000L,
		doom_second = 0x7fffffff;
	int curYear, doomYear;

	printf("It is now %s", ctime(&cur_second));
	printf("That is %ld seconds since the beginning of time\n",
		cur_second);
	printf("(actually since the beginning of the modern computing era).\n\n");
	printf("One Billion Seconds of UNIX occurred at %s\n",
		ctime(&billion));
	printf("\n");
	printf("32-bit time ends when time_t reaches %x, or %s\n", 
		doom_second, ctime(&doom_second));
	printf("And unless ye buy a 64 bit UNIX, let that be known as\n");
	printf("THE HOUR OF THY DOOM, for this very architecture\n");
	printf("shall crumble about thy feet one second afterward!\n");
	printf("\n");

	curYear = getYear(cur_second);

	/* now roll the doom_second clock forward :-) */
	for ( ; doom_second!=0; ++doom_second) {	
		doomYear = getYear(doom_second);
		/* printf("doomYear=%d\n", doomYear); */
		if (doomYear <= curYear) {
			printf("The first second when time runs backward: (%8x,%ld)",
				doom_second, doom_second);
			printf(" shall be known as %d to gmtime() but to ctime() as %s" ,
				doomYear, ctime(&doom_second));
			return;
		}
	}
}

int getYear(time_t t) {
	return 1900+(gmtime(&t)->tm_year);
}

