Where is strftime(.....)?
Ingo Feulner
alf at xenon.stgt.sub.org
Mon Aug 13 01:03:47 AEST 1990
In article <1990Aug11.230626.2706 at zoo.toronto.edu>, henry at zoo.toronto.edu (Henry Spencer) writes:
> In article <2048 at ux.acs.umn.edu> edh at ux.acs.umn.edu (Eric D. Hendrickson) writes:
> >In the standard library in K&R (ANSI) on page 256 there is a very nice
> >looking procedure for formatting the current time into whatever form you
> >like. It doesn't seem to be present on this system, or on a local Sparc.
>
> Many systems are not yet ANSI-compliant.
Hope that will change in the near future...
>
> I don't know of any redistributable strftime() implementation.
Here is one. It comes with a public domain C compiler named Sozobon C (for
the Amiga), so I think I can post this.
-Enjoy, Ingo.
-------------------------snip----------------------snap--------------
/*
* STRFTIME.C
*/
#include <time.h>
#include <string.h>
#include <stddef.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((size_t)i);
}
-------------------------------snip--------------snap----------------
--
Ingo Feulner - alf at xenon.stgt.sub.org
Wolfacher Weg 22 - 7030 Boeblingen - (+49) 7031 272691 - West Germany
Love your enemies. It'll make 'em crazy.
AMIGA - the only way to go!
More information about the Comp.lang.c
mailing list