home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume18 / geneal / part01 / dates.c < prev    next >
C/C++ Source or Header  |  1989-03-08  |  4KB  |  172 lines

  1. /* dates.c - some date manipulation routines
  2.  *
  3.  *  4.Jan.88  jimmc  Initial definition
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include <ctype.h>
  8. #include <strings.h>
  9. #include "geneal.h"
  10.  
  11. struct {
  12.     char *sname;
  13.     char *lname;
  14. } montab[] = {
  15.     { "???", "???" },
  16.     { "Jan", "January" },
  17.     { "Feb", "February" },
  18.     { "Mar", "March" },
  19.     { "Apr", "April" },
  20.     { "May", "May" },
  21.     { "Jun", "June" },
  22.     { "Jul", "July" },
  23.     { "Aug", "August" },
  24.     { "Sep", "September" },
  25.     { "Oct", "October" },
  26.     { "Nov", "November" },
  27.     { "Dec", "December" },
  28. };
  29. int montabsize = sizeof(montab)/sizeof(montab[0]);
  30.  
  31. int        /* returns 1 to 12 for a month, 0 if not found */
  32. findmonth(sname)
  33. char *sname;
  34. {
  35.     int i;
  36.  
  37.     for (i=1; i<montabsize; i++) {
  38.         if (strcmp(sname,montab[i].sname)==0)
  39.             return i;
  40.     }
  41.     return 0;    /* not found */
  42. }
  43.  
  44. int            /* (year*10000) + (month(jan==1)*100 + day(1-based) */
  45.             /* or -1 if it can't make the conversion */
  46. sdatetoint(olddate)    /*    for example, decimal 18760123 */
  47. char *olddate;        /* in format "23-Jan-1876" */
  48. {
  49.     char *dash1, *dash2;
  50.     int day, month, year;
  51.     int n;
  52.  
  53.     if (!olddate || !olddate[0]) return -1;    /* nothing to convert */
  54.     dash1 = index(olddate,'-');
  55.     if (dash1) {
  56.         dash2 = index(dash1+1,'-');
  57.         if (dash2) {    /* all three of day, month, year given */
  58.             if (strlen(dash2)!=sizeof("-1876")-1 ||
  59.                 (dash2-dash1)!=sizeof("-Jan")-1 ||
  60.                 (dash1-olddate>2)) {
  61.                 goto badformat;
  62.             }
  63.             day = atoi(olddate);
  64.             year = atoi(dash2+1);
  65.             *dash2 = 0;
  66.             month = findmonth(dash1+1);
  67.             *dash2 = '-';
  68.         }
  69.         else {    /* only two of day, month, year given */
  70.             if (isdigit(olddate[0])) {    /* day given */
  71.                 if (dash1-olddate>2)
  72.                     goto badformat;
  73.                 day = atoi(olddate);
  74.                 if (isdigit(dash1[1])) { /* year given */
  75.                     if (strlen(dash1)!=sizeof("-1876")-1)
  76.                         goto badformat;
  77.                     year = atoi(dash1+1);
  78.                     month = 0;
  79.                 }
  80.                 else {
  81.                     if (strlen(dash1)!=sizeof("-Jan")-1)
  82.                         goto badformat;
  83.                     year = 0;
  84.                     month = findmonth(dash1+1);
  85.                 }
  86.             }
  87.             else {    /* must be month and year */
  88.                 if (strlen(dash1)!=sizeof("-1876")-1
  89.                     || (dash1-olddate!=sizeof("Jan")-1))
  90.                     goto badformat;
  91.                 day = 0;
  92.                 year = atoi(dash1+1);
  93.                 *dash1 = 0;
  94.                 month = findmonth(olddate);
  95.                 *dash1 = '-';
  96.             }
  97.         }
  98.     }
  99.     else {        /* only one of day, month, year given */
  100.         if (isdigit(olddate[0])) {
  101.             n = atoi(olddate);
  102.             if (n>=32) {    /* assume no years before 100 */
  103.                 if (strlen(olddate)!=sizeof("1876")-1)
  104.                     goto badformat;
  105.                 year = n;
  106.                 day = 0;
  107.             }
  108.             else {
  109.                 if (strlen(olddate)<2)
  110.                     goto badformat;
  111.                 year = n;
  112.                 year = 0;
  113.                 day = n;
  114.             }
  115.             month = 0;
  116.         }
  117.         else {        /* not a digit, must be a month */
  118.             if (strlen(olddate)!=sizeof("Jan")-1)
  119.                 goto badformat;
  120.             month = findmonth(olddate);
  121.             year = 0;
  122.             day = 0;
  123.         }
  124.     }
  125.     return year*10000 + month*100 + day;
  126.  
  127. badformat:
  128.     return -1;    /* don't understand it */
  129. }
  130.  
  131. char *
  132. convertidate(cnvdate)
  133. int cnvdate;
  134. {
  135.     int year, month, day;
  136.     char tbuf[100];
  137.  
  138.     year = cnvdate/10000;
  139.     month = cnvdate/100 - year*100;
  140.     day = cnvdate - month*100 - year*10000;
  141.  
  142.     tbuf[0] = 0;
  143.     if (month)
  144.         sprintf(tbuf,"%s",montab[month].lname);
  145.     if (day) {
  146.         if (tbuf[0])
  147.             strcat(tbuf," ");
  148.         sprintf(tbuf+strlen(tbuf),"%d",day);
  149.     }
  150.     if (year) {
  151.         if (tbuf[0])
  152.             strcat(tbuf,", ");
  153.         sprintf(tbuf+strlen(tbuf),"%d",year);
  154.     }
  155.     return strsav(tbuf);
  156. }
  157.  
  158. char *            /* allocated string in format "January 23, 1876" */
  159. convertsdate(olddate)
  160. char *olddate;        /* in format "23-Jan-1876" */
  161. {
  162.     int cnvdate;
  163.  
  164.     cnvdate = sdatetoint(olddate);
  165.     if (cnvdate<=0)
  166.         return strsav(olddate);
  167.             /* can't figure it out, use what's there */
  168.     return convertidate(cnvdate);
  169. }
  170.  
  171. /* end */
  172.