int mktimes(char *buf, int len, char *fmt, char *tzname) { time_t tim; struct tm *timtm; int warn = 0; // TODO: make this much more general. bzero(buf, sizeof(buf)); settz(tzname); tim = time(NULL); timtm = localtime(&tim); if (timtm == NULL) { perror("localtime"); exit(1); } if (timtm->tm_hour == 17 && timtm->tm_min > 45) { warn = 1; } else if (timtm->tm_hour == 18 && timtm->tm_min < 15) { warn = 1; } if (!strftime(buf, len-1, fmt, timtm)) { fprintf(stderr, "strftime == 0\n"); exit(1); } return warn; }
char *mktimes(char *fmt, char *tzname) { char buf[129]; time_t tim; struct tm *timtm; memset(buf, 0, sizeof(buf)); settz(tzname); tim = time(NULL); timtm = localtime(&tim); if (timtm == NULL) { perror("localtime"); exit(1); } if (!strftime(buf, sizeof(buf)-1, fmt, timtm)) { fprintf(stderr, "strftime == 0\n"); exit(1); } return smprintf("%s", buf); }
/* * Преобразование строки даты из Unix-формата в RFC 1036. * Wdy, DD Mon YY HH:MM:SS *HHMM * 0 0 0 0 * Wdy Mon DD HH:MM:SS YYYY */ static char *ctim2rfc (register char *ct, int tz) { if (ct[8] == ' ') ct[8] = '0'; strcpy (buf, "Wdy, DD Mon YY HH:MM:SS *HHMM"); strncpy (buf, ct, 3); /* Wdy */ strncpy (buf+5, ct+8, 2); /* DD */ strncpy (buf+8, ct+4, 3); /* Mon */ strncpy (buf+15, ct+11, 8); /* HH:MM:SS */ if (ct[20] == '1' || ct[20] == '2') strncpy (buf+12, ct+22, 2); /* YY */ else { char *czone; /* Wdy Mon DD HH:MM:SS DST YYYY */ strncpy (buf+12, ct+26, 2); /* YY */ czone = ct + 20; tz = getkeyword (&czone, zonetab, KEYSIZE (zonetab)); if (tz == ERROR) tz = 0; } settz (buf, tz); return (buf); }
/* * Преобразование даты в стандарт RFC 1036. */ char *rfcdate (char *ctim) { #define skip(p) while (*(p)==' ' || *(p)=='\t') ++(p) int wday, day, mon, year, h, m, s, tz; skip (ctim); if (ctim[3]==' ' && ctim[7]==' ' && ctim[10]==' ' && ctim[13]==':' && ctim[16]==':' && ctim[19]==' ' && (ctim[20]=='1' || ctim[20]=='2' || ctim[24]=='1' || ctim[24]=='2')) return (ctim2rfc (ctim, 0)); /* [Wdy,] DD Mon YY HH:MM[:SS] TIMEZONE */ if ((*ctim>='A' && *ctim<='Z') || (*ctim>='a' && *ctim<='z')) { wday = getkeyword (&ctim, daytab, KEYSIZE (daytab)); if (wday == ERROR) return (0); if (*ctim == ',') ++ctim; skip (ctim); } else wday = -1; day = getint (&ctim); if (day < 0) return (0); skip (ctim); mon = getkeyword (&ctim, monthtab, KEYSIZE (monthtab)); if (mon == ERROR) return (0); skip (ctim); year = getint (&ctim); if (year < 0) return (0); if (year < 70) year += 2000; else if (year < 100) year += 1900; if (year <= 1981) /* ancient date */ return (0); skip (ctim); if (*ctim>='0' && *ctim<='9') { h = getint (&ctim); if (h<0 || *ctim!=':') return (0); ++ctim; skip (ctim); m = getint (&ctim); if (m < 0) return (0); if (*ctim == ':') { ++ctim; skip (ctim); s = getint (&ctim); if (s < 0) return (0); } else s = 0; skip (ctim); } else { h = 12; m = s = 0; } if (*ctim=='+' || *ctim=='-') { int sign = (*ctim == '+'); ++ctim; skip (ctim); tz = getint (&ctim); if (tz < 0) return (0); tz = tz / 100 * 60 + tz % 100; if (sign) tz = -tz; } else if (*ctim) { tz = getkeyword (&ctim, zonetab, KEYSIZE (zonetab)); if (tz == ERROR) return (0); } else tz = 0; if (wday < 0) wday = weekday (day, mon, year); sprintf (buf, "%s, %02d %s %02d %02d:%02d:%02d *HHMM", dayname [wday], day, monthname [mon-1], year % 100, h, m, s); settz (buf, tz); return (buf); }
size_t my_strftime( char *s, size_t maxsize, const char *format, struct tm *timeptr) { #ifdef HAVE_STRFTIME return strftime(s, maxsize, format, timeptr); #else char *endp = s + maxsize; char *start = s; char tbuf[100]; int i; /* * various tables, useful in North America */ static const char *days_a[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", }; static const char *days_l[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", }; static const char *months_a[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", }; static const char *months_l[] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December", }; static const char *ampm[] = { "AM", "PM", }; if (s == NULL || format == NULL || timeptr == NULL || maxsize == 0) return 0; if (strchr(format, '%') == NULL && strlen(format) + 1 >= maxsize) return 0; #ifdef HAVE_TZSET tzset(); #else # ifdef HAVE_SETTZ settz(); # endif /* HAVE_SETTZ */ #endif /* HAVE_TZSET */ for (; *format && s < endp - 1; format++) { tbuf[0] = '\0'; if (*format != '%') { *s++ = *format; continue; } switch (*++format) { case '\0': *s++ = '%'; goto out; case '%': *s++ = '%'; continue; case 'a': /* abbreviated weekday name */ strcpy(tbuf, days_a[timeptr->tm_wday]); break; case 'A': /* full weekday name */ strcpy(tbuf, days_l[timeptr->tm_wday]); break; # ifdef SYSV_EXT case 'h': /* abbreviated month name */ # endif /* SYSV_EXT */ case 'b': /* abbreviated month name */ strcpy(tbuf, months_a[timeptr->tm_mon]); break; case 'B': /* full month name */ strcpy(tbuf, months_l[timeptr->tm_mon]); break; case 'c': /* appropriate date and time representation */ sprintf(tbuf, "%s %s %2d %02d:%02d:%02d %d", days_a[timeptr->tm_wday], months_a[timeptr->tm_mon], timeptr->tm_mday, timeptr->tm_hour, timeptr->tm_min, timeptr->tm_sec, timeptr->tm_year + 1900); break; case 'd': /* day of the month, 01 - 31 */ sprintf(tbuf, "%02d", timeptr->tm_mday); break; case 'H': /* hour, 24-hour clock, 00 - 23 */ sprintf(tbuf, "%02d", timeptr->tm_hour); break; case 'I': /* hour, 12-hour clock, 01 - 12 */ i = timeptr->tm_hour; if (i == 0) i = 12; else if (i > 12) i -= 12; sprintf(tbuf, "%02d", i); break; case 'j': /* day of the year, 001 - 366 */ sprintf(tbuf, "%03d", timeptr->tm_yday + 1); break; case 'm': /* month, 01 - 12 */ sprintf(tbuf, "%02d", timeptr->tm_mon + 1); break; case 'M': /* minute, 00 - 59 */ sprintf(tbuf, "%02d", timeptr->tm_min); break; case 'p': /* am or pm based on 12-hour clock */ strcpy(tbuf, ampm[((timeptr->tm_hour < 12) ? 0 : 1)]); break; case 'S': /* second, 00 - 61 */ sprintf(tbuf, "%02d", timeptr->tm_sec); break; case 'w': /* weekday, Sunday == 0, 0 - 6 */ sprintf(tbuf, "%d", timeptr->tm_wday); break; case 'x': /* appropriate date representation */ sprintf(tbuf, "%s %s %2d %d", days_a[timeptr->tm_wday], months_a[timeptr->tm_mon], timeptr->tm_mday, timeptr->tm_year + 1900); break; case 'X': /* appropriate time representation */ sprintf(tbuf, "%02d:%02d:%02d", timeptr->tm_hour, timeptr->tm_min, timeptr->tm_sec); break; case 'y': /* year without a century, 00 - 99 */ i = timeptr->tm_year % 100; sprintf(tbuf, "%d", i); break; case 'Y': /* year with century */ sprintf(tbuf, "%d", timeptr->tm_year + 1900); break; # ifdef SYSV_EXT case 'n': /* same as \n */ tbuf[0] = '\n'; tbuf[1] = '\0'; break; case 't': /* same as \t */ tbuf[0] = '\t'; tbuf[1] = '\0'; break; case 'D': /* date as %m/%d/%y */ my_strftime(tbuf, sizeof tbuf, "%m/%d/%y", timeptr); break; case 'e': /* day of month, blank padded */ sprintf(tbuf, "%2d", timeptr->tm_mday); break; case 'r': /* time as %I:%M:%S %p */ my_strftime(tbuf, sizeof tbuf, "%I:%M:%S %p", timeptr); break; case 'R': /* time as %H:%M */ my_strftime(tbuf, sizeof tbuf, "%H:%M", timeptr); break; case 'T': /* time as %H:%M:%S */ my_strftime(tbuf, sizeof tbuf, "%H:%M:%S", timeptr); break; # endif /* SYSV_EXT */ default: tbuf[0] = '%'; tbuf[1] = *format; tbuf[2] = '\0'; break; } if ((i = strlen(tbuf))) { if (s + i < endp - 1) { strcpy(s, tbuf); s += i; } else return 0; } } out: if (s < endp && *format == '\0') { *s = '\0'; return (size_t) (s - start); } else return 0; #endif /* HAVE_STRFTIME */ }