home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2152 < prev    next >
Internet Message Format  |  1990-12-28  |  2KB

  1. From: boyd@necisa.ho.necisa.oz (Boyd Roberts)
  2. Newsgroups: alt.sources
  3. Subject: Re: Looking for a tool to make UNIX-Time
  4. Message-ID: <1948@necisa.ho.necisa.oz>
  5. Date: 26 Nov 90 04:24:32 GMT
  6.  
  7. Here's a mktime() sort of thing based on calling localtime(3) and
  8. binary chopping on a guessed UNIX time long.  Ok?
  9.  
  10.  
  11. Boyd Roberts            boyd@necisa.ho.necisa.oz.au
  12.  
  13. ``When the going gets wierd, the weird turn pro...''
  14.  
  15. <---cut-here--->
  16. /*
  17.  *    Convert a struct tm to a number of seconds.
  18.  */
  19. #include    <time.h>
  20.  
  21. /*
  22.  *    Compare two ints.
  23.  */
  24. int
  25. cmp(a, b)    
  26. register int    a;
  27. register int    b;
  28. {
  29.     register int    x;
  30.  
  31.     if ((x = a - b) == 0)
  32.         return 0;
  33.  
  34.     return x > 0 ? 1 : -1;
  35. }
  36.  
  37. /*
  38.  *    Compare two struct tm's.  Only the year, month, day of the
  39.  *    month, hour, minute and second fields are used.
  40.  */
  41. int
  42. tmcmp(t1p, t2p)
  43. register struct tm    *t1p;
  44. register struct tm    *t2p;
  45. {
  46.     register int    x;
  47.  
  48.     if
  49.     (
  50.         (x = cmp(t1p->tm_year, t2p->tm_year)) == 0
  51.         &&
  52.         (x = cmp(t1p->tm_mon, t2p->tm_mon)) == 0
  53.         &&
  54.         (x = cmp(t1p->tm_mday, t2p->tm_mday)) == 0
  55.         &&
  56.         (x = cmp(t1p->tm_hour, t2p->tm_hour)) == 0
  57.         &&
  58.         (x = cmp(t1p->tm_min, t2p->tm_min)) == 0
  59.         &&
  60.         (x = cmp(t1p->tm_sec, t2p->tm_sec)) == 0
  61.     )
  62.         return 0;
  63.  
  64.     return x;
  65. }
  66.  
  67. /*
  68.  *    Take a struct tm and return the associated number of seconds.
  69.  */
  70. long
  71. gmsecs(tp)
  72. register struct tm    *tp;
  73. {
  74.     register struct tm    *ltp;
  75.     register int        bit;
  76.     long            t;
  77.  
  78.     t = 0;
  79.  
  80.     /*
  81.      *    Binary chop by formulating a guess at what the time may be.
  82.      *    Avoid negative times, as localtime() loses its mind.
  83.      */
  84.     for (bit = 30; bit >= 0; bit--)
  85.     {
  86.         /* guess */
  87.         t |= 1 << bit;
  88.  
  89.         ltp = localtime(&t);
  90.  
  91.         switch (tmcmp(tp, ltp))
  92.         {
  93.         case -1:
  94.             /* guess > time -- unset this guess bit */
  95.             t ^= 1 << bit;
  96.             break;
  97.  
  98.         case 0:
  99.             /* found it */
  100.             return t;
  101.  
  102.         case 1:
  103.             /* time > guess -- guess was in the right direction */
  104.             break;
  105.         }
  106.     }
  107.  
  108.     return t;
  109. }
  110.