Cute time program

Maarten Koning mak at bnr-rsc.UUCP
Wed Mar 29 00:55:44 AEST 1989


[Here is the cute time program that people have been mentioning.  Enjoy.
 Mark Dapoz is mdapoz at hybrid. --mak]

#! /bin/sh
# This is a shell archive.  Remove anything before this line, then feed it
# into a shell via "sh file" or similar.  To overwrite existing files,
# type "sh file -c".
# The tool that generated this appeared in the comp.sources.unix newsgroup;
# send mail to comp-sources-unix at uunet.uu.net if you want that tool.
# If this archive is complete, you will see the following message at the end:
#		"End of shell archive."
# Contents:  qt.c
# Wrapped by smeg at malibu on Tue Mar 28 09:08:15 1989
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f 'qt.c' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'qt.c'\"
else
echo shar: Extracting \"'qt.c'\" \(3577 characters\)
sed "s/^X//" >'qt.c' <<'END_OF_FILE'
X/* qt.c - Displays time in real English, also chimes.      		*/
X/*	Mike Cowlishaw,  December 1979 - December 1982   		*/
X/* 	Mark Dapoz, Converted to C for UNIX, April 1986 		*/
X/*	   86/11/13 - major update, code really cleaned up :-)		*/
X
X#include <sys/types.h>
X#include <sys/time.h>
X#define	 TIMESIZ	80	/* size of our saying			*/
X
X#define	 DEBUG
X#undef	 DEBUG
X
Xchar *h[13] = {			/* numbers in words			*/
X	"one",
X	"two",
X	"three",
X	"four",
X	"five",
X	"six",  
X	"seven", 
X	"eight",
X	"nine",
X	"ten",  
X	"eleven", 
X	""
X};
X
Xmain(argc,argv)
Xint argc;
Xchar **argv;
X
X{
X	char	  ot[TIMESIZ];		/* words to display		*/
X	short int hr,mn,sc,mod;		/* time of day			*/
X	short int five_mn;		/* time to nearest five min	*/
X	time_t	  clock;		/* struct for time function	*/
X	struct tm *tm;
X	char	  *malloc();
X	struct tm *localtime();		/* conver to local time function*/
X	time_t	  time();		/* GMT time function		*/
X
X	strcpy(ot,"It's ");		/* our saying			*/
X
X	time(&clock);			/* GMT time of day from system	*/
X	tm=localtime(&clock);		/* get local time 		*/
X	hr=tm->tm_hour;
X	mn=tm->tm_min;
X	sc=tm->tm_sec;
X
X#ifdef DEBUG
X	if (argc > 1) {			/* debug			*/
X		hr=atoi(argv[1]);
X		mn=atoi(argv[2]);
X		sc=atoi(argv[3]);
X		printf("hr=%u, mn=%u, sc=%u\n",hr,mn,sc);
X	}
X#endif
X     
X	if (sc > 29) 			/* round up mins 		*/
X		mn+=1;
X	if (mn > 32)			/* round up hours		*/
X		hr+=1;
X					/* how far off 5 min mark?	*/
X	switch(mn%5) {	
X		case 0: 		/* exact hour			*/
X			break;
X 		case 1:	
X			strcat(ot,"just gone ");
X			break;
X		case 2:
X			strcat(ot,"just after ");
X			break;
X		case 3:
X			strcat(ot,"nearly ");
X			break;
X		case 4:
X			strcat(ot,"almost ");
X			break;
X	}
X	five_mn = mn;
X	if (mn%5 >= 3)			/* round up			*/
X		five_mn = mn+5;
X    					/* what part of the hour?	*/ 
X	switch(five_mn-(five_mn%5)) {	/* 5 minute resolution		*/
X	case 0:
X	case 60:	
X		break;
X	case 5:
X		strcat(ot,"five past ");
X		break;
X	case 10:	
X		strcat(ot,"ten past ");
X		break;
X	case 15:
X		strcat(ot,"a quarter past ");
X		break;
X	case 20:	
X		strcat(ot,"twenty past ");
X		break;	
X	case 25:
X		strcat(ot,"twenty-five past ");
X		break;
X	case 30:
X		strcat(ot,"half past ");
X		break;
X	case 35:
X		strcat(ot,"twenty-five to ");
X		break;
X	case 40:
X		strcat(ot,"twenty to ");
X		break;
X	case 45:
X		strcat(ot,"a quarter to ");
X		break;
X	case 50:
X		strcat(ot,"ten to ");
X		break;
X	case 55:
X		strcat(ot,"five to ");
X		break;
X	}
X
X	if ((hr%12) == 0)
X		midnoon(&hr,ot);	/* noon and midnight		*/
X	if (hr > 12)
X		hr -= 12;		/* get rid of 24-hour clock 	*/
X	else
X		if (hr == 0)
X			hr=12;		/* cater for midnight		*/
X					/* add the o'clock		*/
X	strcat(ot,h[hr-1]);
X	if ((mn%60 == 0) && (hr != 12))
X		strcat(ot," o'clock");
X	strcat(ot,".");			/* end of sentence		*/
X	if ((mn%15) == 0) 		/* chime every 15 minutes	*/
X		chime(&hr,&mn);
X	printf("\n%s\n\n",ot); 		/* town crier :-)		*/
X}
X
Xmidnoon(hr,ot)				/* is it noon or midnight?	*/
Xshort int *hr;		/* hour of day		*/
Xchar *ot;		/* the saying		*/
X
X{
X	if (*hr == 12) 			/* it's noon			*/
X		strcat(ot,"Noon");
X	else 				/* it's midnight		*/
X		strcat(ot,"Midnight");
X	*hr=12;				/* make it noon			*/
X}
X     
Xchime(hr,mn)				/* ring the bells		*/
Xshort int *hr;		/* hour of day		*/
Xshort int *mn;		/* minute of day	*/
X
X{
X	int	num,i;
X	char	bell[16], ring[80];
X
X	if ((*mn%60) == 0) {		/* on the hour?			*/
X		strcpy(bell,"Bong");
X		num = *hr;
X	}
X	else {	        		/* must be quarter hour		*/
X		strcpy(bell,"Ding-Dong");
X		num=(*mn%15);
X	}
X	strcpy(ring,"(");
X	strcat(ring,bell);		/* ring the bells		*/
X	for (i=0;i < num-1;i++) {
X		strcat(ring,",");
X		strcat(ring,bell);
X	}
X	strcat(ring,"!)");
X	printf("\n%s",ring);
X}

END_OF_FILE
echo shar: NEWLINE appended to \"'qt.c'\"
if test 3578 -ne `wc -c <'qt.c'`; then
    echo shar: \"'qt.c'\" unpacked with wrong size!
fi
# end of 'qt.c'
fi
echo shar: End of shell archive.
exit 0

--



More information about the Alt.sources mailing list