home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 463.lha / Date_routines / datevals.c < prev    next >
C/C++ Source or Header  |  1991-01-04  |  2KB  |  69 lines

  1. /*****
  2.                               datevals()
  3.  
  4.       This function breaks a date string into integer variables. The
  5.    input string may use any nondigit separator (such as a slash, as in
  6.    MM/DD/YY) and may be in any of the formats given for w below.
  7.  
  8.    Argument list:    int *d         the day
  9.                      int *m          "  month
  10.                      int *y          "  year
  11.                      int w          the format, where:
  12.                                     0 = MM/DD/YY
  13.                                     1 = DD/MM/YY
  14.                                     2 = YY/MM/DD
  15.  
  16.    Return value:     void
  17.  
  18. *****/
  19.  
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <ctype.h>
  23. #include <string.h>
  24.  
  25. void datevals(char *s, int *d, int *m, int *y, int w)
  26. {
  27.    char *ptr, temp[5];
  28.    int i, vals[3];
  29.  
  30.    ptr = s;
  31.    i = 0;
  32.    for (;;) {
  33.       if (isdigit(*s)) {               /* Loop for separator      */
  34.          s++;
  35.          if (*s != '\0') {
  36.             continue;
  37.          }
  38.       }
  39.       strncpy(temp, ptr, s - ptr);     /* Form a digit string     */
  40.       temp[s - ptr] = '\0';
  41.       vals[i++] = atoi(temp);          /* Make it a number        */
  42.       if (*s == '\0') {                /* If done, end it...      */
  43.          break;
  44.       }
  45.       s++;                             /* otherwise keep parsing  */
  46.       ptr = s;
  47.    }
  48.    switch (w) {
  49.       case 0:                 /* Do MM/DD/YY */
  50.          *m = vals[0];
  51.          *d = vals[1];
  52.          *y = vals[2];
  53.          break;
  54.       case 1:                 /* Do DD/MM/YY */
  55.          *m = vals[1];
  56.          *d = vals[0];
  57.          *y = vals[2];
  58.          break;
  59.       case 2:                 /* Do YY/MM/DD */
  60.          *m = vals[1];
  61.          *d = vals[2];
  62.          *y = vals[0];
  63.          break;
  64.       default:                /* Error       */
  65.          *d = *m = *y = 0;
  66.          break;
  67.    }
  68. }
  69.