#include "..\h\config.h"

/*
 * The following code is operating-system dependent [@time.01].  Include files
 *  that are system-dependent.
 */

#if PORT
   /* probably needs something */
Deliberate Syntax Error
#endif					/* PORT */

#if AMIGA
#include "time.h"
#endif					/* AMIGA */

#if ATARI_ST
   /* nothing is needed */
#endif					/* ATARI_ST */

#if HIGHC_386 || MVS || VM
#include <time.h>
#endif					/* HIGHC_386 || MVS || ... */

#if MACINTOSH
#if LSC
#include <time.h>
#endif					/* LSC */
#if MPW
#include <types.h>
#include "time.h"
#include <OSUtils.h>
#include <Events.h>
#endif					/* MPW */
#endif					/* MACINTOSH */

#if MSDOS
#include <time.h>
#if MICROSOFT
#include <sys\types.h>
#endif					/* MICROSOFT */
#endif					/* MSDOS */

#if OS2
#include <time.h>
#include <sys\types.h>
#endif					/* OS2 */

#if UNIX
#ifdef CRAY
#define word	fubar_word
#include <sys\types.h>
#include <sys\times.h>
#undef word
#else					/* CRAY */
#include <sys\types.h>
#include <sys\times.h>
#endif					/* CRAY */
#include SysTime
#endif					/* UNIX */

#if VMS
#include <types.h>
#include <time.h>
struct tms {
    time_t    tms_utime;	/* user time */
    time_t    tms_stime;	/* system time */
    time_t    tms_cutime;	/* user time, children */
    time_t    tms_cstime;	/* system time, children */
};
#endif					/* VMS */

/*
 * End of operating-system specific code.
 */

static char *day[] = {
   "Sunday", "Monday", "Tuesday", "Wednesday",
   "Thursday", "Friday", "Saturday"
   };

static char *month[] = {
   "January", "February", "March", "April", "May", "June",
   "July", "August", "September", "October", "November", "December"
   };


/*
 * getitime - fill in a "struct cal_time" with information about the current
 *  time and date.
 */
novalue getitime(ct)
struct cal_time *ct;
   {

/*
 * The following code is operating-system dependent [@time.02]. Declarations
 *  for getting time.
 */

#if PORT
   long time();
   long xclock;
Deliberate Syntax Error
#endif					/* PORT */

#if AMIGA || OS2 || UNIX || VMS
   long time();
   long xclock;
#endif					/* AMIGA || OS2 || UNIX || VMS */

#if ATARI_ST
   struct tm {
       short tm_year;
       short tm_mon;
       short tm_wday;
       short tm_mday;
       short tm_hour;
       short tm_min;
       short tm_sec;
   };
   long xclock;
#endif					/* ATARI_ST */

#if MSDOS
   long xclock;
#if LATTICE || MICROSOFT || TURBO
   long time();
#endif					/* LATTICE || MICROSOFT || TURBO */
#if MWC
   time_t time();
#endif					/* MWC */
#endif					/* MSDOS */

#if HIGHC_386
   long time();
   time_t xclock;
#endif					/* HIGHC_386 */

#if MACINTOSH
#if LSC
   unsigned long xclock;
   unsigned long time();
#else					/* LSC */
   long xclock;
   long time();
#endif					/* LSC */
#endif					/* MACINTOSH */

#if MVS || VM
   time_t xclock;
#endif					/* MVS || VM */

/*
 * End of operating-system specific code.
 */

   struct tm *tbuf, *localtime();
/*
 * The following code is operating-system dependent [@time.03]. Code for
 *  getting time.
 */

#if PORT
   time(&xclock);
   tbuf = localtime(&xclock);
Deliberate Syntax Error
#endif					/* PORT */

#if AMIGA || HIGHC_386 || MACINTOSH || MSDOS || OS2 || UNIX || VMS || MVS || VM
   time(&xclock);
   tbuf = localtime(&xclock);
#endif					/* AMIGA || HIGHC || ... */

#if ATARI_ST
    tbuf = localtime(&xclock);
#endif					/* ATARI_ST */

/*
 * End of operating-system specific code.
 */

   ct->year = 1900 + tbuf->tm_year;
   ct->month_no = tbuf->tm_mon+1;
   ct->month_nm = month[tbuf->tm_mon];
   ct->mday = tbuf->tm_mday;
   ct->wday = day[tbuf->tm_wday];
   ct->hour = tbuf->tm_hour;
   ct->minute = tbuf->tm_min;
   ct->second = tbuf->tm_sec;
   return;
   }

