home *** CD-ROM | disk | FTP | other *** search
- /* This source file is part of the LynxLib miscellaneous library by
- Robert Fischer, and is Copyright 1990 by Robert Fischer. It costs no
- money, and you may not make money off of it, but you may redistribute
- it. It comes with ABSOLUTELY NO WARRANTY. See the file LYNXLIB.DOC
- for more details.
- To contact the author:
- Robert Fischer \\80 Killdeer Rd \\Hamden, CT 06517 USA
- (203) 288-9599 fischer-robert@cs.yale.edu */
-
- /* Routines for converting dates and times to strings */
- /* By Robert Fischer March 17, 1990 */
- #include <stddef.h>
- #include <ctype.h>
- #include <tos.h>
- #include <string.h>
-
- #define isdelim(c) (_ctype[(c)+1]&(_S|_P|_X))
- #define isnonnum(c) (_ctype[(c)+1]&(_S|_P|_X|_A))
- /* ------------------------------------------------------- */
- dtoa(date, divide, order, s) /* Date to string */
- date_rec date; /* System-format date */
- char divide; /* Divider to use ('/', '-', or '\0' if none to be used) */
- int order; /* Bit string specifying what should be in each field */
- /* It's twelve bits long, and bits are in groups of four. Each group */
- /* holds either a 0 (DAY), 1 (MONTH) or 2 (YEAR). So 0x102 means MMDDYY. */
- char *s; /* String to put output in */
- {
- typedef char num_str[6];
- num_str date_part[3];
- char dash[2];
- dash[0] = divide;
- dash[1] = NIL;
-
- fitoa(date.day, 2, 10, date_part[0]);
- fitoa(date.month, 2, 10, date_part[1]);
- fitoa( (date.year + 80) % 100, 2, 10, date_part[2]);
-
- strcpy(s, date_part[(order >> 8) & 3]);
- strcat(strcat(strcat(strcat(s, dash), date_part[(order >> 4) & 3]),
- dash), date_part[order & 3]);
- }
- /* ------------------------------------------------------- */
- ttoa(time, divide, showsec, s) /* Time to string */
- time_rec time; /* Time to convert */
- char divide; /* Divider character, NIL if none */
- BOOLEAN showsec; /* Convert seconds too? */
- char *s; /* Output string */
- {
- char minute[4], second[4];
- char colon[2];
- colon[0] = divide;
- colon[1] = NIL;
-
- /* Make up the number strings */
- fitoa(time.second << 1, 2, 10, second);
- fitoa(time.minute, 2, 10, minute);
- fitoa(time.hour, 2, 10, s);
-
- /* Concatenate together correctly */
- strcat(strcat(s, colon), minute);
- if (showsec) strcat(strcat(s, colon), second);
- }
- /* ------------------------------------------------------- */
- unsigned char *skip_nonnum(s)
- register unsigned char *s;
- {
- while (isnonnum(*s)) s++;
- return s;
- }
- /* ------------------------------------------------------- */
- unsigned char *skip_delim(s)
- register unsigned char *s;
- {
- while (isdelim(*s)) s++;
- return s;
- }
- /* ------------------------------------------------------- */
- unsigned char *getnum(s0, maxlen, val)
- register unsigned char *s0;
- register int maxlen;
- int *val;
- {
- register int i;
- register int d=0;
- register unsigned char *s;
-
- for (i=0, s=s0; i<maxlen && isdigit(*s); i++, s++)
- d = 10*d + (*s - '0');
-
- if (i==0) {
- *val = -1;
- return s;
- }
-
- *val = d;
- return s;
- }
- /* ------------------------------------------------------- */
- BOOLEAN atot(t, s)
- /* Returns FALSE if no date found, or on error */
- time_rec *t;
- register unsigned char *s;
- {
- unsigned char ampm;
- int hour, minute, second;
-
- /* Check for empty response */
- if (*s==0) return FALSE; /* use old values */
-
- /* Parse string */
- s = skip_nonnum(s); /* skip nonnumerics */
- s = getnum(s, 2, &hour);
- s = skip_nonnum(s); /* skip nonnumerics */
- s = getnum(s, 2, &minute);
- s = skip_delim(s); /* skip delimiters */
- if (isdigit(*s)) s = getnum(s, 2, &second);
- else second = 0;
- s = skip_delim(s); /* skip delimiters */
- ampm = *s;
- s = skip_nonnum(s); /* skip nonnumerics */
-
- /* Check for too many fields */
- if (*s!=0) return FALSE;
-
- /* Check for validity */
- if (hour < 0 || hour > 23 ||
- minute < 0 || minute > 59 ||
- second < 0 || second > 59)
- return FALSE;
-
- /* Handle am/pm indicator */
- if (ampm == 'a' || ampm == 'A' ) {
- if (hour < 1 || hour > 12) return FALSE;
- if (hour == 12) hour -= 12;
- }
- else if (ampm == 'p' || ampm == 'P' ) {
- if (hour < 1 || hour > 12) return FALSE;
- if (hour < 12) hour += 12;
- }
- else if (ampm != NIL) return FALSE;
-
- /* Use the new data */
- t->hour = hour;
- t->minute = minute;
- t->second = second>>1;
-
- return TRUE;
- }
- /* ------------------------------------------------------- */
- BOOLEAN atod(d, order, s)
- date_rec *d;
- int order; /* Order expected, as with dtoa */
- register unsigned char *s;
- {
- int date_part[3]; /* date_part[0] = day, date_part[2] = year */
-
- /* Check for empty response */
- if (*s==0) return FALSE; /* use old values */
-
- /* Parse string */
- s = skip_nonnum(s); /* skip nonnumerics */
- s = getnum(s, 2, &date_part[(order >> 8) & 3]);
- s = skip_nonnum(s); /* skip nonnumerics */
- s = getnum(s, 2, &date_part[(order >> 4) & 3]);
- s = skip_nonnum(s); /* skip nonnumerics */
- s = getnum(s, 2, &date_part[order & 3]);
- s = skip_nonnum(s); /* skip nonnumerics */
-
- /* Check for too many fields */
- if (*s!=0) return FALSE;
-
- /* Check for validity */
- if (date_part[1] < 1 || date_part[1] > 12 /* month */
- || date_part[0] < 1 || date_part[0] > 31 /* day */
- || date_part[2] < 80 || date_part[2] > 99) /* year */
- return FALSE;
-
- /* Adjust year to offset from 1980 */
- date_part[2] -= 80;
-
- /* Use the new values */
- d->day = date_part[0];
- d->month = date_part[1];
- d->year = date_part[2];
-
- return TRUE;
- }
- /* ------------------------------------------------------- */
- BOOLEAN atotd(td, order, s)
- /* Converts a date&time of the form "DATE TIME" to GEMDOS_TD_REC */
- GEMDOS_TD_REC *td;
- int order; /* Order for date */
- char *s;
- {
- char *c;
- BOOLEAN ret;
- c = index(s, ' ');
- if (c == NULL) return FALSE;
- *c = NIL;
- ret = atod(&td->td.date, order, s);
- if (ret) ret = atot(&td->td.time, c+1);
- *c = ' ';
- return ret;
- }
- /* ------------------------------------------------------- */
- BOOLEAN cmp_date(adate, atime, bdate, btime)
- /* Returns TRUE if the adtm > bdtm */
- date_rec adate;
- time_rec atime;
- date_rec bdate;
- time_rec btime;
- {
- BIOS_DT_REC a, b;
- a.dt.date = adate;
- a.dt.time = atime;
- b.dt.date = bdate;
- b.dt.time = btime;
- return (a.l > b.l);
- }
- /* ------------------------------------------------------- */
- #if 0
- #include <dateconv.h>
- main()
- {
- char d[80], t[80];
- BOOLEAN bd, bt;
- date_rec date;
- time_rec time;
- do {
- printf("Enter your date: ");
- gets(d);
- printf("Enter your time: ");
- gets(t);
- bd = atod(&date, MMDDYY, d);
- bt = atot(&time, t);
- printf("bd: %d, bt: %d\n", bd, bt);
- } while (t[0] != 'x');
- }
- #endif
- /* ------------------------------------------------------- */
- /* ------------------------------------------------------- */
- /* ------------------------------------------------------- */
- /* ------------------------------------------------------- */
- /* ------------------------------------------------------- */
-