home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume1 / 8707 / 47 < prev    next >
Encoding:
Internet Message Format  |  1990-07-13  |  5.5 KB

  1. From: mdapoz@watdragon.UUCP (Mark Dapoz)
  2. Newsgroups: comp.sources.misc
  3. Subject: Yet Another Time Program......
  4. Message-ID: <2926@ncoast.UUCP>
  5. Date: 17 Jul 87 22:25:50 GMT
  6. Sender: allbery@ncoast.UUCP
  7. Organization: U. of Waterloo, Ontario
  8. Lines: 235
  9. Approved: allbery@ncoast.UUCP
  10. X-Archive: comp.sources.misc/8707/47
  11.  
  12. While we're on the topic of time programs, I may as well put my 2 cents in.
  13. Here's a little program I wrote a while back to tell you the time in
  14. english.  It compiles fine on 4.[23] BSD machines and should be ok on
  15. most other systems, although no warranties are expresses or implied :-) 
  16.     -Mark
  17.  
  18. --- cut here --- cut here --- cut here --- cut here --- cut here --- cut here 
  19. # This is a shell archive.  Remove anything before this line,
  20. # then unpack it by saving it in a file and typing "sh file".
  21. #
  22. # Wrapped by watdragon!mdapoz on Fri Jun 26 00:21:34 EDT 1987
  23. # Contents:  qt.c qt.p
  24.  
  25. echo x - qt.c
  26. sed 's/^@//' > "qt.c" <<'@//E*O*F qt.c//'
  27. /* qt.c - Displays time in real English, also chimes.              */
  28. /*     Mark Dapoz, Converted to C for UNIX, April 1986         */
  29. /*    Mike Cowlishaw,  December 1979 - December 1982           */
  30. /*       86/11/13 - major update, code really cleaned up :-)        */
  31.  
  32. #include <sys/types.h>
  33. #include <sys/time.h>
  34. #define     TIMESIZ    80    /* size of our saying            */
  35.  
  36. #define     DEBUG
  37. #undef     DEBUG
  38.  
  39. char *h[13] = {            /* numbers in words            */
  40.     "one",
  41.     "two",
  42.     "three",
  43.     "four",
  44.     "five",
  45.     "six",  
  46.     "seven", 
  47.     "eight",
  48.     "nine",
  49.     "ten",  
  50.     "eleven", 
  51.     ""
  52. };
  53.  
  54. main(argc,argv)
  55. int argc;
  56. char **argv;
  57.  
  58. {
  59.     char      ot[TIMESIZ];        /* words to display        */
  60.     short int hr,mn,sc;        /* time of day            */
  61.     short int five_mn;        /* time to nearest five min    */
  62.     time_t      clock;        /* struct for time function    */
  63.     struct tm *tm;
  64.     struct tm *localtime();        /* conver to local time function*/
  65.     time_t      time();        /* GMT time function        */
  66.  
  67.     strcpy(ot,"It's ");        /* our saying            */
  68.  
  69.     time(&clock);            /* GMT time of day from system    */
  70.     tm=localtime(&clock);        /* get local time         */
  71.     hr=(short int)tm->tm_hour;
  72.     mn=(short int)tm->tm_min;
  73.     sc=(short int)tm->tm_sec;
  74.  
  75. #ifdef DEBUG
  76.     if (argc > 1) {            /* debug            */
  77.         hr=atoi(argv[1]);
  78.         mn=atoi(argv[2]);
  79.         sc=atoi(argv[3]);
  80.         printf("hr=%u, mn=%u, sc=%u\n",hr,mn,sc);
  81.     }
  82. #endif
  83.      
  84.     if (sc > 29)             /* round up mins         */
  85.         mn+=1;
  86.     if (mn > 32)            /* round up hours        */
  87.         hr+=1;
  88.                     /* how far off 5 min mark?    */
  89.     switch(mn%5) {    
  90.         case 0:         /* exact hour            */
  91.             break;
  92.          case 1:    
  93.             strcat(ot,"just gone ");
  94.             break;
  95.         case 2:
  96.             strcat(ot,"just after ");
  97.             break;
  98.         case 3:
  99.             strcat(ot,"nearly ");
  100.             break;
  101.         case 4:
  102.             strcat(ot,"almost ");
  103.             break;
  104.     }
  105.     five_mn = mn;
  106.     if (mn%5 >= 3)            /* round up            */
  107.         five_mn = mn+5;
  108.                         /* what part of the hour?    */ 
  109.     switch(five_mn-(five_mn%5)) {    /* 5 minute resolution        */
  110.     case 0:
  111.     case 60:    
  112.         break;
  113.     case 5:
  114.         strcat(ot,"five past ");
  115.         break;
  116.     case 10:    
  117.         strcat(ot,"ten past ");
  118.         break;
  119.     case 15:
  120.         strcat(ot,"a quarter past ");
  121.         break;
  122.     case 20:    
  123.         strcat(ot,"twenty past ");
  124.         break;    
  125.     case 25:
  126.         strcat(ot,"twenty-five past ");
  127.         break;
  128.     case 30:
  129.         strcat(ot,"half past ");
  130.         break;
  131.     case 35:
  132.         strcat(ot,"twenty-five to ");
  133.         break;
  134.     case 40:
  135.         strcat(ot,"twenty to ");
  136.         break;
  137.     case 45:
  138.         strcat(ot,"a quarter to ");
  139.         break;
  140.     case 50:
  141.         strcat(ot,"ten to ");
  142.         break;
  143.     case 55:
  144.         strcat(ot,"five to ");
  145.         break;
  146.     }
  147.  
  148.     if ((hr%12) == 0)
  149.         midnoon(&hr,ot);    /* noon and midnight        */
  150.     if (hr > 12)
  151.         hr -= 12;        /* get rid of 24-hour clock     */
  152.     else
  153.         if (hr == 0)
  154.             hr=12;        /* cater for midnight        */
  155.                     /* add the o'clock        */
  156.     strcat(ot,h[hr-1]);
  157.     if ((mn%60 == 0) && (hr != 12))
  158.         strcat(ot," o'clock");
  159.     strcat(ot,".");            /* end of sentence        */
  160.     if ((mn%15) == 0)         /* chime every 15 minutes    */
  161.         chime(&hr,&mn);
  162.     printf("\n%s\n\n",ot);         /* town crier :-)        */
  163. }
  164.  
  165. midnoon(hr,ot)                /* is it noon or midnight?    */
  166. short int *hr;        /* hour of day        */
  167. char *ot;        /* the saying        */
  168.  
  169. {
  170.     if (*hr == 12)             /* it's noon            */
  171.         strcat(ot,"Noon");
  172.     else                 /* it's midnight        */
  173.         strcat(ot,"Midnight");
  174.     *hr=12;                /* make it noon            */
  175. }
  176.      
  177. chime(hr,mn)                /* ring the bells        */
  178. short int *hr;        /* hour of day        */
  179. short int *mn;        /* minute of day    */
  180.  
  181. {
  182.     int    num,i;
  183.     char    bell[16], ring[80];
  184.  
  185.     if ((*mn%60) == 0) {        /* on the hour?            */
  186.         strcpy(bell,"Bong");
  187.         num = *hr;
  188.     }
  189.     else {                    /* must be quarter hour        */
  190.         strcpy(bell,"Ding-Dong");
  191.         num=(*mn%15);
  192.     }
  193.     strcpy(ring,"(");
  194.     strcat(ring,bell);        /* ring the bells        */
  195.     for (i=0;i < num-1;i++) {
  196.         strcat(ring,",");
  197.         strcat(ring,bell);
  198.     }
  199.     strcat(ring,"!)");
  200.     printf("\n%s",ring);
  201. }
  202. @//E*O*F qt.c//
  203. chmod u=rw,g=r,o=r qt.c
  204.  
  205. echo x - qt.p
  206. sed 's/^@//' > "qt.p" <<'@//E*O*F qt.p//'
  207. @.TH QT PUBLIC UW
  208. @.SH NAME
  209. qt \- print the current time in english (also chimes)
  210. @.SH SYNOPSIS
  211. @.B public qt 
  212. @.SH DESCRIPTION
  213. @.PP
  214. QT can be used to display the current time in a more "user friedly" manner
  215. than date does.
  216. @.SH AUTHOR
  217. Mark Dapoz, University of Waterloo
  218. @.SH SEE ALSO
  219. date(1)
  220. @.SH BUGS
  221. @.PP
  222. This is the first public release.  Unknown bugs may still be present :-).
  223. @.SH SUPPORT
  224. This software is
  225. @.I not
  226. supported by
  227. @.SM MFCF.
  228. @.PP
  229. Send complaints or suggestions to mdapoz@watdragon.
  230. @//E*O*F qt.p//
  231. chmod u=rw,g=r,o=r qt.p
  232.  
  233. echo Inspecting for damage in transit...
  234. temp=/tmp/shar$$; dtemp=/tmp/.shar$$
  235. trap "rm -f $temp $dtemp; exit" 0 1 2 3 15
  236. cat > $temp <<\!!!
  237.      175     538    3587 qt.c
  238.       23      87     487 qt.p
  239.      198     625    4074 total
  240. !!!
  241. wc  qt.c qt.p | sed 's=[^ ]*/==' | diff -b $temp - >$dtemp
  242. if [ -s $dtemp ]
  243. then echo "Ouch [diff of wc output]:" ; cat $dtemp
  244. else echo "No problems found."
  245. fi
  246. exit 0
  247.