📄 strftime.c
字号:
/* strftime.c - strftime file for time *//* Copyright 1991-1999 Wind River Systems, Inc. *//*modification history--------------------01l,05aug99,cno atoi calls need byte for null termination (SPR28245)01k,23jul00,jrp SPR 27606 End of DST miscalculated.01j,08jun99,map fixed 12hr reporting (SPR# 7499)01i,20jan97,dbt modified comment concerning seconds (SPR #4436).01h,04oct96,dbt reworked the fix for SPR #7277.01g,03oct96,dbt use memcpy with 'strlen + 1' instead of 'strlen' (SPR #7277)01f,27jun96,dbt corrected __getDstInfo() (spr 2521) Updated copyright.01g,10feb95,rhp internal doc tweak from ansiTime.c01f,15sep94,rhp fixed TIMEZONE example in comment (related to SPR #3490)01e,17aug93,dvs changed TIME to TIMEO to fix conflicting defines (SPR #2249)01d,05feb93,jdi documentation cleanup for 5.1.01c,20sep92,smb documentation additions01b,26jul92,rrr fixed decl of __weekOfYear to compile on mips.01a,25jul92,smb written.*//* DESCRIPTION INCLUDE FILE: time.h, stdlib.h, string.h, locale.h SEE ALSO: American National Standard X3.159-1989 NOMANUAL*/#include "vxWorks.h"#include "string.h"#include "stdlib.h"#include "locale.h"#include "private/timeP.h"extern TIMELOCALE * __loctime; /* time locale information *//* LOCAL */LOCAL size_t strftime_r (char *, size_t, const char *, const struct tm *, TIMELOCALE *);LOCAL void __generateTime (char *, const struct tm *, TIMELOCALE *, int *, const char *);LOCAL void __getDay (char *, int, int, TIMELOCALE *, int *);LOCAL void __getMonth (char *, int, int, TIMELOCALE *, int *);LOCAL void __getLocale (char *, int, const struct tm *, TIMELOCALE *, int *);LOCAL void __intToStr (char *, int, int);LOCAL int __weekOfYear (int, int, int);/********************************************************************************* strftime - convert broken-down time into a formatted string (ANSI)** This routine formats the broken-down time in <tptr> based on the conversion* specified in the string <format>, and places the result in the string <s>.** The format is a multibyte character sequence, beginning and ending in its* initial state. The <format> string consists of zero or more conversion* specifiers and ordinary multibyte characters. A conversion specifier* consists of a % character followed by a character that determines the* behavior of the conversion. All ordinary multibyte characters (including* the terminating NULL character) are copied unchanged to the array. If* copying takes place between objects that overlap, the behavior is* undefined. No more than <n> characters are placed into the array.** Each conversion specifier is replaced by appropriate characters as * described in the following list. The appropriate characters are determined * by the LC_TIME category of the current locale and by the values contained * in the structure pointed to by <tptr>.** .iP %a* the locale's abbreviated weekday name.* .iP %A* the locale's full weekday name.* .iP %b* the locale's abbreviated month name.* .iP %B* the locale's full month name.* .iP %c* the locale's appropriate date and time representation.* .iP %d* the day of the month as decimal number (01-31).* .iP %H* the hour (24-hour clock) as a decimal number (00-23).* .iP %I* the hour (12-hour clock) as a decimal number (01-12).* .iP %j* the day of the year as decimal number (001-366).* .iP %m* the month as a decimal number (01-12).* .iP %M* the minute as a decimal number (00-59).* .iP %P* the locale's equivalent of the AM/PM * designations associated with a 12-hour clock.* .iP %S* the second as a decimal number (00-59).* .iP %U* the week number of the year (first Sunday* as the first day of week 1) as a decimal number (00-53).* .iP %w* the weekday as a decimal number (0-6), where Sunday is 0.* .iP %W* the week number of the year (the first Monday* as the first day of week 1) as a decimal number (00-53).* .iP %x* the locale's appropriate date representation.* .iP %X* the locale's appropriate time representation.* .iP %y* the year without century as a decimal number (00-99).* .iP %Y* the year with century as a decimal number.* .iP %Z* the time zone name or abbreviation, or by no* characters if no time zone is determinable.* .iP %%* %.* .LP** For any other conversion specifier, the behavior is undefined.** INCLUDE FILES: time.h** RETURNS:* The number of characters in <s>, not including the terminating null* character -- or zero if the number of characters in <s>, including the null* character, is more than <n> (in which case the contents of <s> are* indeterminate).*/size_t strftime ( char * s, /* string array */ size_t n, /* maximum size of array */ const char * format, /* format of output string */ const struct tm * tptr /* broken-down time */ ) { return (strftime_r (s, n, format, tptr, __loctime)); }/********************************************************************************* strftime_r - format time into a string (POSIX)** Re-entrant version of strftime().** RETURNS:*/LOCAL size_t strftime_r ( char * bufHd, /* string array */ size_t bufMaxSz, /* maximum size of array */ const char * fmtHd, /* format of output string */ const struct tm * tmptr, /* broken-down time */ TIMELOCALE * timeInfo /* locale information */ ) { const char * fmt = fmtHd; char * buffer = bufHd; char addOn[MaxBufferSize]; int bufLen = 0; int bufszNow = 0; FOREVER { while ((*fmt != '%') && (bufszNow != bufMaxSz) && (*fmt != EOS)) { bufszNow++; *buffer++ = *fmt++; } if (bufszNow == bufMaxSz) break; if (*fmt++ != EOS) { __generateTime (addOn, tmptr, timeInfo, &bufLen, fmt); if (bufLen >= 0) { if (bufMaxSz > (bufszNow + bufLen)) { memcpy (buffer, addOn, bufLen); bufszNow += bufLen; buffer += bufLen; fmt++; } /* endif */ else { memcpy (buffer, addOn, bufMaxSz - bufszNow); buffer += (bufMaxSz - bufszNow); bufszNow = bufMaxSz; break; } } else { /* process format strings */ *(addOn + abs (bufLen)) = EOS; /* This is recursive - but should recurse ONCE only */ bufLen = (int) strftime_r (buffer, bufMaxSz - bufszNow, addOn, tmptr, timeInfo); buffer += bufLen; bufszNow += bufLen; fmt++; } /* endif */ } else break; } /* end forever */ *buffer = EOS; return (bufszNow); }/********************************************************************************* __generateTime - generate a string representing the format indicator.** Internal routine** RETURNS:* NOMANUAL*/LOCAL void __generateTime ( char * addOn, /* string buffer */ const struct tm * tmptr, /* broken-down time */ TIMELOCALE * timeInfo, /* locale information */ int * pnum, /* position number for strftime string */ const char * fmt /* format to be decoded */ ) { switch (*fmt) { case 'a': /* day */ __getDay (addOn, tmptr->tm_wday, ABBR, timeInfo, pnum); break; case 'A': /* day */ __getDay (addOn, tmptr->tm_wday, FULL, timeInfo, pnum); break; case 'b': /* month */ __getMonth (addOn, tmptr->tm_mon, ABBR, timeInfo, pnum); break; case 'B': /* month */ __getMonth (addOn, tmptr->tm_mon, FULL, timeInfo, pnum); break; case 'c': /* date and time */ __getLocale (addOn, DATETIME, tmptr, timeInfo, pnum); *pnum = -*pnum; break; case 'd': /* day of month */ __intToStr (addOn, tmptr->tm_mday, *pnum = 2); break; case 'H': /* hour */ __intToStr (addOn, tmptr->tm_hour, *pnum = 2); break; case 'I': /* hour */ __intToStr (addOn, ((tmptr->tm_hour % 12) ? (tmptr->tm_hour % 12) : 12), *pnum = 2); break; case 'j': /* day of year */ __intToStr (addOn, tmptr->tm_yday + 1, *pnum = 3); break; case 'm': /* month */ __intToStr (addOn, tmptr->tm_mon + 1, *pnum = 2); break; case 'M': /* minute */ __intToStr (addOn, tmptr->tm_min, *pnum = 2); break; case 'p': /* AP/PM */ __getLocale (addOn, AMPM, tmptr, timeInfo, pnum); break; case 'S': /* second */ __intToStr (addOn, tmptr->tm_sec, *pnum = 2); break; case 'U': /* week number */ __intToStr (addOn, __weekOfYear(TM_SUNDAY, tmptr->tm_wday, tmptr->tm_yday), *pnum = 2); break; case 'w': /* weekday */ __intToStr (addOn, tmptr->tm_wday, *pnum = 1); break; case 'W': /* week number */ __intToStr (addOn, __weekOfYear(TM_MONDAY, tmptr->tm_wday, tmptr->tm_yday), *pnum = 2); break; case 'x': /* date */ __getLocale (addOn, DATE, tmptr, timeInfo, pnum); *pnum = -*pnum; break; case 'X': /* time */ __getLocale (addOn, TIMEO, tmptr, timeInfo, pnum); *pnum = -*pnum; break; case 'y': /* year */ __intToStr (addOn, (tmptr->tm_year % CENTURY), *pnum = 2); break; case 'Y': /* year */ __intToStr (addOn, (tmptr->tm_year + TM_YEAR_BASE), *pnum = 4); break; case 'Z': /* zone */ __getLocale (addOn, ZONE, tmptr, timeInfo, pnum); break; case '%': /* % */ memcpy (addOn, CHAR_FROM_CONST ("%"), 1); *pnum = 1; break; default: *pnum = 0; break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -