home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / ADDTIME.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  3KB  |  114 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. **  ADDTIME.C - Add a time period to a base time, normalizing the result
  5. **
  6. **  arguments: 1  - Base hours (0 -> 24)
  7. **             2  - Base minutes (0 -> 59)
  8. **             3  - Base seconds (0 -> 59)
  9. **             4  - Span hours
  10. **             5  - Span minutes
  11. **             6  - Span seconds
  12. **             7  - Address of returned hours (0 -> 23)
  13. **             9  - Address of returned minutes (0 -> 59)
  14. **             10 - Address of returned seconds (0 -> 59)
  15. **             11 - Address of number of days to add to result
  16. **
  17. **  returns: 0 if no error, non-zero if base time range error
  18. **
  19. **  Notes: 1) Span values may be negative.
  20. **         2) Overflowing midnight will cause a positive number of days to be
  21. **            returned.
  22. **         3) Underflowing midnight will cause a negative number of days to be
  23. **            returned.
  24. **
  25. **  Original Copyright 1994 by Bob Stout as part of
  26. **  the MicroFirm Function Library (MFL)
  27. **
  28. **  The user is granted a free limited license to use this source file
  29. **  to create royalty-free programs, subject to the terms of the
  30. **  license restrictions specified in the LICENSE.MFL file.
  31. */
  32.  
  33. #include <stdlib.h>
  34. #include "datetime.h"
  35.  
  36. int add_time(unsigned basehrs, unsigned basemins, unsigned basesecs,
  37.              int spanhrs, int spanmins, int spansecs,
  38.              unsigned *hrs, unsigned *mins, unsigned *secs, int *days)
  39. {
  40.       int h, m, s;
  41.       div_t r;
  42.  
  43.       if (basehrs > 24 || basemins > 59 || basesecs > 59)
  44.             return -1;
  45.  
  46.       if (24 == basehrs)
  47.             basehrs = 0;
  48.       h = (int)basehrs  + spanhrs;
  49.       m = (int)basemins + spanmins;
  50.       s = (int)basesecs + spansecs;
  51.  
  52.       r = div(s, 60);
  53.       if (s < 0)
  54.       {
  55.             r.rem += 60;
  56.             --r.quot;
  57.       }
  58.       *secs = r.rem;
  59.       m += r.quot;
  60.  
  61.       r = div(m, 60);
  62.       if (m < 0)
  63.       {
  64.             r.rem += 60;
  65.             --r.quot;
  66.       }
  67.       *mins = r.rem;
  68.       h += r.quot;
  69.  
  70.       r = div(h, 24);
  71.       if (h < 0)
  72.       {
  73.             r.rem += 24;
  74.             --r.quot;
  75.       }
  76.       *hrs = r.rem;
  77.       *days = r.quot;
  78.  
  79.       return 0;
  80. }
  81.  
  82. #ifdef TEST
  83.  
  84. #include <stdio.h>
  85.  
  86. main(int argc, char *argv[])
  87. {
  88.       unsigned bh, bm, bs, h, m, s;
  89.       int sh, sm, ss, days;
  90.  
  91.       if (7 > argc)
  92.       {
  93.             puts("Usage: ADDTIME base_hrs base_mins base_secs "
  94.                   "span_hrs span_mins span_secs");
  95.             return EXIT_FAILURE;
  96.       }
  97.  
  98.       bh = (unsigned)atoi(argv[1]);
  99.       bm = (unsigned)atoi(argv[2]);
  100.       bs = (unsigned)atoi(argv[3]);
  101.       sh = atoi(argv[4]);
  102.       sm = atoi(argv[5]);
  103.       ss = atoi(argv[6]);
  104.  
  105.       printf("add_time() returned %d\n",
  106.             add_time(bh, bm, bs, sh, sm, ss, &h, &m, &s, &days));
  107.  
  108.       printf("%2d:%02d:%02d + %2d:%02d:%02d = %2d:%02d:%02d + %d days\n",
  109.             bh, bm, bs, sh, sm, ss, h, m, s, days);
  110.       return EXIT_SUCCESS;
  111. }
  112.  
  113. #endif /* TEST */
  114.