⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 strftime.c

📁 早期freebsd实现
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * strftime.c * * Public-domain relatively quick-and-dirty implementation of * ANSI library routine for System V Unix systems. * * It's written in old-style C for maximal portability. * However, since I'm used to prototypes, I've included them too. * * If you want stuff in the System V ascftime routine, add the SYSV_EXT define. * For extensions from SunOS, add SUNOS_EXT. * For stuff needed to implement the P1003.2 date command, add POSIX2_DATE. * For complete POSIX semantics, add POSIX_SEMANTICS. * * The code for %c, %x, and %X is my best guess as to what's "appropriate". * This version ignores LOCALE information. * It also doesn't worry about multi-byte characters. * So there. * * This file is also shipped with GAWK (GNU Awk), gawk specific bits of * code are included if GAWK is defined. * * Arnold Robbins * January, February, March, 1991 * Updated March, April 1992 * Updated April, 1993 * * Fixes from ado@elsie.nci.nih.gov * February 1991, May 1992 * Fixes from Tor Lillqvist tor@tik.vtt.fi * May, 1993 */#ifndef GAWK#include <stdio.h>#include <ctype.h>#include <string.h>#include <time.h>#include <sys/types.h>#endif/* defaults: season to taste */#define SYSV_EXT	1	/* stuff in System V ascftime routine */#define SUNOS_EXT	1	/* stuff in SunOS strftime routine */#define POSIX2_DATE	1	/* stuff in Posix 1003.2 date command */#define VMS_EXT		1	/* include %v for VMS date format */#ifndef GAWK#define POSIX_SEMANTICS	1	/* call tzset() if TZ changes */#endif#if defined(POSIX2_DATE)#if ! defined(SYSV_EXT)#define SYSV_EXT	1#endif#if ! defined(SUNOS_EXT)#define SUNOS_EXT	1#endif#endif#if defined(POSIX2_DATE)#define adddecl(stuff)	stuff#else#define adddecl(stuff)#endif#undef strchr	/* avoid AIX weirdness */#ifndef __STDC__#define const	/**/extern void *malloc();extern void *realloc();extern void tzset();extern char *strchr();extern char *getenv();static int weeknumber();adddecl(static int iso8601wknum();)#elseextern void *malloc(unsigned count);extern void *realloc(void *ptr, unsigned count);extern void tzset(void);extern char *strchr(const char *str, int ch);extern char *getenv(const char *v);static int weeknumber(const struct tm *timeptr, int firstweekday);adddecl(static int iso8601wknum(const struct tm *timeptr);)#endif#ifdef __GNUC__#define inline	__inline__#else#define inline	/**/#endif#define range(low, item, hi)	max(low, min(item, hi))#if !defined(MSDOS) && !defined(TZNAME_MISSING)extern char *tzname[2];extern int daylight;#endif/* min --- return minimum of two numbers */#ifndef __STDC__static inline intmin(a, b)int a, b;#elsestatic inline intmin(int a, int b)#endif{	return (a < b ? a : b);}/* max --- return maximum of two numbers */#ifndef __STDC__static inline intmax(a, b)int a, b;#elsestatic inline intmax(int a, int b)#endif{	return (a > b ? a : b);}/* strftime --- produce formatted time */#ifndef __STDC__size_tstrftime(s, maxsize, format, timeptr)char *s;size_t maxsize;const char *format;const struct tm *timeptr;#elsesize_tstrftime(char *s, size_t maxsize, const char *format, const struct tm *timeptr)#endif{	char *endp = s + maxsize;	char *start = s;	char tbuf[100];	int i;	static short first = 1;#ifdef POSIX_SEMANTICS	static char *savetz = NULL;	static int savetzlen = 0;	char *tz;#endif /* POSIX_SEMANTICS */	/* various tables, useful in North America */	static char *days_a[] = {		"Sun", "Mon", "Tue", "Wed",		"Thu", "Fri", "Sat",	};	static char *days_l[] = {		"Sunday", "Monday", "Tuesday", "Wednesday",		"Thursday", "Friday", "Saturday",	};	static char *months_a[] = {		"Jan", "Feb", "Mar", "Apr", "May", "Jun",		"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",	};	static char *months_l[] = {		"January", "February", "March", "April",		"May", "June", "July", "August", "September",		"October", "November", "December",	};	static 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;#ifndef POSIX_SEMANTICS	if (first) {		tzset();		first = 0;	}#else	/* POSIX_SEMANTICS */	tz = getenv("TZ");	if (first) {		if (tz != NULL) {			int tzlen = strlen(tz);			savetz = (char *) malloc(tzlen + 1);			if (savetz != NULL) {				savetzlen = tzlen + 1;				strcpy(savetz, tz);			}		}		tzset();		first = 0;	}	/* if we have a saved TZ, and it is different, recapture and reset */	if (tz && savetz && (tz[0] != savetz[0] || strcmp(tz, savetz) != 0)) {		i = strlen(tz) + 1;		if (i > savetzlen) {			savetz = (char *) realloc(savetz, i);			if (savetz) {				savetzlen = i;				strcpy(savetz, tz);			}		} else			strcpy(savetz, tz);		tzset();	}#endif	/* POSIX_SEMANTICS */	for (; *format && s < endp - 1; format++) {		tbuf[0] = '\0';		if (*format != '%') {			*s++ = *format;			continue;		}	again:		switch (*++format) {		case '\0':			*s++ = '%';			goto out;		case '%':			*s++ = '%';			continue;		case 'a':	/* abbreviated weekday name */			if (timeptr->tm_wday < 0 || timeptr->tm_wday > 6)				strcpy(tbuf, "?");			else				strcpy(tbuf, days_a[timeptr->tm_wday]);			break;		case 'A':	/* full weekday name */			if (timeptr->tm_wday < 0 || timeptr->tm_wday > 6)				strcpy(tbuf, "?");			else				strcpy(tbuf, days_l[timeptr->tm_wday]);			break;#ifdef SYSV_EXT		case 'h':	/* abbreviated month name */#endif		case 'b':	/* abbreviated month name */			if (timeptr->tm_mon < 0 || timeptr->tm_mon > 11)				strcpy(tbuf, "?");			else				strcpy(tbuf, months_a[timeptr->tm_mon]);			break;		case 'B':	/* full month name */			if (timeptr->tm_mon < 0 || timeptr->tm_mon > 11)				strcpy(tbuf, "?");			else				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[range(0, timeptr->tm_wday, 6)],				months_a[range(0, timeptr->tm_mon, 11)],				range(1, timeptr->tm_mday, 31),				range(0, timeptr->tm_hour, 23),				range(0, timeptr->tm_min, 59),				range(0, timeptr->tm_sec, 61),				timeptr->tm_year + 1900);			break;		case 'd':	/* day of the month, 01 - 31 */			i = range(1, timeptr->tm_mday, 31);			sprintf(tbuf, "%02d", i);			break;		case 'H':	/* hour, 24-hour clock, 00 - 23 */			i = range(0, timeptr->tm_hour, 23);			sprintf(tbuf, "%02d", i);			break;		case 'I':	/* hour, 12-hour clock, 01 - 12 */			i = range(0, timeptr->tm_hour, 23);			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 */			i = range(0, timeptr->tm_mon, 11);			sprintf(tbuf, "%02d", i + 1);			break;		case 'M':	/* minute, 00 - 59 */			i = range(0, timeptr->tm_min, 59);			sprintf(tbuf, "%02d", i);			break;		case 'p':	/* am or pm based on 12-hour clock */			i = range(0, timeptr->tm_hour, 23);			if (i < 12)				strcpy(tbuf, ampm[0]);			else				strcpy(tbuf, ampm[1]);			break;		case 'S':	/* second, 00 - 61 */			i = range(0, timeptr->tm_sec, 61);			sprintf(tbuf, "%02d", i);			break;		case 'U':	/* week of year, Sunday is first day of week */			sprintf(tbuf, "%d", weeknumber(timeptr, 0));			break;		case 'w':	/* weekday, Sunday == 0, 0 - 6 */			i = range(0, timeptr->tm_wday, 6);			sprintf(tbuf, "%d", i);			break;		case 'W':	/* week of year, Monday is first day of week */			sprintf(tbuf, "%d", weeknumber(timeptr, 1));			break;		case 'x':	/* appropriate date representation */			sprintf(tbuf, "%s %s %2d %d",				days_a[range(0, timeptr->tm_wday, 6)],				months_a[range(0, timeptr->tm_mon, 11)],				range(1, timeptr->tm_mday, 31),				timeptr->tm_year + 1900);			break;		case 'X':	/* appropriate time representation */			sprintf(tbuf, "%02d:%02d:%02d",				range(0, timeptr->tm_hour, 23),				range(0, timeptr->tm_min, 59),				range(0, timeptr->tm_sec, 61));			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", 1900 + timeptr->tm_year);			break;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -