[comp.sys.amiga.tech] Decoding a DateStamp

cosell@bbn.com (Bernie Cosell) (05/08/88)

Is there a routine tucked away in one of the libraries that'll convert
a struct DateStamp to something more people-palatable.  Just converting it
to a suitable string would be OK, but actually getting an array with
year, month, day, hour, minute separated would be neat.  I've been looking
through the kernel documentation I have and I can't find anything that does
that (or goes the other way: takes a string or array describing a date and
packs it into a struct DateStamp).  Thanks.
   __
  /  )                              Bernie Cosell
 /--<  _  __  __   o _              BBN Labs, Cambridge, MA 02238
/___/_(<_/ (_/) )_(_(<_             cosell@bbn.com

dillon@CORY.BERKELEY.EDU (Matt Dillon) (05/08/88)

:Is there a routine tucked away in one of the libraries that'll convert
:a struct DateStamp to something more people-palatable.  Just converting it
:to a suitable string would be OK, but actually getting an array with
:year, month, day, hour, minute separated would be neat.  I've been looking
:through the kernel documentation I have and I can't find anything that does
:that (or goes the other way: takes a string or array describing a date and
:packs it into a struct DateStamp).  Thanks.

	No.  Not in any of the system libraries.  However, routines to
do this type of conversion do exist in the C libraries for Aztec and 
Lattice.  

	The way DateStamp() is setup:

	[0] = # of days since (when?)
	[1] = # minutes that occured so far this day
	[2] = # of ticks elapsed in the current minute.

	It is quite easy to determine an ascii time stamp from the
first argument.  Not including daylight savings, you only have to worry
about leap years:

	-A leap year occurs every four years (mod 4), except three out
	 of every four century marks (mod 100) are NOT leap years.  The one
	 that is is (mod 400).

	Good luck,

					-Matt

backstro@silver.bacs.indiana.edu (05/09/88)

look in "librarires/dos.h" for a function called ShowDate.

cosell@bbn.com (Bernie Cosell) (05/09/88)

In article <97800005@silver> backstro@silver.bacs.indiana.edu writes:
}
}look in "librarires/dos.h" for a function called ShowDate.

sounds like it would be the perfect thing, but I can't find it.  No sign
of it in my dos.h, and I don't see any vector offset for it in dos.i -- I'm
not working out of C, so I can only use the standard-Commodore utilities.
Any chance what you found caem with Manx or Lattice?  /bernie\

doug-merritt@cup.portal.com (05/10/88)

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() */

backstro@silver.bacs.indiana.edu (05/10/88)

After looking at the program more closely it turns out that ShowDate is
defined somewhere in the program, so as soon as I find the routine
I'll mail you the source.

 +----------------------------------------------------------------------------+
 | James Colyer | #define LIFE (?) | AMIGA!  //// |  Running his 1 Meg Amiga  |
 |---------------------------------|        ////  |   1000 from a Lobby (!)   |
 | ARPA: backstro@silver.bacs.     |       ////   |---------------------------|
 |       indiana.edu               |      ////    | "There is no dark side on |
 | USSnail: 4755 N. Kinser Pike,   | \\\\////     |  the moon, really. Matter |
 |          Bloomington, IN, 47401 |  \\XX//      |  of fact it's all dark."  |
 |----------------------------------------------------------------------------|
 | The opinions expressed are those of a sick and deranged maniac.  Poor sod. |
 +----------------------------------------------------------------------------+

cosell@bbn.com (Bernie Cosell) (05/11/88)

Jim -- no need to send me the source code.  The simplest answer was sitting
under my nose.  Turns out that the ARP library includes StrtoStamp and
StamptoStr amidst a veritable cornucopia of other goodies.  If only I
knew what the "Famous" Heath file requester was, I'd probably understand what
the FileRequest() page is talking aobut... guess I should try calling it and
see what it does! :-)

Anyhow, this is a two pronged posting: (a) I'm all set now, I think -- thanks
for the help and the pointers, and (b) ARP looks really neat.. I wonder why I
mostly ignored it for so long!
   __
  /  )                              Bernie Cosell
 /--<  _  __  __   o _              BBN Labs, Cambridge, MA 02238
/___/_(<_/ (_/) )_(_(<_             cosell@bbn.com