Path: utzoo!utgpu!water!watmath!clyde!att!osu-cis!tut.cis.ohio-state.edu!bloom-beacon!mit-eddie!ll-xn!ames!pasteur!ucbvax!decwrl!sun!pitstop!sundc!seismo!uunet!portal!cup.portal.com!doug-merritt
From: doug-merritt@cup.portal.com
Newsgroups: comp.sys.amiga.tech
Subject: Re: Decoding a DateStamp
Message-ID: <5258@cup.portal.com>
Date: 9 May 88 21:21:51 GMT
References: <24309@bbn.COM>
Organization: The Portal System (TM)
Lines: 133
XPortal-User-Id: 1.1001.4407

Funny you should ask, Bernie...I was just talking about this a bit
ago. Here's some C code to do what you want (showdate.c)
	Doug

-------------------------- CUT HERE ------------------------------------
/*
 * Showdate.c
 *
 * Example date routines; ripped from another of my programs to
 * answer a question on the net. Original routines worked fine;
 * this extract untested. Proceed with caution.
 * May 9 1988
 *
 * copyright 1987 Douglas R. Merritt; license to use is hereby granted.
 * Created Apr 1987.
 *
 * Known bugs:
 *	- Leap year check will fail in 2100 AD
 *	- Not as much fun as Tom Rokicki's ShowDate() function.
 *
 * Lemme know if you find any bugs; it's worked ok for me for the
 * last year.
 *
 *     Doug Merritt        ucbvax!sun.com!cup.portal.com!doug-merritt
 *                     or  ucbvax!eris!doug (doug@eris.berkeley.edu)
 *                     or  ucbvax!unisoft!certes!doug
 * 1995 Ashland Way
 * San Jose CA 95130
 * (408) 370-7875
 * Consultant; C programming for Unix & Amiga
 */

#include "stdio.h"

typedef struct {
	int	Dday;		/* day in month (1-31)	*/
	int	Dweekday;	/* day of week (Sun=0)	*/
	int	Dmonth;		/* month of year (0-11)	*/
	int	Dyear;		/* year AD (e.g. 1987)	*/
} DATE;

struct {
        char    *Mname;
        int     Mdays;
} calendar[12] = {
        { "Jan", 31 },   { "Feb", 28 },  { "Mar", 31 }, { "Apr", 30 },
        { "May", 31 },   { "Jun", 30 },  { "Jul", 31 }, { "Aug", 31 },
        { "Sep", 30 },   { "Oct", 31 },  { "Nov", 30 }, { "Dec", 31 }
};

#define	MINS_PER_HOUR	60
#define	TICS_PER_SEC	50

#define	YEARS_PER_CENTURY	100

main(ac, av)
	int	ac;
	char	**av;
{
	long	datestamp[3];
	DATE	*date, *getdate();

	/*
	 * display results from DateStamp() (hours:minutes:seconds)
	 */
	DateStamp(datestamp);
	printf("%02d:%02d:%02d\n",
		(int) datestamp[1] / MINS_PER_HOUR,
		(int) datestamp[1] % MINS_PER_HOUR,
		(int) datestamp[2] / TICS_PER_SEC);

	/*
	 * display results from getdate() (e.g. "03-May-88")
	 */
	date = getdate(datestamp[0]);
	printf("%02d-%s-%02d\n",
		date->Dday,
		calendar[ date->Dmonth ].Mname,
		(date->Dyear % YEARS_PER_CENTURY));
} /* main() */

/*
 * definitions to calculate current date
 */
#define	FEB		1	/* index of feb. in table (for leap years) */
#define	DAYS_PER_WEEK	7
#define	DAYS_PER_YEAR	365
#define	YEARS_PER_LEAP	4
#define	START_YEAR	1978
#define	LEAP_FEB_DAYS	29
#define	NORM_FEB_DAYS	28
#define	IsLeap(N)	(!((N) % YEARS_PER_LEAP))

/*
 * calculate current year, month, day of month, and day of week, given
 *	days elapsed since 1978 (see DateStamp())
 * Returns info in a static structure (not re-entrant)
 */
DATE	*
getdate(DaysElapsed)
        long     DaysElapsed;
{
	int     YearsElapsed, LeapYears, ThisYear, ThisDay, ThisMonth;
	static	DATE date;

	/* elapsed years and leap years since start of time */
	YearsElapsed = DaysElapsed / DAYS_PER_YEAR;
	LeapYears  = YearsElapsed / YEARS_PER_LEAP + IsLeap(START_YEAR);

	ThisYear = YearsElapsed + START_YEAR;

	/* day of year, after adjustment for previous leap years: */
	ThisDay  = (DaysElapsed - LeapYears) % DAYS_PER_YEAR;

	/* adjust length of february if this is a leap year */
	if (IsLeap(ThisYear))
		calendar[FEB].Mdays = LEAP_FEB_DAYS;

	/* find month of year, and day of month, from day of year */
	for (ThisMonth=0; ThisMonth<12; ThisMonth++) {
		if (ThisDay < calendar[ThisMonth].Mdays)
			 break;
		ThisDay -= calendar[ThisMonth].Mdays;
	}
	date.Dday = ThisDay+1;
	date.Dmonth = ThisMonth;
	date.Dyear = ThisYear;
	date.Dweekday = DaysElapsed % DAYS_PER_WEEK;

	/* reset in case called again for some other purpose */
	calendar[FEB].Mdays = NORM_FEB_DAYS;
	return(&date);
} /* getdate() */