/*
 * qtime.c - quickly describes current time to stdout
 *
 * Bruno Costa - 16 Mar 91 - 17 Mar 91
 */

#include <stdio.h>
#include <string.h>

#define NSLICES 12
#define mround(min)	((2 * (min) + 5) / 10)
#define hround(hour)	((hour) + (mround (m) > 6))
#define slice(min)	(mround (min) % NSLICES)
#define sliceoff(min)	((min) - 5 * mround (min))

void getcurtime (int *hourp, int *minp);
void print (char *msg);


char *adverb (int h, int m)
{
 static char *advlist[] = {
   " a little before",
   " just before",
   " exactly",
   " just after",
   " a little after"
 };

 return advlist [sliceoff (m) + 2];
}


char *minstr (int h, int m)
{
 static char *minlist[NSLICES]  = {
   "",
   " five past",
   " ten past",
   " a quarter past",
   " twenty past",
   " twenty-five past",
   " half past",
   " twenty-five to",
   " twenty to",
   " a quarter to",
   " ten to",
   " five to"
 };

 return minlist[slice (m)];
}


char *hourstr (int h, int m)
{
 static char *hlist[] = {
  " midnight",
  " one",
  " two",
  " three",
  " four",
  " five",
  " six",
  " seven",
  " eight",
  " nine",
  " ten",
  " eleven",
  " midday",
  " one",
  " two",
  " three",
  " four",
  " five",
  " six",
  " seven",
  " eight",
  " nine",
  " ten",
  " eleven"
 };

 return hlist[hround (h) % 24];
}


char *oclock (int h, int m)
{
 return (slice (m) == 0  &&  (hround (h) % 12) != 0) ? " o'clock" : "";
}


void main (void)
{
 char timestr[80];
 int hour, min;

 getcurtime (&hour, &min);

 strcpy (timestr, "It is");
 strcat (timestr, adverb (hour, min));
 strcat (timestr, minstr (hour, min));
 strcat (timestr, hourstr (hour, min));
 strcat (timestr, oclock (hour, min));
 strcat (timestr, ".");

 print (timestr);
}


#if defined (DEBUG)

void getcurtime (int *hourp, int *minp)
{
 printf ("enter time (hh:mm):");
 scanf ("%d:%d", hourp, minp);
}


void print (char *msg)
{
 puts (msg);
}

#elif defined (AMIGA)

#include <libraries/dos.h>
#include <proto/dos.h>


void getcurtime (int *hourp, int *minp)
{
 static struct DateStamp date;

 DateStamp (&date);

 *hourp = date.ds_Minute / 60;
 *minp = date.ds_Minute % 60;
}


void print (char *msg)
{
 Write (Output (), msg, strlen (msg));
 Write (Output (), "\n", 1);
}

#else

#include <time.h>

void getcurtime (int *hourp, int *minp)
{
 time_t t;
 struct tm *tm;

 time (&t);
 tm = localtime (&t);

 *minp = tm->tm_min;
 *hourp = tm->tm_hour;
}


void print (char *msg)
{
 puts (msg);
}

#endif
