home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / program / lynxlib / dateconv.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-23  |  6.8 KB  |  245 lines

  1. /* This source file is part of the LynxLib miscellaneous library by
  2. Robert Fischer, and is Copyright 1990 by Robert Fischer.  It costs no
  3. money, and you may not make money off of it, but you may redistribute
  4. it.  It comes with ABSOLUTELY NO WARRANTY.  See the file LYNXLIB.DOC
  5. for more details.
  6. To contact the author:
  7.     Robert Fischer \\80 Killdeer Rd \\Hamden, CT   06517   USA
  8.     (203) 288-9599     fischer-robert@cs.yale.edu                 */
  9.  
  10. /* Routines for converting dates and times to strings */
  11. /* By Robert Fischer March 17, 1990 */
  12. #include <stddef.h>
  13. #include <ctype.h>
  14. #include <tos.h>
  15. #include <string.h>
  16.  
  17. #define isdelim(c)    (_ctype[(c)+1]&(_S|_P|_X))
  18. #define isnonnum(c)    (_ctype[(c)+1]&(_S|_P|_X|_A))
  19. /* ------------------------------------------------------- */
  20. dtoa(date, divide, order, s)    /* Date to string */
  21. date_rec date;        /* System-format date */
  22. char divide;        /* Divider to use ('/', '-', or '\0' if none to be used) */
  23. int order;            /* Bit string specifying what should be in each field */
  24.     /* It's twelve bits long, and bits are in groups of four.  Each group */
  25.     /* holds either a 0 (DAY), 1 (MONTH) or 2 (YEAR).  So 0x102 means MMDDYY. */
  26. char *s;            /* String to put output in */
  27. {
  28. typedef char num_str[6];
  29. num_str date_part[3];
  30. char dash[2];
  31.     dash[0] = divide;
  32.     dash[1] = NIL;
  33.  
  34.     fitoa(date.day, 2, 10, date_part[0]);
  35.     fitoa(date.month, 2, 10, date_part[1]);
  36.     fitoa( (date.year + 80) % 100, 2, 10, date_part[2]);
  37.  
  38.     strcpy(s, date_part[(order >> 8) & 3]);
  39.     strcat(strcat(strcat(strcat(s, dash), date_part[(order >> 4) & 3]),
  40.         dash), date_part[order & 3]);
  41. }
  42. /* ------------------------------------------------------- */
  43. ttoa(time, divide, showsec, s)    /* Time to string */
  44. time_rec time;        /* Time to convert */
  45. char divide;        /* Divider character, NIL if none */
  46. BOOLEAN showsec;    /* Convert seconds too? */
  47. char *s;            /* Output string */
  48. {
  49. char minute[4], second[4];
  50. char colon[2];
  51.     colon[0] = divide;
  52.     colon[1] = NIL;
  53.  
  54.     /* Make up the number strings */
  55.     fitoa(time.second << 1, 2, 10, second);
  56.     fitoa(time.minute, 2, 10, minute);
  57.     fitoa(time.hour, 2, 10, s);
  58.  
  59.     /* Concatenate together correctly */
  60.     strcat(strcat(s, colon), minute);
  61.     if (showsec) strcat(strcat(s, colon), second);
  62. }
  63. /* ------------------------------------------------------- */
  64. unsigned char *skip_nonnum(s)
  65. register unsigned char *s;
  66. {
  67.     while (isnonnum(*s)) s++;
  68.     return s;
  69. }
  70. /* ------------------------------------------------------- */
  71. unsigned char *skip_delim(s)
  72. register unsigned char *s;
  73. {
  74.     while (isdelim(*s)) s++;
  75.     return s;
  76. }
  77. /* ------------------------------------------------------- */
  78. unsigned char *getnum(s0, maxlen, val)
  79. register unsigned char *s0;
  80. register int maxlen;
  81. int *val;
  82. {
  83. register int i;
  84. register int d=0;
  85. register unsigned char *s;
  86.  
  87.     for (i=0, s=s0; i<maxlen && isdigit(*s); i++, s++)
  88.         d = 10*d + (*s - '0');
  89.  
  90.     if (i==0) {
  91.         *val = -1;
  92.         return s;
  93.     }
  94.  
  95.     *val = d;
  96.     return s;
  97. }
  98. /* ------------------------------------------------------- */
  99. BOOLEAN atot(t, s)
  100. /* Returns FALSE if no date found, or on error */
  101. time_rec *t;
  102. register unsigned char *s;
  103. {
  104. unsigned char ampm;
  105. int hour, minute, second;
  106.  
  107.     /* Check for empty response */
  108.     if (*s==0) return FALSE;    /* use old values */
  109.  
  110.     /* Parse string */
  111.     s = skip_nonnum(s);        /* skip nonnumerics */
  112.     s = getnum(s, 2, &hour);
  113.     s = skip_nonnum(s);        /* skip nonnumerics */
  114.     s = getnum(s, 2, &minute);
  115.     s = skip_delim(s);        /* skip delimiters */
  116.     if (isdigit(*s)) s = getnum(s, 2, &second);
  117.     else second = 0;
  118.     s = skip_delim(s);        /* skip delimiters */
  119.     ampm = *s;
  120.     s = skip_nonnum(s);        /* skip nonnumerics */
  121.  
  122.     /* Check for too many fields */
  123.     if (*s!=0) return FALSE;
  124.  
  125.     /* Check for validity */
  126.     if (hour < 0 || hour > 23 ||
  127.         minute < 0 || minute > 59 ||
  128.         second < 0 || second > 59)
  129.         return FALSE;
  130.  
  131.     /* Handle am/pm indicator */
  132.     if      (ampm == 'a' || ampm == 'A' ) {
  133.         if (hour < 1 || hour > 12) return FALSE;
  134.         if (hour == 12) hour -= 12;
  135.     }
  136.     else if (ampm == 'p' || ampm == 'P' ) {
  137.         if (hour < 1 || hour > 12) return FALSE;
  138.         if (hour <  12) hour += 12;
  139.     }
  140.     else if (ampm != NIL) return FALSE;
  141.  
  142.     /* Use the new data */
  143.     t->hour = hour;
  144.     t->minute = minute;
  145.     t->second = second>>1;
  146.  
  147.     return TRUE;
  148. }    
  149. /* ------------------------------------------------------- */
  150. BOOLEAN atod(d, order, s)
  151. date_rec *d;
  152. int order;            /* Order expected, as with dtoa */
  153. register unsigned char *s;
  154. {
  155. int date_part[3];        /* date_part[0] = day, date_part[2] = year */
  156.  
  157.     /* Check for empty response */
  158.     if (*s==0) return FALSE;    /* use old values */
  159.  
  160.     /* Parse string */
  161.     s = skip_nonnum(s);        /* skip nonnumerics */
  162.     s = getnum(s, 2, &date_part[(order >> 8) & 3]);
  163.     s = skip_nonnum(s);        /* skip nonnumerics */
  164.     s = getnum(s, 2, &date_part[(order >> 4) & 3]);
  165.     s = skip_nonnum(s);        /* skip nonnumerics */
  166.     s = getnum(s, 2, &date_part[order & 3]);
  167.     s = skip_nonnum(s);        /* skip nonnumerics */
  168.  
  169.     /* Check for too many fields */
  170.     if (*s!=0) return FALSE;
  171.  
  172.     /* Check for validity */
  173.     if (date_part[1] < 1 || date_part[1] > 12        /* month */
  174.         || date_part[0] < 1 || date_part[0] > 31    /* day */
  175.         || date_part[2] < 80 || date_part[2] > 99)    /* year */
  176.         return FALSE;
  177.  
  178.     /* Adjust year to offset from 1980 */
  179.     date_part[2] -= 80;
  180.  
  181.     /* Use the new values */
  182.     d->day = date_part[0];
  183.     d->month = date_part[1];
  184.     d->year = date_part[2];
  185.  
  186.     return TRUE;
  187. }
  188. /* ------------------------------------------------------- */
  189. BOOLEAN atotd(td, order, s)
  190. /* Converts a date&time of the form "DATE TIME" to GEMDOS_TD_REC */
  191. GEMDOS_TD_REC *td;
  192. int order;            /* Order for date */
  193. char *s;
  194. {
  195. char *c;
  196. BOOLEAN ret;
  197.     c = index(s, ' ');
  198.     if (c == NULL) return FALSE;
  199.     *c = NIL;
  200.     ret = atod(&td->td.date, order, s);
  201.     if (ret) ret = atot(&td->td.time, c+1);
  202.     *c = ' ';
  203.     return ret;
  204. }
  205. /* ------------------------------------------------------- */
  206. BOOLEAN cmp_date(adate, atime, bdate, btime)
  207. /* Returns TRUE if the adtm > bdtm */
  208. date_rec adate;
  209. time_rec atime;
  210. date_rec bdate;
  211. time_rec btime;
  212. {
  213. BIOS_DT_REC a, b;
  214.     a.dt.date = adate;
  215.     a.dt.time = atime;
  216.     b.dt.date = bdate;
  217.     b.dt.time = btime;
  218.     return (a.l > b.l);
  219. }
  220. /* ------------------------------------------------------- */
  221. #if 0
  222. #include <dateconv.h>
  223. main()
  224. {
  225. char d[80], t[80];
  226. BOOLEAN bd, bt;
  227. date_rec date;
  228. time_rec time;
  229.     do {
  230.         printf("Enter your date: ");
  231.         gets(d);
  232.         printf("Enter your time: ");
  233.         gets(t);
  234.         bd = atod(&date, MMDDYY, d);
  235.         bt = atot(&time, t);
  236.         printf("bd: %d, bt: %d\n", bd, bt);
  237.     } while (t[0] != 'x');
  238. }
  239. #endif
  240. /* ------------------------------------------------------- */
  241. /* ------------------------------------------------------- */
  242. /* ------------------------------------------------------- */
  243. /* ------------------------------------------------------- */
  244. /* ------------------------------------------------------- */
  245.