📄 strftime.c
字号:
/* Copyright (C) 1991-1999, 2000, 2001 Free Software Foundation, Inc. NOTE: The canonical source of this file is maintained with the GNU C Library. Bugs can be reported to bug-glibc@prep.ai.mit.edu. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */#ifdef HAVE_CONFIG_H# include <config.h>#endif#ifdef _LIBC# define HAVE_LIMITS_H 1# define HAVE_MBLEN 1# define HAVE_MBRLEN 1# define HAVE_STRUCT_ERA_ENTRY 1# define HAVE_TM_GMTOFF 1# define HAVE_TM_ZONE 1# define HAVE_TZNAME 1# define HAVE_TZSET 1# define MULTIBYTE_IS_FORMAT_SAFE 1# define STDC_HEADERS 1# include "../locale/localeinfo.h"#endif#if defined emacs && !defined HAVE_BCOPY# define HAVE_MEMCPY 1#endif#include <ctype.h>#include <sys/types.h> /* Some systems define `time_t' here. */#ifdef TIME_WITH_SYS_TIME# include <sys/time.h># include <time.h>#else# ifdef HAVE_SYS_TIME_H# include <sys/time.h># else# include <time.h># endif#endif#if HAVE_TZNAMEextern char *tzname[];#endif/* Do multibyte processing if multibytes are supported, unless multibyte sequences are safe in formats. Multibyte sequences are safe if they cannot contain byte sequences that look like format conversion specifications. The GNU C Library uses UTF8 multibyte encoding, which is safe for formats, but strftime.c can be used with other C libraries that use unsafe encodings. */#define DO_MULTIBYTE (HAVE_MBLEN && ! MULTIBYTE_IS_FORMAT_SAFE)#if DO_MULTIBYTE# if HAVE_MBRLEN# include <wchar.h># else /* Simulate mbrlen with mblen as best we can. */# define mbstate_t int# define mbrlen(s, n, ps) mblen (s, n)# define mbsinit(ps) (*(ps) == 0)# endif static const mbstate_t mbstate_zero;#endif#if HAVE_LIMITS_H# include <limits.h>#endif#if STDC_HEADERS# include <stddef.h># include <stdlib.h># include <string.h>#else# ifndef HAVE_MEMCPY# define memcpy(d, s, n) bcopy ((s), (d), (n))# endif#endif#ifdef COMPILE_WIDE# include <endian.h># define CHAR_T wchar_t# define UCHAR_T unsigned int# define L_(Str) L##Str# define NLW(Sym) _NL_W##Sym# define MEMCPY(d, s, n) __wmemcpy (d, s, n)# define STRLEN(s) __wcslen (s)#else# define CHAR_T char# define UCHAR_T unsigned char# define L_(Str) Str# define NLW(Sym) Sym# if !defined STDC_HEADERS && !defined HAVE_MEMCPY# define MEMCPY(d, s, n) bcopy ((s), (d), (n))# else# define MEMCPY(d, s, n) memcpy ((d), (s), (n))# endif# define STRLEN(s) strlen (s)# ifdef _LIBC# define MEMPCPY(d, s, n) __mempcpy (d, s, n)# else# ifndef HAVE_MEMPCPY# define MEMPCPY(d, s, n) ((void *) ((char *) memcpy (d, s, n) + (n)))# endif# endif#endif#ifndef __P# if defined __GNUC__ || (defined __STDC__ && __STDC__)# define __P(args) args# else# define __P(args) ()# endif /* GCC. */#endif /* Not __P. */#ifndef PTR# ifdef __STDC__# define PTR void *# else# define PTR char *# endif#endif#ifndef CHAR_BIT# define CHAR_BIT 8#endif#ifndef NULL# define NULL 0#endif#define TYPE_SIGNED(t) ((t) -1 < 0)/* Bound on length of the string representing an integer value of type t. Subtract one for the sign bit if t is signed; 302 / 1000 is log10 (2) rounded up; add one for integer division truncation; add one more for a minus sign if t is signed. */#define INT_STRLEN_BOUND(t) \ ((sizeof (t) * CHAR_BIT - TYPE_SIGNED (t)) * 302 / 1000 + 1 + TYPE_SIGNED (t))#define TM_YEAR_BASE 1900#ifndef __isleap/* Nonzero if YEAR is a leap year (every 4 years, except every 100th isn't, and every 400th is). */# define __isleap(year) \ ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))#endif#ifdef _LIBC# define my_strftime_gmtime_r __gmtime_r# define my_strftime_localtime_r __localtime_r# define tzname __tzname# define tzset __tzset#else/* If we're a strftime substitute in a GNU program, then prefer gmtime to gmtime_r, since many gmtime_r implementations are buggy. Similarly for localtime_r. */# if ! HAVE_TM_GMTOFFstatic struct tm *my_strftime_gmtime_r __P ((const time_t *, struct tm *));static struct tm *my_strftime_gmtime_r (t, tp) const time_t *t; struct tm *tp;{ struct tm *l = gmtime (t); if (! l) return 0; *tp = *l; return tp;}static struct tm *my_strftime_localtime_r __P ((const time_t *, struct tm *));static struct tm *my_strftime_localtime_r (t, tp) const time_t *t; struct tm *tp;{ struct tm *l = localtime (t); if (! l) return 0; *tp = *l; return tp;}# endif /* ! HAVE_TM_GMTOFF */#endif /* ! defined _LIBC */#if !defined memset && !defined HAVE_MEMSET && !defined _LIBC/* Some systems lack the `memset' function and we don't want to introduce additional dependencies. *//* The SGI compiler reportedly barfs on the trailing null if we use a string constant as the initializer. 28 June 1997, rms. */static const CHAR_T spaces[16] = /* " " */{ L_(' '),L_(' '),L_(' '),L_(' '),L_(' '),L_(' '),L_(' '),L_(' '), L_(' '),L_(' '),L_(' '),L_(' '),L_(' '),L_(' '),L_(' '),L_(' ')};static const CHAR_T zeroes[16] = /* "0000000000000000" */{ L_('0'),L_('0'),L_('0'),L_('0'),L_('0'),L_('0'),L_('0'),L_('0'), L_('0'),L_('0'),L_('0'),L_('0'),L_('0'),L_('0'),L_('0'),L_('0')};# define memset_space(P, Len) \ do { \ int _len = (Len); \ \ do \ { \ int _this = _len > 16 ? 16 : _len; \ (P) = MEMPCPY ((P), spaces, _this * sizeof (CHAR_T)); \ _len -= _this; \ } \ while (_len > 0); \ } while (0)# define memset_zero(P, Len) \ do { \ int _len = (Len); \ \ do \ { \ int _this = _len > 16 ? 16 : _len; \ (P) = MEMPCPY ((P), zeroes, _this * sizeof (CHAR_T)); \ _len -= _this; \ } \ while (_len > 0); \ } while (0)#else# ifdef COMPILE_WIDE# define memset_space(P, Len) (wmemset ((P), L' ', (Len)), (P) += (Len))# define memset_zero(P, Len) (wmemset ((P), L'0', (Len)), (P) += (Len))# else# define memset_space(P, Len) (memset ((P), ' ', (Len)), (P) += (Len))# define memset_zero(P, Len) (memset ((P), '0', (Len)), (P) += (Len))# endif#endif#define add(n, f) \ do \ { \ int _n = (n); \ int _delta = width - _n; \ int _incr = _n + (_delta > 0 ? _delta : 0); \ if (i + _incr >= maxsize) \ return 0; \ if (p) \ { \ if (_delta > 0) \ { \ if (pad == L_('0')) \ memset_zero (p, _delta); \ else \ memset_space (p, _delta); \ } \ f; \ p += _n; \ } \ i += _incr; \ } while (0)#define cpy(n, s) \ add ((n), \ if (to_lowcase) \ memcpy_lowcase (p, (s), _n); \ else if (to_uppcase) \ memcpy_uppcase (p, (s), _n); \ else \ MEMCPY ((PTR) p, (const PTR) (s), _n))#ifdef COMPILE_WIDE# define widen(os, ws, l) \ { \ mbstate_t __st; \ const char *__s = os; \ memset (&__st, '\0', sizeof (__st)); \ l = __mbsrtowcs (NULL, &__s, 0, &__st); \ ws = alloca ((l + 1) * sizeof (wchar_t)); \ (void) __mbsrtowcs (ws, &__s, l, &__st); \ }#endif#ifdef COMPILE_WIDE# define TOUPPER(Ch) towupper (Ch)# define TOLOWER(Ch) towlower (Ch)#else# ifdef _LIBC# define TOUPPER(Ch) toupper (Ch)# define TOLOWER(Ch) tolower (Ch)# else# define TOUPPER(Ch) (islower (Ch) ? toupper (Ch) : (Ch))# define TOLOWER(Ch) (isupper (Ch) ? tolower (Ch) : (Ch))# endif#endif/* We don't use `isdigit' here since the locale dependent interpretation is not what we want here. We only need to accept the arabic digits in the ASCII range. One day there is perhaps a more reliable way to accept other sets of digits. */#define ISDIGIT(Ch) ((unsigned int) (Ch) - L_('0') <= 9)static CHAR_T *memcpy_lowcase __P ((CHAR_T *dest, const CHAR_T *src, size_t len));static CHAR_T *memcpy_lowcase (dest, src, len) CHAR_T *dest; const CHAR_T *src; size_t len;{ while (len-- > 0) dest[len] = TOLOWER ((UCHAR_T) src[len]); return dest;}static CHAR_T *memcpy_uppcase __P ((CHAR_T *dest, const CHAR_T *src, size_t len));static CHAR_T *memcpy_uppcase (dest, src, len) CHAR_T *dest; const CHAR_T *src; size_t len;{ while (len-- > 0) dest[len] = TOUPPER ((UCHAR_T) src[len]); return dest;}#if ! HAVE_TM_GMTOFF/* Yield the difference between *A and *B, measured in seconds, ignoring leap seconds. */# define tm_diff ftime_tm_diffstatic int tm_diff __P ((const struct tm *, const struct tm *));static inttm_diff (a, b) const struct tm *a; const struct tm *b;{ /* Compute intervening leap days correctly even if year is negative. Take care to avoid int overflow in leap day calculations, but it's OK to assume that A and B are close to each other. */ int a4 = (a->tm_year >> 2) + (TM_YEAR_BASE >> 2) - ! (a->tm_year & 3); int b4 = (b->tm_year >> 2) + (TM_YEAR_BASE >> 2) - ! (b->tm_year & 3); int a100 = a4 / 25 - (a4 % 25 < 0); int b100 = b4 / 25 - (b4 % 25 < 0); int a400 = a100 >> 2; int b400 = b100 >> 2; int intervening_leap_days = (a4 - b4) - (a100 - b100) + (a400 - b400); int years = a->tm_year - b->tm_year; int days = (365 * years + intervening_leap_days + (a->tm_yday - b->tm_yday)); return (60 * (60 * (24 * days + (a->tm_hour - b->tm_hour)) + (a->tm_min - b->tm_min)) + (a->tm_sec - b->tm_sec));}#endif /* ! HAVE_TM_GMTOFF *//* The number of days from the first day of the first ISO week of this year to the year day YDAY with week day WDAY. ISO weeks start on Monday; the first ISO week has the year's first Thursday. YDAY may be as small as YDAY_MINIMUM. */#define ISO_WEEK_START_WDAY 1 /* Monday */#define ISO_WEEK1_WDAY 4 /* Thursday */#define YDAY_MINIMUM (-366)static int iso_week_days __P ((int, int));#ifdef __GNUC____inline__#endifstatic intiso_week_days (yday, wday) int yday; int wday;{ /* Add enough to the first operand of % to make it nonnegative. */ int big_enough_multiple_of_7 = (-YDAY_MINIMUM / 7 + 2) * 7; return (yday - (yday - wday + ISO_WEEK1_WDAY + big_enough_multiple_of_7) % 7 + ISO_WEEK1_WDAY - ISO_WEEK_START_WDAY);}#if !(defined _NL_CURRENT || HAVE_STRFTIME)static CHAR_T const weekday_name[][10] = { L_("Sunday"), L_("Monday"), L_("Tuesday"), L_("Wednesday"), L_("Thursday"), L_("Friday"), L_("Saturday") };static CHAR_T const month_name[][10] = { L_("January"), L_("February"), L_("March"), L_("April"), L_("May"), L_("June"), L_("July"), L_("August"), L_("September"), L_("October"), L_("November"), L_("December") };#endif/* When compiling this file, GNU applications can #define my_strftime to a symbol (typically nstrftime) to get an extended strftime with extra arguments UT and NS. Emacs is a special case for now, but this Emacs-specific code can be removed once Emacs's config.h defines my_strftime. */#if defined emacs && !defined my_strftime# define my_strftime nstrftime#endif#ifdef my_strftime# define extra_args , ut, ns# define extra_args_spec int ut; int ns;# define extra_args_spec_iso , int ut, int ns#else# ifdef COMPILE_WIDE# define my_strftime wcsftime# else# define my_strftime strftime# endif# define extra_args# define extra_args_spec# define extra_args_spec_iso/* We don't have this information in general. */# define ut 0# define ns 0#endif#if !defined _LIBC && HAVE_TZNAME && HAVE_TZSET /* Solaris 2.5 tzset sometimes modifies the storage returned by localtime. Work around this bug by copying *tp before it might be munged. */ size_t _strftime_copytm __P ((char *, size_t, const char *, const struct tm * extra_args_spec_iso)); size_t my_strftime (s, maxsize, format, tp extra_args) CHAR_T *s; size_t maxsize; const CHAR_T *format; const struct tm *tp; extra_args_spec { struct tm tmcopy; tmcopy = *tp; return _strftime_copytm (s, maxsize, format, &tmcopy extra_args); }# undef my_strftime# define my_strftime _strftime_copytm
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -