home *** CD-ROM | disk | FTP | other *** search
/ System Booster / System Booster.iso / Archives / Timing / QTime.lzh / qtime / qtime.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-01-18  |  2.4 KB  |  172 lines

  1. /*
  2.  * qtime.c - quickly describes current time to stdout
  3.  *
  4.  * Bruno Costa - 16 Mar 91 - 17 Mar 91
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <string.h>
  9.  
  10. #define NSLICES 12
  11. #define mround(min)    ((2 * (min) + 5) / 10)
  12. #define hround(hour)    ((hour) + (mround (m) > 6))
  13. #define slice(min)    (mround (min) % NSLICES)
  14. #define sliceoff(min)    ((min) - 5 * mround (min))
  15.  
  16. void getcurtime (int *hourp, int *minp);
  17. void print (char *msg);
  18.  
  19.  
  20. char *adverb (int h, int m)
  21. {
  22.  static char *advlist[] = {
  23.    " a little before",
  24.    " just before",
  25.    " exactly",
  26.    " just after",
  27.    " a little after"
  28.  };
  29.  
  30.  return advlist [sliceoff (m) + 2];
  31. }
  32.  
  33.  
  34. char *minstr (int h, int m)
  35. {
  36.  static char *minlist[NSLICES]  = {
  37.    "",
  38.    " five past",
  39.    " ten past",
  40.    " a quarter past",
  41.    " twenty past",
  42.    " twenty-five past",
  43.    " half past",
  44.    " twenty-five to",
  45.    " twenty to",
  46.    " a quarter to",
  47.    " ten to",
  48.    " five to"
  49.  };
  50.  
  51.  return minlist[slice (m)];
  52. }
  53.  
  54.  
  55. char *hourstr (int h, int m)
  56. {
  57.  static char *hlist[] = {
  58.   " midnight",
  59.   " one",
  60.   " two",
  61.   " three",
  62.   " four",
  63.   " five",
  64.   " six",
  65.   " seven",
  66.   " eight",
  67.   " nine",
  68.   " ten",
  69.   " eleven",
  70.   " midday",
  71.   " one",
  72.   " two",
  73.   " three",
  74.   " four",
  75.   " five",
  76.   " six",
  77.   " seven",
  78.   " eight",
  79.   " nine",
  80.   " ten",
  81.   " eleven"
  82.  };
  83.  
  84.  return hlist[hround (h) % 24];
  85. }
  86.  
  87.  
  88. char *oclock (int h, int m)
  89. {
  90.  return (slice (m) == 0  &&  (hround (h) % 12) != 0) ? " o'clock" : "";
  91. }
  92.  
  93.  
  94. void main (void)
  95. {
  96.  char timestr[80];
  97.  int hour, min;
  98.  
  99.  getcurtime (&hour, &min);
  100.  
  101.  strcpy (timestr, "It is");
  102.  strcat (timestr, adverb (hour, min));
  103.  strcat (timestr, minstr (hour, min));
  104.  strcat (timestr, hourstr (hour, min));
  105.  strcat (timestr, oclock (hour, min));
  106.  strcat (timestr, ".");
  107.  
  108.  print (timestr);
  109. }
  110.  
  111.  
  112. #if defined (DEBUG)
  113.  
  114. void getcurtime (int *hourp, int *minp)
  115. {
  116.  printf ("enter time (hh:mm):");
  117.  scanf ("%d:%d", hourp, minp);
  118. }
  119.  
  120.  
  121. void print (char *msg)
  122. {
  123.  puts (msg);
  124. }
  125.  
  126. #elif defined (AMIGA)
  127.  
  128. #include <libraries/dos.h>
  129. #include <proto/dos.h>
  130.  
  131.  
  132. void getcurtime (int *hourp, int *minp)
  133. {
  134.  static struct DateStamp date;
  135.  
  136.  DateStamp (&date);
  137.  
  138.  *hourp = date.ds_Minute / 60;
  139.  *minp = date.ds_Minute % 60;
  140. }
  141.  
  142.  
  143. void print (char *msg)
  144. {
  145.  Write (Output (), msg, strlen (msg));
  146.  Write (Output (), "\n", 1);
  147. }
  148.  
  149. #else
  150.  
  151. #include <time.h>
  152.  
  153. void getcurtime (int *hourp, int *minp)
  154. {
  155.  time_t t;
  156.  struct tm *tm;
  157.  
  158.  time (&t);
  159.  tm = localtime (&t);
  160.  
  161.  *minp = tm->tm_min;
  162.  *hourp = tm->tm_hour;
  163. }
  164.  
  165.  
  166. void print (char *msg)
  167. {
  168.  puts (msg);
  169. }
  170.  
  171. #endif
  172.