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

📄 strftime.c

📁 KPIT GNU Tools is a set of GNU development tools for Renesas microcontrollers.
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * strftime.c * Original Author:	G. Haley * Additions from:	Eric Blake * * Places characters into the array pointed to by s as controlled by the string * pointed to by format. If the total number of resulting characters including * the terminating null character is not more than maxsize, returns the number * of characters placed into the array pointed to by s (not including the * terminating null character); otherwise zero is returned and the contents of * the array indeterminate. *//*FUNCTION<<strftime>>---flexible calendar time formatterINDEX	strftimeANSI_SYNOPSIS	#include <time.h>	size_t strftime(char *<[s]>, size_t <[maxsize]>,			const char *<[format]>, const struct tm *<[timp]>);TRAD_SYNOPSIS	#include <time.h>	size_t strftime(<[s]>, <[maxsize]>, <[format]>, <[timp]>)	char *<[s]>;	size_t <[maxsize]>;	char *<[format]>;	struct tm *<[timp]>;DESCRIPTION<<strftime>> converts a <<struct tm>> representation of the time (at<[timp]>) into a null-terminated string, starting at <[s]> and occupyingno more than <[maxsize]> characters.You control the format of the output using the string at <[format]>.<<*<[format]>>> can contain two kinds of specifications: text to becopied literally into the formatted string, and time conversionspecifications.  Time conversion specifications are two- andthree-character sequences beginning with `<<%>>' (use `<<%%>>' toinclude a percent sign in the output).  Each defined conversionspecification selects only the specified field(s) of calendar timedata from <<*<[timp]>>>, and converts it to a string in one of thefollowing ways:o+o %aA three-letter abbreviation for the day of the week. [tm_wday]o %AThe full name for the day of the week, one of `<<Sunday>>',`<<Monday>>', `<<Tuesday>>', `<<Wednesday>>', `<<Thursday>>',`<<Friday>>', or `<<Saturday>>'. [tm_wday]o %bA three-letter abbreviation for the month name. [tm_mon]o %BThe full name of the month, one of `<<January>>', `<<February>>',`<<March>>', `<<April>>', `<<May>>', `<<June>>', `<<July>>',`<<August>>', `<<September>>', `<<October>>', `<<November>>',`<<December>>'. [tm_mon]o %cA string representing the complete date and time, in the form`<<"%a %b %e %H:%M:%S %Y">>' (example "Mon Apr 01 13:13:131992"). [tm_sec, tm_min, tm_hour, tm_mday, tm_mon, tm_year, tm_wday]o %CThe century, that is, the year divided by 100 then truncated.  For4-digit years, the result is zero-padded and exactly two characters;but for other years, there may a negative sign or more digits.  Inthis way, `<<%C%y>>' is equivalent to `<<%Y>>'. [tm_year] o %dThe day of the month, formatted with two digits (from `<<01>>' to`<<31>>'). [tm_mday]o %DA string representing the date, in the form `<<"%m/%d/%y">>'.[tm_mday, tm_mon, tm_year]o %eThe day of the month, formatted with leading space if single digit(from `<<1>>' to `<<31>>'). [tm_mday]o %E<<x>>In some locales, the E modifier selects alternative representations ofcertain modifiers <<x>>.  But in the "C" locale supported by newlib,it is ignored, and treated as %<<x>>.o %FA string representing the ISO 8601:2000 date format, in the form`<<"%Y-%m-%d">>'. [tm_mday, tm_mon, tm_year]o %gThe last two digits of the week-based year, see specifier %G (from`<<00>>' to `<<99>>'). [tm_year, tm_wday, tm_yday]o %GThe week-based year. In the ISO 8601:2000 calendar, week 1 of the yearincludes January 4th, and begin on Mondays. Therefore, if January 1st,2nd, or 3rd falls on a Sunday, that day and earlier belong to the lastweek of the previous year; and if December 29th, 30th, or 31st fallson Monday, that day and later belong to week 1 of the next year.  Forconsistency with %Y, it always has at least four characters. Example: "%G" for Saturday 2nd January 1999 gives "1998", and forTuesday 30th December 1997 gives "1998". [tm_year, tm_wday, tm_yday]o %hA three-letter abbreviation for the month name (synonym for"%b"). [tm_mon]o %HThe hour (on a 24-hour clock), formatted with two digits (from`<<00>>' to `<<23>>'). [tm_hour]o %IThe hour (on a 12-hour clock), formatted with two digits (from`<<01>>' to `<<12>>'). [tm_hour]o %jThe count of days in the year, formatted with three digits(from `<<001>>' to `<<366>>'). [tm_yday]o %kThe hour (on a 24-hour clock), formatted with leading space if singledigit (from `<<0>>' to `<<23>>'). Non-POSIX extension. [tm_hour]o %lThe hour (on a 12-hour clock), formatted with leading space if singledigit (from `<<1>>' to `<<12>>'). Non-POSIX extension. [tm_hour]o %mThe month number, formatted with two digits (from `<<01>>' to `<<12>>').[tm_mon]o %MThe minute, formatted with two digits (from `<<00>>' to `<<59>>'). [tm_min]o %nA newline character (`<<\n>>').o %O<<x>>In some locales, the O modifier selects alternative digit charactersfor certain modifiers <<x>>.  But in the "C" locale supported by newlib, itis ignored, and treated as %<<x>>.o %pEither `<<AM>>' or `<<PM>>' as appropriate. [tm_hour]o %rThe 12-hour time, to the second.  Equivalent to "%I:%M:%S %p". [tm_sec,tm_min, tm_hour]o %RThe 24-hour time, to the minute.  Equivalent to "%H:%M". [tm_min, tm_hour]o %SThe second, formatted with two digits (from `<<00>>' to `<<60>>').  Thevalue 60 accounts for the occasional leap second. [tm_sec]o %tA tab character (`<<\t>>').o %TThe 24-hour time, to the second.  Equivalent to "%H:%M:%S". [tm_sec,tm_min, tm_hour]o %uThe weekday as a number, 1-based from Monday (from `<<1>>' to`<<7>>'). [tm_wday]o %UThe week number, where weeks start on Sunday, week 1 contains the firstSunday in a year, and earlier days are in week 0.  Formatted with twodigits (from `<<00>>' to `<<53>>').  See also <<%W>>. [tm_wday, tm_yday]o %VThe week number, where weeks start on Monday, week 1 contains January 4th,and earlier days are in the previous year.  Formatted with two digits(from `<<01>>' to `<<53>>').  See also <<%G>>. [tm_year, tm_wday, tm_yday]o %wThe weekday as a number, 0-based from Sunday (from `<<0>>' to `<<6>>').[tm_wday]o %WThe week number, where weeks start on Monday, week 1 contains the firstMonday in a year, and earlier days are in week 0.  Formatted with twodigits (from `<<00>>' to `<<53>>'). [tm_wday, tm_yday]o %xA string representing the complete date, equivalent to "%m/%d/%y".[tm_mon, tm_mday, tm_year]o %XA string representing the full time of day (hours, minutes, andseconds), equivalent to "%H:%M:%S". [tm_sec, tm_min, tm_hour]o %yThe last two digits of the year (from `<<00>>' to `<<99>>'). [tm_year]o %YThe full year, equivalent to <<%C%y>>.  It will always have at least fourcharacters, but may have more.  The year is accurate even when tm_yearadded to the offset of 1900 overflows an int. [tm_year]o %zThe offset from UTC.  The format consists of a sign (negative is west ofGreewich), two characters for hour, then two characters for minutes(-hhmm or +hhmm).  If tm_isdst is negative, the offset is unknown and nooutput is generated; if it is zero, the offset is the standard offset forthe current time zone; and if it is positive, the offset is the daylightsavings offset for the current timezone. The offset is determined fromthe TZ environment variable, as if by calling tzset(). [tm_isdst]o %ZThe time zone name.  If tm_isdst is negative, no output is generated.Otherwise, the time zone name is based on the TZ environment variable,as if by calling tzset(). [tm_isdst]o %%A single character, `<<%>>'.o-RETURNSWhen the formatted time takes up no more than <[maxsize]> characters,the result is the length of the formatted string.  Otherwise, if theformatting operation was abandoned due to lack of room, the result is<<0>>, and the string starting at <[s]> corresponds to just thoseparts of <<*<[format]>>> that could be completely filled in within the<[maxsize]> limit.PORTABILITYANSI C requires <<strftime>>, but does not specify the contents of<<*<[s]>>> when the formatted string would require more than<[maxsize]> characters.  Unrecognized specifiers and fields of<<timp>> that are out of range cause undefined results.  Since someformats expand to 0 bytes, it is wise to set <<*<[s]>>> to a nonzerovalue beforehand to distinguish between failure and an empty string.This implementation does not support <<s>> being NULL, nor overlapping<<s>> and <<format>>.<<strftime>> requires no supporting OS subroutines.*/#include <stddef.h>#include <stdio.h>#include <time.h>#include <string.h>#include <stdlib.h>#include "local.h"static _CONST int dname_len[7] ={6, 6, 7, 9, 8, 6, 8};static _CONST char *_CONST dname[7] ={"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};static _CONST int mname_len[12] ={7, 8, 5, 5, 3, 4, 4, 6, 9, 7, 8, 8};static _CONST char *_CONST mname[12] ={"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};/* Using the tm_year, tm_wday, and tm_yday components of TIM_P, return   -1, 0, or 1 as the adjustment to add to the year for the ISO week   numbering used in "%g%G%V", avoiding overflow.  */static int_DEFUN (iso_year_adjust, (tim_p),	_CONST struct tm *tim_p){  /* Account for fact that tm_year==0 is year 1900.  */  int leap = isleap (tim_p->tm_year + (YEAR_BASE				       - (tim_p->tm_year < 0 ? 0 : 2000)));  /* Pack the yday, wday, and leap year into a single int since there are so     many disparate cases.  */#define PACK(yd, wd, lp) (((yd) << 4) + (wd << 1) + (lp))  switch (PACK (tim_p->tm_yday, tim_p->tm_wday, leap))    {    case PACK (0, 5, 0): /* Jan 1 is Fri, not leap.  */    case PACK (0, 6, 0): /* Jan 1 is Sat, not leap.  */    case PACK (0, 0, 0): /* Jan 1 is Sun, not leap.  */    case PACK (0, 5, 1): /* Jan 1 is Fri, leap year.  */    case PACK (0, 6, 1): /* Jan 1 is Sat, leap year.  */    case PACK (0, 0, 1): /* Jan 1 is Sun, leap year.  */    case PACK (1, 6, 0): /* Jan 2 is Sat, not leap.  */    case PACK (1, 0, 0): /* Jan 2 is Sun, not leap.  */    case PACK (1, 6, 1): /* Jan 2 is Sat, leap year.  */    case PACK (1, 0, 1): /* Jan 2 is Sun, leap year.  */    case PACK (2, 0, 0): /* Jan 3 is Sun, not leap.  */    case PACK (2, 0, 1): /* Jan 3 is Sun, leap year.  */      return -1; /* Belongs to last week of previous year.  */    case PACK (362, 1, 0): /* Dec 29 is Mon, not leap.  */    case PACK (363, 1, 1): /* Dec 29 is Mon, leap year.  */    case PACK (363, 1, 0): /* Dec 30 is Mon, not leap.  */    case PACK (363, 2, 0): /* Dec 30 is Tue, not leap.  */    case PACK (364, 1, 1): /* Dec 30 is Mon, leap year.  */    case PACK (364, 2, 1): /* Dec 30 is Tue, leap year.  */    case PACK (364, 1, 0): /* Dec 31 is Mon, not leap.  */    case PACK (364, 2, 0): /* Dec 31 is Tue, not leap.  */    case PACK (364, 3, 0): /* Dec 31 is Wed, not leap.  */    case PACK (365, 1, 1): /* Dec 31 is Mon, leap year.  */    case PACK (365, 2, 1): /* Dec 31 is Tue, leap year.  */    case PACK (365, 3, 1): /* Dec 31 is Wed, leap year.  */      return 1; /* Belongs to first week of next year.  */    }  return 0; /* Belongs to specified year.  */#undef PACK}size_t_DEFUN (strftime, (s, maxsize, format, tim_p),	char *s _AND	size_t maxsize _AND	_CONST char *format _AND	_CONST struct tm *tim_p){  size_t count = 0;  int i;  for (;;)    {      while (*format && *format != '%')	{	  if (count < maxsize - 1)	    s[count++] = *format++;	  else	    return 0;	}      if (*format == '\0')	break;      format++;      if (*format == 'E' || *format == 'O')	format++;      switch (*format)	{	case 'a':	  for (i = 0; i < 3; i++)	    {	      if (count < maxsize - 1)		s[count++] =		  dname[tim_p->tm_wday][i];	      else		return 0;	    }	  break;	case 'A':	  for (i = 0; i < dname_len[tim_p->tm_wday]; i++)	    {	      if (count < maxsize - 1)		s[count++] =		  dname[tim_p->tm_wday][i];	      else		return 0;	    }	  break;	case 'b':	case 'h':	  for (i = 0; i < 3; i++)	    {	      if (count < maxsize - 1)		s[count++] =		  mname[tim_p->tm_mon][i];	      else		return 0;	    }	  break;	case 'B':	  for (i = 0; i < mname_len[tim_p->tm_mon]; i++)	    {	      if (count < maxsize - 1)		s[count++] =		  mname[tim_p->tm_mon][i];	      else		return 0;	    }	  break;	case 'c':	  {	    /* Length is not known because of %C%y, so recurse. */	    size_t adjust = strftime (&s[count], maxsize - count,				      "%a %b %e %H:%M:%S %C%y", tim_p);	    if (adjust > 0)	      count += adjust;	    else	      return 0;	  }	  break;	case 'C':	  {	    /* Examples of (tm_year + YEAR_BASE) that show how %Y == %C%y	       with 32-bit int.	       %Y		%C		%y	       2147485547	21474855	47	       10000		100		00

⌨️ 快捷键说明

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