📄 strftime.c
字号:
/* Copyright (C) 1991-1999, 2000, 2001, 2003 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 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_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# include "../locale/localeinfo.h"#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#include <limits.h>#include <stddef.h>#include <stdlib.h>#include <string.h>#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# define MEMCPY(d, s, n) memcpy (d, s, n)# 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#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 tzname __tzname# define tzset __tzset#endif#if !HAVE_TM_GMTOFF/* Portable standalone applications should supply a "time_r.h" that declares a POSIX-compliant localtime_r, for the benefit of older implementations that lack localtime_r or have a nonstandard one. See the gnulib time_r module for one way to implement this. */# include "time_r.h"# undef __gmtime_r# undef __localtime_r# define __gmtime_r gmtime_r# define __localtime_r localtime_r#endif#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#define add(n, f) \ do \ { \ int _n = (n); \ int _delta = width - _n; \ int _incr = _n + (_delta > 0 ? _delta : 0); \ if ((size_t) _incr >= maxsize - i) \ 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 LOCALE_ARG); \ else if (to_uppcase) \ memcpy_uppcase (p, (s), _n LOCALE_ARG); \ else \ MEMCPY ((void *) p, (void const *) (s), _n))#ifdef COMPILE_WIDE# ifndef USE_IN_EXTENDED_LOCALE_MODEL# undef __mbsrtowcs_l# define __mbsrtowcs_l(d, s, l, st, loc) __mbsrtowcs (d, s, l, st)# endif# define widen(os, ws, l) \ { \ mbstate_t __st; \ const char *__s = os; \ memset (&__st, '\0', sizeof (__st)); \ l = __mbsrtowcs_l (NULL, &__s, 0, &__st, loc); \ ws = (wchar_t *) alloca ((l + 1) * sizeof (wchar_t)); \ (void) __mbsrtowcs_l (ws, &__s, l, &__st, loc); \ }#endif#if defined _LIBC && defined USE_IN_EXTENDED_LOCALE_MODEL/* We use this code also for the extended locale handling where the function gets as an additional argument the locale which has to be used. To access the values we have to redefine the _NL_CURRENT macro. */# define strftime __strftime_l# define wcsftime __wcsftime_l# undef _NL_CURRENT# define _NL_CURRENT(category, item) \ (current->values[_NL_ITEM_INDEX (item)].string)# define LOCALE_ARG , loc# define LOCALE_PARAM_PROTO , __locale_t loc# define HELPER_LOCALE_ARG , current#else# define LOCALE_PARAM_PROTO# define LOCALE_ARG# ifdef _LIBC# define HELPER_LOCALE_ARG , _NL_CURRENT_DATA (LC_TIME)# else# define HELPER_LOCALE_ARG# endif#endif#ifdef COMPILE_WIDE# ifdef USE_IN_EXTENDED_LOCALE_MODEL# define TOUPPER(Ch, L) __towupper_l (Ch, L)# define TOLOWER(Ch, L) __towlower_l (Ch, L)# else# define TOUPPER(Ch, L) towupper (Ch)# define TOLOWER(Ch, L) towlower (Ch)# endif#else# ifdef _LIBC# ifdef USE_IN_EXTENDED_LOCALE_MODEL# define TOUPPER(Ch, L) __toupper_l (Ch, L)# define TOLOWER(Ch, L) __tolower_l (Ch, L)# else# define TOUPPER(Ch, L) toupper (Ch)# define TOLOWER(Ch, L) tolower (Ch)# endif# else# define TOUPPER(Ch, L) (islower (Ch) ? toupper (Ch) : (Ch))# define TOLOWER(Ch, L) (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 (CHAR_T *dest, const CHAR_T *src, size_t len LOCALE_PARAM_PROTO){ while (len-- > 0) dest[len] = TOLOWER ((UCHAR_T) src[len], loc); return dest;}static CHAR_T *memcpy_uppcase (CHAR_T *dest, const CHAR_T *src, size_t len LOCALE_PARAM_PROTO){ while (len-- > 0) dest[len] = TOUPPER ((UCHAR_T) src[len], loc); 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 inttm_diff (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)#ifdef __GNUC____inline__#endifstatic intiso_week_days (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#else# ifdef COMPILE_WIDE# define my_strftime wcsftime# define nl_get_alt_digit _nl_get_walt_digit# else# define my_strftime strftime# define nl_get_alt_digit _nl_get_alt_digit# endif# define extra_args# define extra_args_spec/* We don't have this information in general. */# define ut 0# define ns 0#endif#if ! defined _LIBC && ! HAVE_RUN_TZSET_TEST/* Solaris 2.5.x and 2.6 tzset sometimes modify the storage returned by localtime. On such systems, we must use the tzset and localtime wrappers to work around the bug. */"you must run the autoconf test for a working tzset function"#endif/* Write information from TP into S according to the format string FORMAT, writing no more that MAXSIZE characters (including the terminating '\0') and returning number of characters written. If S is NULL, nothing will be written anywhere, so to determine how many characters would be written, use NULL for S and (size_t) UINT_MAX for MAXSIZE. */size_tmy_strftime (CHAR_T *s, size_t maxsize, const CHAR_T *format, const struct tm *tp extra_args_spec LOCALE_PARAM_PROTO){#if defined _LIBC && defined USE_IN_EXTENDED_LOCALE_MODEL struct locale_data *const current = loc->__locales[LC_TIME];#endif int hour12 = tp->tm_hour;#ifdef _NL_CURRENT /* We cannot make the following values variables since we must delay the evaluation of these values until really needed since some expressions might not be valid in every situation. The `struct tm' might be generated by a strptime() call that initialized only a few elements. Dereference the pointers only if the format requires this. Then it is ok to fail if the pointers are invalid. */# define a_wkday \ ((const CHAR_T *) _NL_CURRENT (LC_TIME, NLW(ABDAY_1) + tp->tm_wday))# define f_wkday \ ((const CHAR_T *) _NL_CURRENT (LC_TIME, NLW(DAY_1) + tp->tm_wday))# define a_month \ ((const CHAR_T *) _NL_CURRENT (LC_TIME, NLW(ABMON_1) + tp->tm_mon))# define f_month \ ((const CHAR_T *) _NL_CURRENT (LC_TIME, NLW(MON_1) + tp->tm_mon))# define ampm \ ((const CHAR_T *) _NL_CURRENT (LC_TIME, tp->tm_hour > 11 \ ? NLW(PM_STR) : NLW(AM_STR)))# define aw_len STRLEN (a_wkday)# define am_len STRLEN (a_month)# define ap_len STRLEN (ampm)#else# if !HAVE_STRFTIME# define f_wkday (weekday_name[tp->tm_wday])# define f_month (month_name[tp->tm_mon])# define a_wkday f_wkday# define a_month f_month# define ampm (L_("AMPM") + 2 * (tp->tm_hour > 11)) size_t aw_len = 3; size_t am_len = 3; size_t ap_len = 2;# endif#endif const char *zone; size_t i = 0; CHAR_T *p = s; const CHAR_T *f;#if DO_MULTIBYTE && !defined COMPILE_WIDE const char *format_end = NULL;#endif zone = NULL;#if HAVE_TM_ZONE /* The POSIX test suite assumes that setting the environment variable TZ to a new value before calling strftime() will influence the result (the %Z format) even if the information in TP is computed with a totally different time zone. This is bogus: though POSIX allows bad behavior like this, POSIX does not require it. Do the right thing instead. */ zone = (const char *) tp->tm_zone;#endif#if HAVE_TZNAME if (ut) { if (! (zone && *zone)) zone = "GMT"; } else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -