[comp.sys.amiga.tech] getting the date in Manx C

sjk@utastro.UUCP (Scot Kleinman) (09/01/88)

Please forgive me as a new C programmer, but I have what I hope is a simple
question.  I would like to write a program which retrieves the current date
from the system.  Since I input the date once in my startup-sequence, I would
like to avoid having to enter it again.  I am using MANX 3.6 on an Amiga 1000.
Thanks for your help.

Scot

sjk@astro.as.utexas.edu (ARPA)

thad@cup.portal.com (09/03/88)

Since you stated you're using Manx, the simplest solution would be for you
to use the time(), ctime(), localtime(), gmtime() and asctime() functions
as documented in Manx' manual, section lib68, page titled TIME (C).

Also, the following code may be useful to you.  It's one of several hundreds
of kwik'n'dirty throwaways I did back in '85 when testing every documented
feature of the Amiga.  The "basic" code also works fine on DEC-20, Vax,
AT&T UNIX PC, C64/C128, and every other system I've tried it on; the only
difference is the "#define DDELTA"; I'll be putting system/compiler cognizance
in the routine (e.g. #ifdef ...) for a later release.

Originally compiled this with Lattice 3.02; to compile with any recent Manx:

	CLI> cc +L dater
	CLI> ln dater -lc32

Feel welcome to use this example for your own learning; I'm intending this
(along with my complete date arithmetic package) to be part of an article
I intend completing (Real Soon Now :-) along with examples in C, assembler,
Fortran, AmigaBasiC, Modula-2 and Pascal.  This specific example has worked
perfectly thru AmigaDOS 1.0, 1.1, and 1.2 (and presumably 1.3 and beyond).

In case it isn't immediately evident, the routine used by dater.c (following)
has been tested for all dates from Jan.1,0000 to Dec.31,9999 (neglecting
the calendar change during the Middle Ages (or whenever).

An annotated version  ( yeah, I'll put in some comments :-)  may appear in
the updated appendix of Rob Peck's PROGRAMMER'S GUIDE TO THE AMIGA and/or
on the program disk accompanying the book.

Thad Floryan   [ thad@cup.portal.com  (or)  ..!sun!portal!cup.portal.com!thad ]

#	This is a shell archive.
#	Remove everything above and including the cut line.
#	Then run the rest of the file through sh.
#----cut here-----cut here-----cut here-----cut here----#
#!/bin/sh
# shar:    Shell Archiver
#	Run the following text with /bin/sh to create:
#	dater.c
# This archive created: Sat Sep  3 03:00:12 1988
echo shar: extracting dater.c
sed 's/^X//' << \SHAR_EOF > dater.c
X/* DATER.C     Check out the DateStamp AmigaDOS system function
X *
X *    see page 2-15 in the AmigaDOS Developer's Manual for more info
X */
X
Xstruct DS {          /* DateStamp structure */
X   long  NDays;      /* Days from Jan. 1, 1978 (a Sunday) */
X   long  NMinutes;   /* Minutes into the current day */
X   long  NTicks;     /* Clock ticks (1/50 sec = 1 tick) in current second */
X};
X
Xstatic char *wkdays[] =
X{"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
X
Xstatic char *mthnam[] =
X{"","JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"};
X
Xmain()
X{
X   void DateStamp(), YrMoDa();
X
X   struct DS datime;
X   long month, day, year, hour, minute, second;
X
X   DateStamp(&datime);
X   printf("Days since 1-JAN-78 = %d\n", datime.NDays);
X   printf("Minutes into today  = %d\n", datime.NMinutes);
X   printf("1/50 secs in minute = %d\n", datime.NTicks);
X
X   YrMoDa(datime.NDays, &year, &month, &day);
X
X   printf("\nTherefore today is ");
X   printf("%s ", wkdays[datime.NDays % 7]);
X
X   printf("%d-%s-%d", day, mthnam[month], year);
X
X   hour   = datime.NMinutes / 60;
X   minute = datime.NMinutes % 60;
X   second = datime.NTicks / 50;
X   printf(" %2d:%02d:%02d\n", hour, minute, second);
X
X   exit(0);
X}
X
X/****************************** YrMoDa **********************************
X *
X *    Extracts the component month, day and year from an AmigaDOS
X *    internal `day number' based from Jan.1, 1978.
X *
X *    The calculations herein use the following assertions:
X *
X *    146097 = number of days in 400 years per 400 * 365.2425 = 146097.00
X *     36524 = number of days in 100 years per 100 * 365.2425 =  36524.25
X *      1461 = number of days in   4 years per   4 * 365.2425 =   1460.97
X *
X *    Thad Floryan, 12-NOV-85
X */
X
X#define DDELTA 722449   /* days from Jan.1,0000 to Jan.1,1978 */
X
Xstatic int mthvec[] =
X   {-1, -1, 30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333, 364};
X
Xvoid YrMoDa(intdat, yr, mo, da)
X   long intdat;   /* I: AmigaDOS format, days since 1-JAN-78 */
X   long *yr;      /* O: resultant year in form YYYY */
X   long *mo;      /* O: resultant month in form MM */
X   long *da;      /* O: resultant day of month in form DD */
X{
X   register long jdate, day0, day1, day2, day3;
X
X   jdate = intdat + DDELTA;  /* adjust internal date to Julian */
X
X   *yr   = (jdate / 146097) * 400;
X   day0  = day1 = jdate %= 146097;
X   *yr  += (jdate / 36524) * 100;
X   day2  = day1 %= 36524;
X   *yr  += (day2 / 1461) * 4;
X   day3  = day1 %= 1461;
X   *yr  += day3 / 365;
X   *mo   = 1 + (day1 %= 365);
X   *da   = *mo % 30;
X   *mo  /= 30;
X
X   if ( ( day3 >= 59 && day0 < 59 ) ||
X        ( day3 <  59 && (day2 >= 59 || day0 < 59) ) )
X      ++day1;
X
X   if (day1 > mthvec[1 + *mo]) ++*mo;
X   *da = day1 - mthvec[*mo];
X}
X
SHAR_EOF
if test 2756 -ne "`wc -c dater.c`"
then
echo shar: error transmitting dater.c '(should have been 2756 characters)'
fi
#	End of shell archive
exit 0