/*
 * getctime - fill a buffer with the "ctime" representation of the current
 *  time and date. The buffer must be at least 26 characters.
 */
novalue getctime(sbuf)
char *sbuf;
   {
   struct cal_time ct;

   getitime(&ct);
   sprintf(sbuf, "%.3s %.3s%3d %.2d:%.2d:%.2d %d\n", ct.wday, ct.month_nm,
      ct.mday, ct.hour, ct.minute, ct.second, ct.year);
   return;
   }

/*
 * millisec - returns execution time in milliseconds. Time is measured
 *  from the fucntions's first call. The granularity of the time is
 *  generally more than one millisecond and on some systems it my only
 *  be accurate to the second.
 */
long millisec()
   {
   static int first_time = 1;

/*
 * The following code is operating-system dependent [@time.04]. Declarations
 *   that are system-dependent.
 */

#if PORT
   static long starttime;
   long time();
Deliberate Syntax Error
#endif					/* PORT */

#if AMIGA || ATARI_ST || OS2
   static long starttime;
   long time();
#endif					/* AMIGA || ATARI_ST || OS2 */

#if HIGHC_386
   static time_t hc_strtime;
   time_t hc_curtime;
   long time();
#endif					/* HIGHC_386 */

#if MACINTOSH
   static long starttime;
#endif					/* MACINTOSH */

#if MSDOS
   static long starttime;
#if LATTICE || MICROSOFT || TURBO
   long time();
#endif					/* LATTICE || MICROSOFT || TURBO */
#if MWC
   time_t time();
#endif					/* MWC */
#endif					/* MSDOS */

#if MVS || VM
   static clock_t starttime;
#endif                                  /* MVS || VM */

#if UNIX || VMS
   struct tms tp;
   static long starttime;
#endif					/* UNIX || VMS */

/*
 * End of operating-system specific code.
 */

   if (first_time) {
      first_time = 0;

/*
 * The following code is operating-system dependent [@time.05].  Get start
 *  time.
 */

#if PORT
      /* needs something */
Deliberate Syntax Error
#endif					/* PORT */

#if AMIGA  || ATARI_ST || MSDOS || OS2
      time(&starttime);	/* note: this obtains time in various units */
#endif					/* AMIGA || ATARI_ST || MSDOS */

#if HIGHC_386
      time(&hc_strtime);
#endif					/* HIGHC_386 */

#if MACINTOSH
      starttime = TickCount();	/* 60 ticks / second */
#endif					/* MACINTOSH */

#if MVS || VM
      starttime = clock();		/* microseconds */
#endif					/* MVS || VM */

#if UNIX || VMS
      times(&tp);
      starttime = tp.tms_utime;
#endif					/* UNIX || VMS */

/*
 * End of operating-system specific code.
 */

      return 0L;
      }
   else {	/* not first time */
/*
 * The following code is operating-system dependent [@time.06].  Get time.
 */

#if PORT
   /* needs something */
Deliberate Syntax Error
#endif					/* PORT */

#if AMIGA || MSDOS || OS2
      return 1000 * (time(NULL) - starttime);
#endif					/* AMIGA || MSDOS || OS2 */

#if ATARI_ST
      return (time(NULL) - starttime) / 10;
#endif					/* ATARI_ST */

#if HIGHC_386
      time(&hc_curtime);
      return 1000 * (long)difftime(hc_curtime,hc_strtime);
#endif					/* HIGHC_386 */

#if MACINTOSH
      return 1000 * ((extended)(TickCount() - starttime) / (extended)Hz);
#endif					/* MACINTOSH */

#if MVS || VM
#if SASC
#ifndef CLOCKS_PER_SEC       /* if late addition to the Std not there */
#define CLOCKS_PER_SEC CLK_TCK     /* use the old name */
#endif					/* CLOCKS_PRE_SEC */
      return (1000.0/CLOCKS_PER_SEC) * (clock() - starttime);
#endif					/* SASC */
#ifdef WATERLOO_C_V3_0
      return ((long)clock() - starttime) / 1000;
#endif					/* WATERLOO_C_V3_0 */
#endif					/* MVS || VM */

#if UNIX || VMS
      times(&tp);
      return 1000 * ((tp.tms_utime - starttime) / (double)Hz);
#endif					/* UNIX || VMS */

/*
 * End of operating-system specific code.
 */
      }
   }
