/* * getdate() %y checker * Author: Andrew Josey, (ajosey@xopen.org) * * Contributions by Walter Mecky * * Determine window for the getdate() function. * * Note that UNIX systems are required only to be able to * represent dates up until between Jan 1 1970 and Jan 1 2038, * some systems may be able to handle a wider range but its * not required. See comment to E_NOLINE_IN_INPUT. * * Based on the idea seen for a test for strptime on comp.unix.aix * reworked to allow for the Jan 1 1970 - Jan 1 2038 limits on * some systems. * * $Log: getdate.c,v $ * Revision 1.6 1999/02/05 09:55:59 mew * It's not necessary now to change #defines for systems with smaller * intervalls. It's all done automatically. * * * Revision 1.4 1999/02/02 10:45:05 mew * Use stdio instead of system calls for enhanced portability * * Revision 1.2 1998/01/23 11:28:13 mew * No core dumps any more. Error message instead. * Does not need an extra mask file. */ #ifndef lint static char *_version = "@(#)getdate_win.c 1.1 - 98/06/08"; #endif #include #include #include extern int getdate_err; #include #ifdef sun #include #else #define TM_YEAR_BASE 1900 #endif #define TOG_START 0 #define TOG_END 100 /* * Error code of my getdate(3C) if it cannot handle the year. * This may happen for years between 1900 and 1969. */ #define E_NOLINE_IN_INPUT 7 static char * tmpf; static void cleanup(void) { remove(tmpf); } main() { int i; struct tm *tm_ptr; char buf[9]; int years[100]; char * cp; FILE * fp; char datemsk[] = "%d/%m/%y\n"; char envstr[] = "DATEMSK="; if ((tmpf = tmpnam(NULL)) == NULL) { perror("tmpfname"); fprintf(stderr, "Error: cannot create temp filename\n"); return 1; } if ((fp = fopen(tmpf, "w")) == NULL) { perror("fopen"); return 2; } atexit(cleanup); if (fputs(datemsk,fp ) == EOF) { perror("fputs"); return 3; } fclose(fp); if ((cp = malloc(sizeof envstr + strlen(tmpf) + 1)) == NULL) { perror("malloc"); return 1; } strcpy(cp, envstr); strcat(cp, tmpf); putenv(cp); /* * run over the whole intervall and notice the year or the errorcode. * the errorcode is stored with a minus sign. */ for (i = TOG_START ; i < TOG_END; i++) { sprintf(buf, "01/01/%02d", i); if ((tm_ptr = getdate (buf)) == NULL) { years[i] = -E_NOLINE_IN_INPUT; /* exclude code block for the moment */ /* if (getdate_err == E_NOLINE_IN_INPUT) years[i] = -E_NOLINE_IN_INPUT; else { fprintf(stderr, "getdate: error %d\n", getdate_err); return 1; } */ } else { years[i] = tm_ptr->tm_year + TM_YEAR_BASE; } #ifdef DEBUG printf("years[%d]=%d\n", i, years[i]); #endif } /* * Here comes the output. * 2 windows are printet: a-b c-d * a is the year for the two digit year 00. * b is the last year in the same century as a. * c is one year after b. * d is the last year in the same century as c. * At least you should see this: 2000-2038 1969-1999. * It will be a bug in getdate(3C) if one window is smaller. */ printf("getdate(3C) window: %04d-", years[0]); for (i = 1; i < TOG_END; i++) { if (years[i] == -E_NOLINE_IN_INPUT) { #ifdef DEBUG printf("\noutp1 aborted. years[%d]=%d\n", i, years[i]); #endif break; } else { /* * Note: the condition here is true if 2 years are in * different centuries: 19 -> 20 or 20 -> 19. */ if (years[i-1] / 100 != years[i] / 100) printf("%04d %04d-", years[i-1], years[i]); } } printf("%04d ", years[i-1]); if (i < TOG_END) { while (i < TOG_END && years[++i] < 0) /*empty*/ ; if (i < TOG_END) { printf("%04d-", years[i++]); for (; i < TOG_END; i++) { if (years[i-1] / 100 != years[i] / 100) printf("%04d %04d-", years[i-1], years[i]); } printf("%04d", years[TOG_END-1]); } } printf("\n"); return 0; }