home *** CD-ROM | disk | FTP | other *** search
-
- /*
- * STRFTIME.C
- */
-
- #include <time.h>
- #include <string.h>
-
- static char *AbMonth[12] = { "Jan","Feb","Mar","Apr","May","Jun","Jul",
- "Aug","Sep","Oct","Nov","Dec"
- };
-
- static char *FuMonth[12] = { "January", "February", "March", "April", "May",
- "June", "July", "August", "September", "October",
- "November", "December"
- };
-
- static char *AbDow[12] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
-
- static char *FuDow[12] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
- "Friday", "Saturday"
- };
-
-
- size_t
- strftime(buf, max, fmt, tm)
- char *buf;
- size_t max;
- const char *fmt;
- const struct tm *tm;
- {
- short i = 0;
-
- buf[i] = 0;
-
- while (*fmt) {
- char *ptr;
-
- i += strlen(buf + i);
-
- if (*fmt != '%') {
- buf[i++] = *fmt++;
- continue;
- }
- ptr = buf + i;
- fmt += 2;
- switch(fmt[-1]) {
- case '%':
- strcpy(ptr, "%");
- break;
- case 'a': /* abbreviated name for dow */
- strcpy(ptr, AbDow[tm->tm_wday]);
- break;
- case 'A': /* full name for dow */
- strcpy(ptr, FuDow[tm->tm_wday]);
- break;
- case 'b': /* abbreviated name for month */
- strcpy(ptr, AbMonth[tm->tm_mon]);
- break;
- case 'B': /* full name for month */
- strcpy(ptr, FuMonth[tm->tm_mon]);
- break;
- case 'c': /* default rep for date & time */
- sprintf(ptr, "%s %s %02d %02d:%02d:%02d %d",
- AbDow[tm->tm_wday], AbMonth[tm->tm_mon], tm->tm_mday,
- tm->tm_hour, tm->tm_min, tm->tm_sec, tm->tm_year + 1900
- );
- break;
- case 'd': /* day of month as integer 01-31 */
- sprintf(ptr, "%02d", tm->tm_mday);
- break;
- case 'H': /* hour as integer 00-23 */
- sprintf(ptr, "%02d", tm->tm_hour);
- break;
- case 'I': /* hour as integer 01-12 */
- sprintf(ptr, "%02d", (tm->tm_hour % 12) + 1);
- break;
- case 'j': /* day of year as int 001-366 */
- sprintf(ptr, "%03d", tm->tm_yday);
- break;
- case 'm': /* month as integer 01-12 */
- sprintf(ptr, "%02d", tm->tm_mon);
- break;
- case 'M': /* minute as integer 00-59 */
- sprintf(ptr, "%02d", tm->tm_min);
- break;
- case 'p': /* 'AM' or 'PM' */
- if (tm->tm_hour >= 12) {
- strcpy(ptr, "PM");
- } else {
- strcpy(ptr, "AM");
- }
- break;
- case 'S': /* the second as an int 00-59 */
- sprintf(ptr, "%02d", tm->tm_sec);
- break;
- case 'U': /* week of year as int 00-53, regard sunday as first day in week */
- {
- int bdiw = tm->tm_yday - tm->tm_wday; /* beginning of week */
- if (bdiw < 0)
- bdiw = 0;
- else
- bdiw = bdiw / 7;
- sprintf(ptr, "%02d", bdiw);
- }
- break;
- case 'w': /* day of week as int 0-6, sunday == 0 */
- sprintf(ptr, "%d", tm->tm_wday);
- break;
- case 'W': /* day of week as int 0-6, monday == 0 */
- {
- int dowmon = tm->tm_wday - 1;
- if (dowmon < 0)
- dowmon += 7;
- sprintf(ptr, "%d", dowmon);
- }
- break;
- case 'x': /* the locale's default rep for date */
- sprintf(ptr, "%s %s %02d",
- AbDow[tm->tm_wday], AbMonth[tm->tm_mon], tm->tm_mday
- );
- break;
- case 'X': /* the locale's default rep for time */
- sprintf(ptr, "%02d:%02d:%02d",
- tm->tm_hour, tm->tm_min, tm->tm_sec
- );
- break;
- case 'y': /* year within the century 00-99 */
- sprintf(ptr, "%02d", tm->tm_year % 100);
- break;
- case 'Y': /* the full year, including century */
- sprintf(ptr, "%04d", tm->tm_year + 1900);
- break;
- case 'Z': /* the name of the time zone or "" */
- strcpy(ptr, "");
- break;
- default:
- strcpy(ptr, "%?");
- break;
- }
- }
- i += strlen(buf + i);
- return(i);
- }
-
-