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

📄 strftime_l.c

📁 Linux下头文件time.h的实现源码
💻 C
📖 第 1 页 / 共 3 页
字号:
/* Copyright (C) 2002, 2004, 2007 Free Software Foundation, Inc.   This file is part of the GNU C Library.   The GNU C Library is free software; you can redistribute it and/or   modify it under the terms of the GNU Lesser General Public   License as published by the Free Software Foundation; either   version 2.1 of the License, or (at your option) any later version.   The GNU C Library 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   Lesser General Public License for more details.   You should have received a copy of the GNU Lesser General Public   License along with the GNU C Library; 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 USE_IN_EXTENDED_LOCALE_MODEL 1# 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># include <stdbool.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 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 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.   Similarly for gmtime_r.  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#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 ((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 ((PTR) p, (const PTR) (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 = 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_PARAM , loc# define LOCALE_ARG , loc# define LOCALE_PARAM_DECL  __locale_t loc;# define LOCALE_PARAM_PROTO , __locale_t loc# define HELPER_LOCALE_ARG  , current#else# define LOCALE_PARAM# define LOCALE_PARAM_PROTO# define LOCALE_ARG# define LOCALE_PARAM_DECL# 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) __THROW;static CHAR_T *memcpy_lowcase (dest, src, len LOCALE_PARAM)     CHAR_T *dest;     const CHAR_T *src;     size_t len;     LOCALE_PARAM_DECL{  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) __THROW;static CHAR_T *memcpy_uppcase (dest, src, len LOCALE_PARAM)     CHAR_T *dest;     const CHAR_T *src;     size_t len;     LOCALE_PARAM_DECL{  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 int tm_diff (const struct tm *, const struct tm *) __THROW;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 (int, int) __THROW;#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#ifdef emacs# define my_strftime emacs_strftimeu# define ut_argument , ut# define ut_argument_spec int ut;# define ut_argument_spec_iso , int ut#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 ut_argument# define ut_argument_spec# define ut_argument_spec_iso/* We don't have this information in general.  */# define ut 0#endifstatic size_t __strftime_internal (CHAR_T *, size_t, const CHAR_T *,				   const struct tm *, bool ut_argument_spec_iso				   LOCALE_PARAM_PROTO) __THROW;/* 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 (s, maxsize, format, tp ut_argument LOCALE_PARAM)     CHAR_T *s;     size_t maxsize;     const CHAR_T *format;     const struct tm *tp;     ut_argument_spec     LOCALE_PARAM_DECL{#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.  */  struct tm tmcopy;  tmcopy = *tp;  tp = &tmcopy;

⌨️ 快捷键说明

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