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

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. **  PARSTIME.C - A simple parser to extract times from strings.
  5. **
  6. **  Public domain by Bob Stout
  7. */
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include "datetime.h"
  12.  
  13.  
  14. /*
  15. **  parse_time() - Parse a time string into its components
  16. **
  17. **  Arguments: 1 - String to parse
  18. **             2 - Address of hours storage
  19. **             3 - Address of minutes storage
  20. **             4 - Address of seconds storage
  21. **
  22. **  Returns: 0 For success, non-zero for range errors
  23. */
  24.  
  25. Boolean_T parse_time(const char *str,
  26.                      unsigned   *hours,
  27.                      unsigned   *mins,
  28.                      unsigned   *secs)
  29. {
  30.       unsigned hh, mm, ss;                /* Local data                 */
  31.  
  32.       if (3 != sscanf((char *)str, "%u:%u:%u", &hh, &mm, &ss))
  33.             return Error_;
  34.       if (hh > 23 || mm > 59 || ss > 59)
  35.             return Error_;
  36.       *hours = hh;
  37.       *mins  = mm;
  38.       *secs  = ss;
  39.       return Success_;
  40. }
  41.  
  42. #ifdef TEST
  43.  
  44. main(int argc, char *argv[])
  45. {
  46.       if (2 > argc)
  47.       {
  48.             puts("Usage: PARSTIME time_string [...time_string]");
  49.             return EXIT_FAILURE;
  50.       }
  51.       while (--argc)
  52.       {
  53.             char *str;
  54.             unsigned hh, mm, ss;
  55.             Boolean_T retval;
  56.  
  57.             retval = parse_time(str = *++argv, &hh, &mm, &ss);
  58.             printf("parse_time(\"%s\") returned %d\n", str, retval);
  59.             if (Success_ == retval)
  60.                   printf("  time = %02u:%02u:%02u\n\n", hh, mm, ss);
  61.       }
  62.       return EXIT_SUCCESS;
  63. }
  64.  
  65. #endif /* TEST */
  66.