[alt.sources] correct qtime

lhf@aries5 (Luiz H. deFigueiredo) (09/13/89)

Just the other day some Ade Lovett posted qtime.
Unfortunately it had a bug in it (I'll let you discover/come across it).
Here is my version.
I've changed some names and removed the need for a buffer.
Also, it now outputs a single line.
Enjoy!
-------------------------------------------------------------------------------
Luiz Henrique de Figueiredo		internet: lhf@aries5.uwaterloo.ca
Computer Systems Group			bitnet:   lhf@watcsg.bitnet
University of Waterloo
-------------------------------------------------------------------------------
/*
* qtime.c
* displays time in real English, also chimes
* Luiz Henrique de Figueiredo
* original by Mike Cowlishaw, Mark Dapoz and Ade Lovett
* 11 Sep 89
*/

#include <stdio.h>
#include <sys/types.h>
#include <time.h>

#define show(s)		fputs(s,stdout)
#define divide(m,x)	(((x)%(m))==0)

static char *adverb[]=
{
 NULL, "just after ", "a little after ", "nearly ", "almost "
};

static char *mark[]=
{
 NULL, "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 ", ""
};

static char *hour[]=
{
 NULL, "one", "two", "three", "four", "five", "six", "seven", "eight", "nine",
 "ten", "eleven"
};

void main()
{
 int h, m;
 time_t clock, time();
 struct tm *tm, *localtime();

 time(&clock);
 tm = localtime(&clock);
 h = tm->tm_hour + (tm->tm_min >= 33);
 m = tm->tm_min  + (tm->tm_sec >= 30);

 if (divide(15,m))
 {
  if (!divide(60,m))
   show("(Ding-Dong!) ");
  else
  {
   int n;
   n=h%12; if (n==0) n=12;
   show("(");
   while (--n>0)
    show("Bong,");
   show("Bong!) ");
  }
 }

 show("It's ");
 show(adverb[m%5]);
 show(mark[(m+2)/5]);

 switch (h)
 {
  case 0:
  case 24:
   show("midnight");
   break;
  case 12:
   show("noon");
   break;
  default:
   show(hour[h%12]);
   if (divide(60,m))
    show(" o'clock");
   break;
 }
 show(".\n");
}

amos@taux01.UUCP (Amos Shapir) (09/13/89)

In article <472@maytag.waterloo.edu> lhf@aries5 (Luiz H. deFigueiredo) writes:
>Just the other day some Ade Lovett posted qtime.
>Unfortunately it had a bug in it (I'll let you discover/come across it).
>Here is my version.
...
>static char *adverb[]=
>{
> NULL, "just after ", "a little after ", "nearly ", "almost "
>};

This is yet another bug, important enough to require posting -
NULL is not the same as "" !  The latter is an empty string, while
the former is an *illegal* string.  At best, your system may print "(null)"
when you try to print it, at worst it would print garbage or even
dump core.

-- 
	Amos Shapir		amos@taux01.nsc.com or amos@nsc.nsc.com
National Semiconductor (Israel) P.O.B. 3007, Herzlia 46104, Israel
Tel. +972 52 522261  TWX: 33691, fax: +972-52-558322
34 48 E / 32 10 N			(My other cpu is a NS32532)