コード例 #1
0
ファイル: strftime.c プロジェクト: phan-pivotal/gpdb
/*
 * POSIX and the C Standard are unclear or inconsistent about
 * what %C and %y do if the year is negative or exceeds 9999.
 * Use the convention that %C concatenated with %y yields the
 * same output as %Y, and that %Y contains at least 4 bytes,
 * with more only if necessary.
 */
static char *
_yconv(int a, int b, bool convert_top, bool convert_yy,
	   char *pt, const char *ptlim)
{
	int			lead;
	int			trail;

#define DIVISOR 100
	trail = a % DIVISOR + b % DIVISOR;
	lead = a / DIVISOR + b / DIVISOR + trail / DIVISOR;
	trail %= DIVISOR;
	if (trail < 0 && lead > 0)
	{
		trail += DIVISOR;
		--lead;
	}
	else if (lead < 0 && trail > 0)
	{
		trail -= DIVISOR;
		++lead;
	}
	if (convert_top)
	{
		if (lead == 0 && trail < 0)
			pt = _add("-0", pt, ptlim);
		else
			pt = _conv(lead, "%02d", pt, ptlim);
	}
	if (convert_yy)
		pt = _conv(((trail < 0) ? -trail : trail), "%02d", pt, ptlim);
	return pt;
}
コード例 #2
0
ファイル: EelEnergy.C プロジェクト: ragusa/Eel_Euler
Real EelEnergy::computeQpResidual()
{
    // Compute convective part of the energy equation:
    RealVectorValue _conv;
    _conv(0) = _rhouA_x[_qp] * ( _u[_qp] + _pressure[_qp]*_area[_qp] ) / _rhoA[_qp];
    _conv(1) = _rhouA_y[_qp] * ( _u[_qp] + _pressure[_qp]*_area[_qp] ) / _rhoA[_qp];
    _conv(2) = _rhouA_z[_qp] * ( _u[_qp] + _pressure[_qp]*_area[_qp] ) / _rhoA[_qp];
    
    // Gravity work:
    RealVectorValue _vector_vel(_rhouA_x[_qp]/_rhoA[_qp], _rhouA_y[_qp]/_rhoA[_qp], _rhouA_z[_qp]/_rhoA[_qp]);
    Real _gravity_work = _rhoA[_qp]*_gravity*_vector_vel;
    
    // Wall heat tranfer (WHT):
    Real rho = _rhoA[_qp] / _area[_qp];
    
    Real Hw_val = isParamValid("Hw_fn_name") ? getFunctionByName(_Hw_fn_name).value(_t, _q_point[_qp]) : _Hw;
    Real Tw_val = isParamValid("Tw_fn_name") ? getFunctionByName(_Tw_fn_name).value(_t, _q_point[_qp]) : _Tw;
    Real WHT = Hw_val * _aw * ( _eos.temperature_from_p_rho(_pressure[_qp], rho) - Tw_val );

    // Returns the residual
    return -_conv * _grad_test[_i][_qp] + (WHT+_gravity_work)*_test[_i][_qp];
}
コード例 #3
0
ファイル: strftime.c プロジェクト: phan-pivotal/gpdb
static char *
_fmt(const char *format, const struct pg_tm *t, char *pt,
	 const char *ptlim, enum warn *warnp)
{
	for (; *format; ++format)
	{
		if (*format == '%')
		{
	label:
			switch (*++format)
			{
				case '\0':
					--format;
					break;
				case 'A':
					pt = _add((t->tm_wday < 0 ||
							   t->tm_wday >= DAYSPERWEEK) ?
							  "?" : Locale->weekday[t->tm_wday],
							  pt, ptlim);
					continue;
				case 'a':
					pt = _add((t->tm_wday < 0 ||
							   t->tm_wday >= DAYSPERWEEK) ?
							  "?" : Locale->wday[t->tm_wday],
							  pt, ptlim);
					continue;
				case 'B':
					pt = _add((t->tm_mon < 0 ||
							   t->tm_mon >= MONSPERYEAR) ?
							  "?" : Locale->month[t->tm_mon],
							  pt, ptlim);
					continue;
				case 'b':
				case 'h':
					pt = _add((t->tm_mon < 0 ||
							   t->tm_mon >= MONSPERYEAR) ?
							  "?" : Locale->mon[t->tm_mon],
							  pt, ptlim);
					continue;
				case 'C':

					/*
					 * %C used to do a... _fmt("%a %b %e %X %Y", t);
					 * ...whereas now POSIX 1003.2 calls for something
					 * completely different. (ado, 1993-05-24)
					 */
					pt = _yconv(t->tm_year, TM_YEAR_BASE,
								true, false, pt, ptlim);
					continue;
				case 'c':
					{
						enum warn	warn2 = IN_SOME;

						pt = _fmt(Locale->c_fmt, t, pt, ptlim, &warn2);
						if (warn2 == IN_ALL)
							warn2 = IN_THIS;
						if (warn2 > *warnp)
							*warnp = warn2;
					}
					continue;
				case 'D':
					pt = _fmt("%m/%d/%y", t, pt, ptlim, warnp);
					continue;
				case 'd':
					pt = _conv(t->tm_mday, "%02d", pt, ptlim);
					continue;
				case 'E':
				case 'O':

					/*
					 * Locale modifiers of C99 and later. The sequences %Ec
					 * %EC %Ex %EX %Ey %EY %Od %oe %OH %OI %Om %OM %OS %Ou %OU
					 * %OV %Ow %OW %Oy are supposed to provide alternate
					 * representations.
					 */
					goto label;
				case 'e':
					pt = _conv(t->tm_mday, "%2d", pt, ptlim);
					continue;
				case 'F':
					pt = _fmt("%Y-%m-%d", t, pt, ptlim, warnp);
					continue;
				case 'H':
					pt = _conv(t->tm_hour, "%02d", pt, ptlim);
					continue;
				case 'I':
					pt = _conv((t->tm_hour % 12) ?
							   (t->tm_hour % 12) : 12,
							   "%02d", pt, ptlim);
					continue;
				case 'j':
					pt = _conv(t->tm_yday + 1, "%03d", pt, ptlim);
					continue;
				case 'k':

					/*
					 * This used to be... _conv(t->tm_hour % 12 ? t->tm_hour %
					 * 12 : 12, 2, ' '); ...and has been changed to the below
					 * to match SunOS 4.1.1 and Arnold Robbins' strftime
					 * version 3.0. That is, "%k" and "%l" have been swapped.
					 * (ado, 1993-05-24)
					 */
					pt = _conv(t->tm_hour, "%2d", pt, ptlim);
					continue;
#ifdef KITCHEN_SINK
				case 'K':

					/*
					 * After all this time, still unclaimed!
					 */
					pt = _add("kitchen sink", pt, ptlim);
					continue;
#endif							/* defined KITCHEN_SINK */
				case 'l':

					/*
					 * This used to be... _conv(t->tm_hour, 2, ' '); ...and
					 * has been changed to the below to match SunOS 4.1.1 and
					 * Arnold Robbin's strftime version 3.0. That is, "%k" and
					 * "%l" have been swapped. (ado, 1993-05-24)
					 */
					pt = _conv((t->tm_hour % 12) ?
							   (t->tm_hour % 12) : 12,
							   "%2d", pt, ptlim);
					continue;
				case 'M':
					pt = _conv(t->tm_min, "%02d", pt, ptlim);
					continue;
				case 'm':
					pt = _conv(t->tm_mon + 1, "%02d", pt, ptlim);
					continue;
				case 'n':
					pt = _add("\n", pt, ptlim);
					continue;
				case 'p':
					pt = _add((t->tm_hour >= (HOURSPERDAY / 2)) ?
							  Locale->pm :
							  Locale->am,
							  pt, ptlim);
					continue;
				case 'R':
					pt = _fmt("%H:%M", t, pt, ptlim, warnp);
					continue;
				case 'r':
					pt = _fmt("%I:%M:%S %p", t, pt, ptlim, warnp);
					continue;
				case 'S':
					pt = _conv(t->tm_sec, "%02d", pt, ptlim);
					continue;
				case 'T':
					pt = _fmt("%H:%M:%S", t, pt, ptlim, warnp);
					continue;
				case 't':
					pt = _add("\t", pt, ptlim);
					continue;
				case 'U':
					pt = _conv((t->tm_yday + DAYSPERWEEK -
								t->tm_wday) / DAYSPERWEEK,
							   "%02d", pt, ptlim);
					continue;
				case 'u':

					/*
					 * From Arnold Robbins' strftime version 3.0: "ISO 8601:
					 * Weekday as a decimal number [1 (Monday) - 7]" (ado,
					 * 1993-05-24)
					 */
					pt = _conv((t->tm_wday == 0) ?
							   DAYSPERWEEK : t->tm_wday,
							   "%d", pt, ptlim);
					continue;
				case 'V':		/* ISO 8601 week number */
				case 'G':		/* ISO 8601 year (four digits) */
				case 'g':		/* ISO 8601 year (two digits) */
/*
 * From Arnold Robbins' strftime version 3.0: "the week number of the
 * year (the first Monday as the first day of week 1) as a decimal number
 * (01-53)."
 * (ado, 1993-05-24)
 *
 * From <https://www.cl.cam.ac.uk/~mgk25/iso-time.html> by Markus Kuhn:
 * "Week 01 of a year is per definition the first week which has the
 * Thursday in this year, which is equivalent to the week which contains
 * the fourth day of January. In other words, the first week of a new year
 * is the week which has the majority of its days in the new year. Week 01
 * might also contain days from the previous year and the week before week
 * 01 of a year is the last week (52 or 53) of the previous year even if
 * it contains days from the new year. A week starts with Monday (day 1)
 * and ends with Sunday (day 7). For example, the first week of the year
 * 1997 lasts from 1996-12-30 to 1997-01-05..."
 * (ado, 1996-01-02)
 */
					{
						int			year;
						int			base;
						int			yday;
						int			wday;
						int			w;

						year = t->tm_year;
						base = TM_YEAR_BASE;
						yday = t->tm_yday;
						wday = t->tm_wday;
						for (;;)
						{
							int			len;
							int			bot;
							int			top;

							len = isleap_sum(year, base) ?
								DAYSPERLYEAR :
								DAYSPERNYEAR;

							/*
							 * What yday (-3 ... 3) does the ISO year begin
							 * on?
							 */
							bot = ((yday + 11 - wday) %
								   DAYSPERWEEK) - 3;

							/*
							 * What yday does the NEXT ISO year begin on?
							 */
							top = bot -
								(len % DAYSPERWEEK);
							if (top < -3)
								top += DAYSPERWEEK;
							top += len;
							if (yday >= top)
							{
								++base;
								w = 1;
								break;
							}
							if (yday >= bot)
							{
								w = 1 + ((yday - bot) /
										 DAYSPERWEEK);
								break;
							}
							--base;
							yday += isleap_sum(year, base) ?
								DAYSPERLYEAR :
								DAYSPERNYEAR;
						}
						if (*format == 'V')
							pt = _conv(w, "%02d",
									   pt, ptlim);
						else if (*format == 'g')
						{
							*warnp = IN_ALL;
							pt = _yconv(year, base,
										false, true,
										pt, ptlim);
						}
						else
							pt = _yconv(year, base,
										true, true,
										pt, ptlim);
					}
					continue;
				case 'v':

					/*
					 * From Arnold Robbins' strftime version 3.0: "date as
					 * dd-bbb-YYYY" (ado, 1993-05-24)
					 */
					pt = _fmt("%e-%b-%Y", t, pt, ptlim, warnp);
					continue;
				case 'W':
					pt = _conv((t->tm_yday + DAYSPERWEEK -
								(t->tm_wday ?
								 (t->tm_wday - 1) :
								 (DAYSPERWEEK - 1))) / DAYSPERWEEK,
							   "%02d", pt, ptlim);
					continue;
				case 'w':
					pt = _conv(t->tm_wday, "%d", pt, ptlim);
					continue;
				case 'X':
					pt = _fmt(Locale->X_fmt, t, pt, ptlim, warnp);
					continue;
				case 'x':
					{
						enum warn	warn2 = IN_SOME;

						pt = _fmt(Locale->x_fmt, t, pt, ptlim, &warn2);
						if (warn2 == IN_ALL)
							warn2 = IN_THIS;
						if (warn2 > *warnp)
							*warnp = warn2;
					}
					continue;
				case 'y':
					*warnp = IN_ALL;
					pt = _yconv(t->tm_year, TM_YEAR_BASE,
								false, true,
								pt, ptlim);
					continue;
				case 'Y':
					pt = _yconv(t->tm_year, TM_YEAR_BASE,
								true, true,
								pt, ptlim);
					continue;
				case 'Z':
					if (t->tm_zone != NULL)
						pt = _add(t->tm_zone, pt, ptlim);

					/*
					 * C99 and later say that %Z must be replaced by the empty
					 * string if the time zone is not determinable.
					 */
					continue;
				case 'z':
					{
						long		diff;
						char const *sign;
						bool		negative;

						if (t->tm_isdst < 0)
							continue;
						diff = t->tm_gmtoff;
						negative = diff < 0;
						if (diff == 0)
						{
							if (t->tm_zone != NULL)
								negative = t->tm_zone[0] == '-';
						}
						if (negative)
						{
							sign = "-";
							diff = -diff;
						}
						else
							sign = "+";
						pt = _add(sign, pt, ptlim);
						diff /= SECSPERMIN;
						diff = (diff / MINSPERHOUR) * 100 +
							(diff % MINSPERHOUR);
						pt = _conv(diff, "%04d", pt, ptlim);
					}
					continue;
				case '+':
					pt = _fmt(Locale->date_fmt, t, pt, ptlim,
							  warnp);
					continue;
				case '%':

					/*
					 * X311J/88-090 (4.12.3.5): if conversion char is
					 * undefined, behavior is undefined. Print out the
					 * character itself as printf(3) also does.
					 */
				default:
					break;
			}
		}
		if (pt == ptlim)
			break;
		*pt++ = *format;
	}
	return pt;
}
コード例 #4
0
ファイル: time_lib.c プロジェクト: brtos/brtos-contiki
static char *_fmt(const char *format, const struct tm *t, char *pt, const char *ptlim) {
  for ( ; *format; ++format) {
    if (*format == '%') {
      if (*format == 'E') {
        format++; // Alternate Era
      } else if (*format == 'O') {
        format++; // Alternate numeric symbols
      }

      switch (*++format) {
        case '\0':
          --format;
          break;

        case 'A':
          pt = _add((t->tm_wday < 0 || t->tm_wday > 6) ? "?" : _days[t->tm_wday], pt, ptlim);
          continue;

        case 'a':
          pt = _add((t->tm_wday < 0 || t->tm_wday > 6) ? "?" : _days_abbrev[t->tm_wday], pt, ptlim);
          continue;

        case 'B':
          pt = _add((t->tm_mon < 0 || t->tm_mon > 11) ? "?" : _months[t->tm_mon], pt, ptlim);
          continue;

        case 'b':
        case 'h':
          pt = _add((t->tm_mon < 0 || t->tm_mon > 11) ? "?" : _months_abbrev[t->tm_mon], pt, ptlim);
          continue;

        case 'C':
          pt = _conv((t->tm_year + TM_YEAR_BASE) / 100, "%02d", pt, ptlim);
          continue;

        case 'c':
          pt = _fmt("%a %b %e %H:%M:%S %Y", t, pt, ptlim);
          continue;

        case 'D':
          pt = _fmt("%m/%d/%y", t, pt, ptlim);
          continue;

        case 'd':
          pt = _conv(t->tm_mday, "%02d", pt, ptlim);
          continue;

        case 'e':
          pt = _conv(t->tm_mday, "%2d", pt, ptlim);
          continue;

        case 'F':
          pt = _fmt("%Y-%m-%d", t, pt, ptlim);
          continue;

        case 'H':
          pt = _conv(t->tm_hour, "%02d", pt, ptlim);
          continue;

        case 'I':
          pt = _conv((t->tm_hour % 12) ? (t->tm_hour % 12) : 12, "%02d", pt, ptlim);
          continue;

        case 'j':
          pt = _conv(t->tm_yday + 1, "%03d", pt, ptlim);
          continue;

        case 'k':
          pt = _conv(t->tm_hour, "%2d", pt, ptlim);
          continue;

        case 'l':
          pt = _conv((t->tm_hour % 12) ? (t->tm_hour % 12) : 12, "%2d", pt, ptlim);
          continue;

        case 'M':
          pt = _conv(t->tm_min, "%02d", pt, ptlim);
          continue;

        case 'm':
          pt = _conv(t->tm_mon + 1, "%02d", pt, ptlim);
          continue;

        case 'n':
          pt = _add("\n", pt, ptlim);
          continue;

        case 'p':
          pt = _add((t->tm_hour >= 12) ? "pm" : "am", pt, ptlim);
          continue;

        case 'R':
          pt = _fmt("%H:%M", t, pt, ptlim);
          continue;

        case 'r':
          pt = _fmt("%I:%M:%S %p", t, pt, ptlim);
          continue;

        case 'S':
          pt = _conv(t->tm_sec, "%02d", pt, ptlim);
          continue;

        case 's': {
          struct tm  tm;
          char buf[32];
          time_t mkt;

          tm = *t;
          mkt = mktime(&tm);
          sprintf(buf, "%lu", mkt);
          pt = _add(buf, pt, ptlim);
          continue;
        }

        case 'T':
          pt = _fmt("%H:%M:%S", t, pt, ptlim);
          continue;

        case 't':
          pt = _add("\t", pt, ptlim);
          continue;

        case 'U':
          pt = _conv((t->tm_yday + 7 - t->tm_wday) / 7, "%02d", pt, ptlim);
          continue;

        case 'u':
          pt = _conv((t->tm_wday == 0) ? 7 : t->tm_wday, "%d", pt, ptlim);
          continue;

        case 'V':   // ISO 8601 week number
        case 'G':   // ISO 8601 year (four digits)
        case 'g': { // ISO 8601 year (two digits)
          int  year;
          int  yday;
          int  wday;
          int  w;

          year = t->tm_year + TM_YEAR_BASE;
          yday = t->tm_yday;
          wday = t->tm_wday;
          while (1) {
            int  len;
            int  bot;
            int  top;

            len = LEAPYEAR(year) ? DAYSPERLYEAR : DAYSPERNYEAR;
            bot = ((yday + 11 - wday) % DAYSPERWEEK) - 3;
            top = bot - (len % DAYSPERWEEK);
            if (top < -3) top += DAYSPERWEEK;
            top += len;
            if (yday >= top) {
              ++year;
              w = 1;
              break;
            }
            if (yday >= bot) {
              w = 1 + ((yday - bot) / DAYSPERWEEK);
              break;
            }
            --year;
            yday += LEAPYEAR(year) ? DAYSPERLYEAR : DAYSPERNYEAR;
          }
          if (*format == 'V') {
            pt = _conv(w, "%02d", pt, ptlim);
          } else if (*format == 'g') {
            pt = _conv(year % 100, "%02d", pt, ptlim);
          } else {
            pt = _conv(year, "%04d", pt, ptlim);
          }
          continue;
        }

        case 'v':
          pt = _fmt("%e-%b-%Y", t, pt, ptlim);
          continue;

        case 'W':
          pt = _conv((t->tm_yday + 7 - (t->tm_wday ? (t->tm_wday - 1) : 6)) / 7, "%02d", pt, ptlim);
          continue;

        case 'w':
          pt = _conv(t->tm_wday, "%d", pt, ptlim);
          continue;

        case 'X':
          pt = _fmt("%H:%M:%S", t, pt, ptlim);
          continue;

        case 'x':
          pt = _fmt("%m/%d/%y", t, pt, ptlim);
          continue;

        case 'y':
          pt = _conv((t->tm_year + TM_YEAR_BASE) % 100, "%02d", pt, ptlim);
          continue;

        case 'Y':
          pt = _conv(t->tm_year + TM_YEAR_BASE, "%04d", pt, ptlim);
          continue;

        case 'Z':
          pt = _add("?", pt, ptlim);
          continue;

        case 'z': {
          long absoff;
          if (_timezone >= 0) {
            absoff = _timezone;
            pt = _add("+", pt, ptlim);
          } else {
            absoff = _timezone;
            pt = _add("-", pt, ptlim);
          }
          pt = _conv(absoff / 3600, "%02d", pt, ptlim);
          pt = _conv((absoff % 3600) / 60, "%02d", pt, ptlim);

          continue;
        }

        case '+':
          pt = _fmt("%a, %d %b %Y %H:%M:%S %z", t, pt, ptlim);
          continue;

        case '%':
        default:
          break;
      }
    }

    if (pt == ptlim) break;
    *pt++ = *format;
  }

  return pt;
}
コード例 #5
0
ファイル: strftime.c プロジェクト: nessneldivad/tz
static char *
_fmt(const char *format, const struct tm *const t, char * pt,
     const char *const ptlim, int *warnp)
{
	for ( ; *format; ++format) {
		if (*format == '%') {
label:
			switch (*++format) {
			case '\0':
				--format;
				break;
			case 'A':
				pt = _add((t->tm_wday < 0 ||
					t->tm_wday >= DAYSPERWEEK) ?
					"?" : Locale->weekday[t->tm_wday],
					pt, ptlim);
				continue;
			case 'a':
				pt = _add((t->tm_wday < 0 ||
					t->tm_wday >= DAYSPERWEEK) ?
					"?" : Locale->wday[t->tm_wday],
					pt, ptlim);
				continue;
			case 'B':
				pt = _add((t->tm_mon < 0 ||
					t->tm_mon >= MONSPERYEAR) ?
					"?" : Locale->month[t->tm_mon],
					pt, ptlim);
				continue;
			case 'b':
			case 'h':
				pt = _add((t->tm_mon < 0 ||
					t->tm_mon >= MONSPERYEAR) ?
					"?" : Locale->mon[t->tm_mon],
					pt, ptlim);
				continue;
			case 'C':
				/*
				** %C used to do a...
				**	_fmt("%a %b %e %X %Y", t);
				** ...whereas now POSIX 1003.2 calls for
				** something completely different.
				** (ado, 1993-05-24)
				*/
				pt = _yconv(t->tm_year, TM_YEAR_BASE, 1, 0,
					pt, ptlim);
				continue;
			case 'c':
				{
				int warn2 = IN_SOME;

				pt = _fmt(Locale->c_fmt, t, pt, ptlim, &warn2);
				if (warn2 == IN_ALL)
					warn2 = IN_THIS;
				if (warn2 > *warnp)
					*warnp = warn2;
				}
				continue;
			case 'D':
				pt = _fmt("%m/%d/%y", t, pt, ptlim, warnp);
				continue;
			case 'd':
				pt = _conv(t->tm_mday, "%02d", pt, ptlim);
				continue;
			case 'E':
			case 'O':
				/*
				** C99 locale modifiers.
				** The sequences
				**	%Ec %EC %Ex %EX %Ey %EY
				**	%Od %oe %OH %OI %Om %OM
				**	%OS %Ou %OU %OV %Ow %OW %Oy
				** are supposed to provide alternate
				** representations.
				*/
				goto label;
			case 'e':
				pt = _conv(t->tm_mday, "%2d", pt, ptlim);
				continue;
			case 'F':
				pt = _fmt("%Y-%m-%d", t, pt, ptlim, warnp);
				continue;
			case 'H':
				pt = _conv(t->tm_hour, "%02d", pt, ptlim);
				continue;
			case 'I':
				pt = _conv((t->tm_hour % 12) ?
					(t->tm_hour % 12) : 12,
					"%02d", pt, ptlim);
				continue;
			case 'j':
				pt = _conv(t->tm_yday + 1, "%03d", pt, ptlim);
				continue;
			case 'k':
				/*
				** This used to be...
				**	_conv(t->tm_hour % 12 ?
				**		t->tm_hour % 12 : 12, 2, ' ');
				** ...and has been changed to the below to
				** match SunOS 4.1.1 and Arnold Robbins'
				** strftime version 3.0. That is, "%k" and
				** "%l" have been swapped.
				** (ado, 1993-05-24)
				*/
				pt = _conv(t->tm_hour, "%2d", pt, ptlim);
				continue;
#ifdef KITCHEN_SINK
			case 'K':
				/*
				** After all this time, still unclaimed!
				*/
				pt = _add("kitchen sink", pt, ptlim);
				continue;
#endif /* defined KITCHEN_SINK */
			case 'l':
				/*
				** This used to be...
				**	_conv(t->tm_hour, 2, ' ');
				** ...and has been changed to the below to
				** match SunOS 4.1.1 and Arnold Robbin's
				** strftime version 3.0. That is, "%k" and
				** "%l" have been swapped.
				** (ado, 1993-05-24)
				*/
				pt = _conv((t->tm_hour % 12) ?
					(t->tm_hour % 12) : 12,
					"%2d", pt, ptlim);
				continue;
			case 'M':
				pt = _conv(t->tm_min, "%02d", pt, ptlim);
				continue;
			case 'm':
				pt = _conv(t->tm_mon + 1, "%02d", pt, ptlim);
				continue;
			case 'n':
				pt = _add("\n", pt, ptlim);
				continue;
			case 'p':
				pt = _add((t->tm_hour >= (HOURSPERDAY / 2)) ?
					Locale->pm :
					Locale->am,
					pt, ptlim);
				continue;
			case 'R':
				pt = _fmt("%H:%M", t, pt, ptlim, warnp);
				continue;
			case 'r':
				pt = _fmt("%I:%M:%S %p", t, pt, ptlim, warnp);
				continue;
			case 'S':
				pt = _conv(t->tm_sec, "%02d", pt, ptlim);
				continue;
			case 's':
				{
					struct tm	tm;
					char		buf[INT_STRLEN_MAXIMUM(
								time_t) + 1];
					time_t		mkt;

					tm = *t;
					mkt = mktime(&tm);
					if (TYPE_SIGNED(time_t))
						(void) sprintf(buf, "%"PRIdMAX,
							(intmax_t) mkt);
					else	(void) sprintf(buf, "%"PRIuMAX,
							(uintmax_t) mkt);
					pt = _add(buf, pt, ptlim);
				}
				continue;
			case 'T':
				pt = _fmt("%H:%M:%S", t, pt, ptlim, warnp);
				continue;
			case 't':
				pt = _add("\t", pt, ptlim);
				continue;
			case 'U':
				pt = _conv((t->tm_yday + DAYSPERWEEK -
					t->tm_wday) / DAYSPERWEEK,
					"%02d", pt, ptlim);
				continue;
			case 'u':
				/*
				** From Arnold Robbins' strftime version 3.0:
				** "ISO 8601: Weekday as a decimal number
				** [1 (Monday) - 7]"
				** (ado, 1993-05-24)
				*/
				pt = _conv((t->tm_wday == 0) ?
					DAYSPERWEEK : t->tm_wday,
					"%d", pt, ptlim);
				continue;
			case 'V':	/* ISO 8601 week number */
			case 'G':	/* ISO 8601 year (four digits) */
			case 'g':	/* ISO 8601 year (two digits) */
/*
** From Arnold Robbins' strftime version 3.0: "the week number of the
** year (the first Monday as the first day of week 1) as a decimal number
** (01-53)."
** (ado, 1993-05-24)
**
** From "http://www.ft.uni-erlangen.de/~mskuhn/iso-time.html" by Markus Kuhn:
** "Week 01 of a year is per definition the first week which has the
** Thursday in this year, which is equivalent to the week which contains
** the fourth day of January. In other words, the first week of a new year
** is the week which has the majority of its days in the new year. Week 01
** might also contain days from the previous year and the week before week
** 01 of a year is the last week (52 or 53) of the previous year even if
** it contains days from the new year. A week starts with Monday (day 1)
** and ends with Sunday (day 7). For example, the first week of the year
** 1997 lasts from 1996-12-30 to 1997-01-05..."
** (ado, 1996-01-02)
*/
				{
					int	year;
					int	base;
					int	yday;
					int	wday;
					int	w;

					year = t->tm_year;
					base = TM_YEAR_BASE;
					yday = t->tm_yday;
					wday = t->tm_wday;
					for ( ; ; ) {
						int	len;
						int	bot;
						int	top;

						len = isleap_sum(year, base) ?
							DAYSPERLYEAR :
							DAYSPERNYEAR;
						/*
						** What yday (-3 ... 3) does
						** the ISO year begin on?
						*/
						bot = ((yday + 11 - wday) %
							DAYSPERWEEK) - 3;
						/*
						** What yday does the NEXT
						** ISO year begin on?
						*/
						top = bot -
							(len % DAYSPERWEEK);
						if (top < -3)
							top += DAYSPERWEEK;
						top += len;
						if (yday >= top) {
							++base;
							w = 1;
							break;
						}
						if (yday >= bot) {
							w = 1 + ((yday - bot) /
								DAYSPERWEEK);
							break;
						}
						--base;
						yday += isleap_sum(year, base) ?
							DAYSPERLYEAR :
							DAYSPERNYEAR;
					}
#ifdef XPG4_1994_04_09
					if ((w == 52 &&
						t->tm_mon == TM_JANUARY) ||
						(w == 1 &&
						t->tm_mon == TM_DECEMBER))
							w = 53;
#endif /* defined XPG4_1994_04_09 */
					if (*format == 'V')
						pt = _conv(w, "%02d",
							pt, ptlim);
					else if (*format == 'g') {
						*warnp = IN_ALL;
						pt = _yconv(year, base, 0, 1,
							pt, ptlim);
					} else	pt = _yconv(year, base, 1, 1,
							pt, ptlim);
				}
				continue;
			case 'v':
				/*
				** From Arnold Robbins' strftime version 3.0:
				** "date as dd-bbb-YYYY"
				** (ado, 1993-05-24)
				*/
				pt = _fmt("%e-%b-%Y", t, pt, ptlim, warnp);
				continue;
			case 'W':
				pt = _conv((t->tm_yday + DAYSPERWEEK -
					(t->tm_wday ?
					(t->tm_wday - 1) :
					(DAYSPERWEEK - 1))) / DAYSPERWEEK,
					"%02d", pt, ptlim);
				continue;
			case 'w':
				pt = _conv(t->tm_wday, "%d", pt, ptlim);
				continue;
			case 'X':
				pt = _fmt(Locale->X_fmt, t, pt, ptlim, warnp);
				continue;
			case 'x':
				{
				int	warn2 = IN_SOME;

				pt = _fmt(Locale->x_fmt, t, pt, ptlim, &warn2);
				if (warn2 == IN_ALL)
					warn2 = IN_THIS;
				if (warn2 > *warnp)
					*warnp = warn2;
				}
				continue;
			case 'y':
				*warnp = IN_ALL;
				pt = _yconv(t->tm_year, TM_YEAR_BASE, 0, 1,
					pt, ptlim);
				continue;
			case 'Y':
				pt = _yconv(t->tm_year, TM_YEAR_BASE, 1, 1,
					pt, ptlim);
				continue;
			case 'Z':
#ifdef TM_ZONE
				if (t->TM_ZONE != NULL)
					pt = _add(t->TM_ZONE, pt, ptlim);
				else
#endif /* defined TM_ZONE */
				if (t->tm_isdst >= 0)
					pt = _add(tzname[t->tm_isdst != 0],
						pt, ptlim);
				/*
				** C99 says that %Z must be replaced by the
				** empty string if the time zone is not
				** determinable.
				*/
				continue;
			case 'z':
				{
				long		diff;
				char const *	sign;

				if (t->tm_isdst < 0)
					continue;
#ifdef TM_GMTOFF
				diff = t->TM_GMTOFF;
#else /* !defined TM_GMTOFF */
				/*
				** C99 says that the UT offset must
				** be computed by looking only at
				** tm_isdst. This requirement is
				** incorrect, since it means the code
				** must rely on magic (in this case
				** altzone and timezone), and the
				** magic might not have the correct
				** offset. Doing things correctly is
				** tricky and requires disobeying C99;
				** see GNU C strftime for details.
				** For now, punt and conform to the
				** standard, even though it's incorrect.
				**
				** C99 says that %z must be replaced by the
				** empty string if the time zone is not
				** determinable, so output nothing if the
				** appropriate variables are not available.
				*/
				if (t->tm_isdst == 0)
#ifdef USG_COMPAT
					diff = -timezone;
#else /* !defined USG_COMPAT */
					continue;
#endif /* !defined USG_COMPAT */
				else
#ifdef ALTZONE
					diff = -altzone;
#else /* !defined ALTZONE */
					continue;
#endif /* !defined ALTZONE */
#endif /* !defined TM_GMTOFF */
				if (diff < 0) {
					sign = "-";
					diff = -diff;
				} else	sign = "+";
				pt = _add(sign, pt, ptlim);
				diff /= SECSPERMIN;
				diff = (diff / MINSPERHOUR) * 100 +
					(diff % MINSPERHOUR);
				pt = _conv(diff, "%04d", pt, ptlim);
				}
				continue;
			case '+':
				pt = _fmt(Locale->date_fmt, t, pt, ptlim,
					warnp);
				continue;
			case '%':
			/*
			** X311J/88-090 (4.12.3.5): if conversion char is
			** undefined, behavior is undefined. Print out the
			** character itself as printf(3) also does.
			*/
			default:
				break;
			}
		}
コード例 #6
0
ファイル: strftime.c プロジェクト: Bgods/r-source
static char *
_fmt(const char *format, const stm *const t, char * pt, const char *const ptlim)
{
    for ( ; *format; ++format) {
	if (*format == '%') {
	    /* Check for POSIX 2008 / GNU modifiers */
	    char pad = '\0'; const char *f; int width = -1;
	    // first look to see if this is %..Y
	    for (f = format+1; *f ; f++)
		if(! (isdigit(*f) || *f == '_' || *f == '+')) break;
	    if (*f == 'Y')  {
		while (1)
		{
		    switch (*++format) {
		    case '0':
		    case '+': // pad with zeroes, and more (not here)
		    case '_': // pad with spaces: GNU extension
			pad = *format;
			continue;
		    default:
			break;
		    }
		    break;
		}
		if (isdigit (*format))
		{
		    width = 0;
		    do
		    {
			if (width > INT_MAX / 10 || 
			    (width == INT_MAX / 10 && *format - '0' > INT_MAX % 10))
			    width = INT_MAX;
			else {width *= 10; width += *format - '0';}
			format++;
		    }
		    while (isdigit (*format));
		}
		--format;
	    }

	label:
	    switch (*++format) {
	    case '\0':
		--format;
		break;

		/* now the locale-dependent cases */
#ifdef HAVE_NL_LANGINFO
	    case 'A':
		pt = _add((t->tm_wday < 0 || t->tm_wday >= 7) ?
			  "?" : nl_langinfo(DAY_1 + t->tm_wday),
			  pt, ptlim);
		continue;
	    case 'a':
		pt = _add((t->tm_wday < 0 || t->tm_wday >= 7) ?
			  "?" : nl_langinfo(ABDAY_1 + t->tm_wday),
			  pt, ptlim);
		continue;
	    case 'B':
		pt = _add((t->tm_mon < 0 || t->tm_mon >= 12) ?
			  "?" : nl_langinfo(MON_1 + t->tm_mon),
			  pt, ptlim);
		continue;
	    case 'b':
	    case 'h':
		pt = _add((t->tm_mon < 0 || t->tm_mon >= 12) ?
			  "?" : nl_langinfo(ABMON_1 + t->tm_mon),
			  pt, ptlim);
		continue;
	    case 'c':
		pt = _fmt(nl_langinfo(D_T_FMT), t, pt, ptlim);
		continue;
	    case 'p':
		pt = _add(nl_langinfo(t->tm_hour < 12 ? AM_STR : PM_STR),
			  pt, ptlim);
		continue;
	    case 'P':
		{
		    char *p = nl_langinfo(t->tm_hour < 12 ? AM_STR : PM_STR),
			*q, buff[20];
		    for (q = buff; *p; ) *q++ = (char) tolower(*p++);
		    *q = '\0'; 
		    pt = _add(buff, pt, ptlim);
		}
		continue;
	    case 'X':
		pt = _fmt(nl_langinfo(T_FMT), t, pt, ptlim);
		continue;
	    case 'x':
		pt = _fmt(nl_langinfo(D_FMT), t, pt, ptlim);
		continue;
#else
	    case 'A':
		pt = _add((t->tm_wday < 0 || t->tm_wday >= 7) ?
			  "?" : orig("%A", t),
			  pt, ptlim);
		continue;
	    case 'a':
		pt = _add((t->tm_wday < 0 || t->tm_wday >= 7) ?
			  "?" : orig("%a", t),
			  pt, ptlim);
		continue;
	    case 'B':
		pt = _add((t->tm_mon < 0 || t->tm_mon >= 12) ?
			  "?" : orig("%B", t),
			  pt, ptlim);
		continue;
	    case 'b':
	    case 'h':
		pt = _add((t->tm_mon < 0 || t->tm_mon >= 12) ?
			  "?" : orig("%b", t),
			  pt, ptlim);
		continue;
	    case 'c':
		// In a C locale this is supposed to be
		// "%a %b %e %T %Y". It is not on Windows ....
#ifdef _WIN32
		pt = _fmt("%a %b %e %T %Y", t, pt, ptlim);
#else
		pt = _fmt(orig("%c", t), t, pt, ptlim);
#endif
		continue;
	    case 'p':
		pt = _add(orig("%p", t), pt, ptlim);
		continue;
	    case 'P':
		{
		    char *p = orig("%p", t), *q, buff[20];
		    for (q = buff; *p; ) *q++ = (char) tolower(*p++);
		    *q = '\0'; 
		    pt = _add(buff, pt, ptlim);
		}
		continue;
	    case 'X':
		pt = _fmt(orig("%X", t), t, pt, ptlim);
		continue;
	    case 'x':
		pt = _fmt(orig("%x", t), t, pt, ptlim);
		continue;
#endif
		/* now the locale-independent ones */
	    case 'C':
		pt = _yconv(t->tm_year, TM_YEAR_BASE, 1, 0, pt, ptlim);
		continue;
	    case 'D':
		pt = _fmt("%m/%d/%y", t, pt, ptlim);
		continue;
	    case 'd':
		pt = _conv(t->tm_mday, "%02d", pt, ptlim);
		continue;
	    case 'E':
	    case 'O':
		/*
		** C99 locale modifiers.
		** The sequences
		**	%Ec %EC %Ex %EX %Ey %EY
		**	%Od %oe %OH %OI %Om %OM
		**	%OS %Ou %OU %OV %Ow %OW %Oy
		** are supposed to provide alternate
		** representations.
		*/
		goto label;
	    case 'e':
		pt = _conv(t->tm_mday, "%2d", pt, ptlim);
		continue;
	    case 'F':
		pt = _fmt("%Y-%m-%d", t, pt, ptlim);
		continue;
	    case 'H':
		pt = _conv(t->tm_hour, "%02d", pt, ptlim);
		continue;
	    case 'I':
		pt = _conv((t->tm_hour % 12) ? (t->tm_hour % 12) : 12,
			   "%02d", pt, ptlim);
		continue;
	    case 'j':
		pt = _conv(t->tm_yday + 1, "%03d", pt, ptlim);
		continue;
	    case 'k':
		pt = _conv(t->tm_hour, "%2d", pt, ptlim);
		continue;
	    case 'l':
		pt = _conv((t->tm_hour % 12) ? (t->tm_hour % 12) : 12,
			   "%2d", pt, ptlim);
		continue;
	    case 'M':
		pt = _conv(t->tm_min, "%02d", pt, ptlim);
		continue;
	    case 'm':
		pt = _conv(t->tm_mon + 1, "%02d", pt, ptlim);
		continue;
	    case 'n':
		pt = _add("\n", pt, ptlim);
		continue;
	    case 'R':
		pt = _fmt("%H:%M", t, pt, ptlim);
		continue;
	    case 'r':
		pt = _fmt("%I:%M:%S %p", t, pt, ptlim);
		continue;
	    case 'S':
		pt = _conv(t->tm_sec, "%02d", pt, ptlim);
		continue;
	    case 's':
	    {
		stm  tm = *t;
		char buf[22]; // <= 19 digs + sign + terminator
		int_fast64_t mkt = R_mktime(&tm);
#ifdef _WIN32
		// not ISO C99, so warns
		(void) snprintf(buf, 22, "%I64d", mkt);
#else
		(void) snprintf(buf, 22, "%lld", (long long) mkt);
#endif
		pt = _add(buf, pt, ptlim);
	    }
	    continue;
	    case 'T':
		pt = _fmt("%H:%M:%S", t, pt, ptlim);
		continue;
	    case 't':
		pt = _add("\t", pt, ptlim);
		continue;
	    case 'U':
		pt = _conv((t->tm_yday + 7 - t->tm_wday) / 7,
			   "%02d", pt, ptlim);
		continue;
	    case 'u':
		pt = _conv((t->tm_wday == 0) ? 7 : t->tm_wday, "%d", pt, ptlim);
		continue;
	    case 'V':	/* ISO 8601 week number */
	    case 'G':	/* ISO 8601 year (four digits) */
	    case 'g':	/* ISO 8601 year (two digits) */
	    {
		int year, base, yday, wday, w;

		year = t->tm_year;
		base = TM_YEAR_BASE;
		yday = t->tm_yday;
		wday = t->tm_wday;
		for ( ; ; ) {
		    int	len, bot, top;

		    len = isleap_sum(year, base) ? DAYSPERLYEAR : DAYSPERNYEAR;
		    /*
		    ** What yday (-3 ... 3) does
		    ** the ISO year begin on?
		    */
		    bot = ((yday + 11 - wday) % 7) - 3;
		    /*
		    ** What yday does the NEXT
		    ** ISO year begin on?
		    */
		    top = bot - (len % 7);
		    if (top < -3)
			top += 7;
		    top += len;
		    if (yday >= top) {
			++base;
			w = 1;
			break;
		    }
		    if (yday >= bot) {
			w = 1 + ((yday - bot) / 7);
			break;
		    }
		    --base;
		    yday += isleap_sum(year, base) ? DAYSPERLYEAR : DAYSPERNYEAR;
		}
		if (*format == 'V')
		    pt = _conv(w, "%02d", pt, ptlim);
		else if (*format == 'g')
		    pt = _yconv(year, base, 0, 1, pt, ptlim);
		else // %G
		    pt = _yconv(year, base, 1, 1, pt, ptlim);
	    }
	    continue;
	    case 'v':
		pt = _fmt("%e-%b-%Y", t, pt, ptlim);
		continue;
	    case 'W':
		pt = _conv((t->tm_yday + 7 -
			    (t->tm_wday ? (t->tm_wday - 1) : (7 - 1))) / 7,
			   "%02d", pt, ptlim);
		continue;
	    case 'w':
		pt = _conv(t->tm_wday, "%d", pt, ptlim);
		continue;
	    case 'y':
		pt = _yconv(t->tm_year, TM_YEAR_BASE, 0, 1, pt, ptlim);
		continue;
	    case 'Y':
//		pt = _yconv(t->tm_year, TM_YEAR_BASE, 1, 1, pt, ptlim);
	    {
		char buf[20] = "%";
		int year = TM_YEAR_BASE + t->tm_year;
		if (pad == '\0') { pad = '0'; width = 4;}
		if (pad == '0' || pad == '+') strcat(buf, "0");
		if (width > 0) sprintf(buf+strlen(buf), "%u", width);
		if (pad == '+' && year > 9999) strcat(buf, "+");
		strcat(buf, "d");
		pt = _conv(year, buf, pt, ptlim);
	    }
	    continue;
	    case 'Z':
#ifdef HAVE_TM_ZONE
		if (t->tm_zone != NULL)
		    pt = _add(t->tm_zone, pt, ptlim);
		else
#endif
		if (t->tm_isdst >= 0)
		    pt = _add(R_tzname[t->tm_isdst != 0], pt, ptlim);
		/*
		** C99 says that %Z must be replaced by the
		** empty string if the time zone is not
		** determinable.
		*/
		continue;
	    case 'z':
	    {
		long  diff;
		char const *sign;

		if (t->tm_isdst < 0)
		    continue;
#ifdef HAVE_TM_GMTOFF
		diff = t->tm_gmtoff;
#else
		diff = R_timegm(t) - R_mktime(t);
#endif
		if (diff < 0) {
		    sign = "-";
		    diff = -diff;
		} else	sign = "+";
		pt = _add(sign, pt, ptlim);
		diff /= SECSPERMIN;
		diff = (diff / MINSPERHOUR) * 100 + (diff % MINSPERHOUR);
		pt = _conv((int) diff, "%04d", pt, ptlim);
	    }
	    continue;
	    case '+':
		pt = _fmt("%a %b %e %H:%M:%S %Z %Y", t, pt, ptlim);
		continue;
	    case '%':
	    default:
		break;
	    }
	}
	if (pt == ptlim)
	    break;
	*pt++ = *format;
    }
    return pt;
}
コード例 #7
0
static char *
_fmt(const char * format, const struct tm * const t, char * pt, const char * const ptlim, int * warnp)
{
    for ( ; *format; ++format) {
        if (*format == '%') {
label:
            switch (*++format) {
            case '\0':
                --format;
                break;
            case 'A':
                pt = _add((t->tm_wday < 0 ||
                           t->tm_wday >= DAYSPERWEEK) ?
                          "?" : Locale->weekday[t->tm_wday],
                          pt, ptlim);
                continue;
            case 'a':
                pt = _add((t->tm_wday < 0 ||
                           t->tm_wday >= DAYSPERWEEK) ?
                          "?" : Locale->wday[t->tm_wday],
                          pt, ptlim);
                continue;
            case 'B':
                pt = _add((t->tm_mon < 0 ||
                           t->tm_mon >= MONSPERYEAR) ?
                          "?" : Locale->month[t->tm_mon],
                          pt, ptlim);
                continue;
            case 'b':
            case 'h':
                pt = _add((t->tm_mon < 0 ||
                           t->tm_mon >= MONSPERYEAR) ?
                          "?" : Locale->mon[t->tm_mon],
                          pt, ptlim);
                continue;
            case 'C':
                /*
                ** %C used to do a...
                **	_fmt("%a %b %e %X %Y", t);
                ** ...whereas now POSIX 1003.2 calls for
                ** something completely different.
                ** (ado, 1993-05-24)
                */
                pt = _conv((t->tm_year + TM_YEAR_BASE) / 100,
                           "%02d", pt, ptlim);
                continue;
            case 'c':
            {
                int warn2 = IN_SOME;

                pt = _fmt(Locale->c_fmt, t, pt, ptlim, warnp);
                if (warn2 == IN_ALL)
                    warn2 = IN_THIS;
                if (warn2 > *warnp)
                    *warnp = warn2;
            }
            continue;
            case 'D':
                pt = _fmt("%m/%d/%y", t, pt, ptlim, warnp);
                continue;
            case 'd':
                pt = _conv(t->tm_mday, "%02d", pt, ptlim);
                continue;
            case 'E':
            case 'O':
                /*
                ** C99 locale modifiers.
                ** The sequences
                **	%Ec %EC %Ex %EX %Ey %EY
                **	%Od %oe %OH %OI %Om %OM
                **	%OS %Ou %OU %OV %Ow %OW %Oy
                ** are supposed to provide alternate
                ** representations.
                */
                goto label;
            case 'e':
                pt = _conv(t->tm_mday, "%2d", pt, ptlim);
                continue;
            case 'F':
                pt = _fmt("%Y-%m-%d", t, pt, ptlim, warnp);
                continue;
            case 'H':
                pt = _conv(t->tm_hour, "%02d", pt, ptlim);
                continue;
            case 'I':
                pt = _conv((t->tm_hour % 12) ?
                           (t->tm_hour % 12) : 12,
                           "%02d", pt, ptlim);
                continue;
            case 'j':
                pt = _conv(t->tm_yday + 1, "%03d", pt, ptlim);
                continue;
            case 'k':
                /*
                ** This used to be...
                **	_conv(t->tm_hour % 12 ?
                **		t->tm_hour % 12 : 12, 2, ' ');
                ** ...and has been changed to the below to
                ** match SunOS 4.1.1 and Arnold Robbins'
                ** strftime version 3.0.  That is, "%k" and
                ** "%l" have been swapped.
                ** (ado, 1993-05-24)
                */
                pt = _conv(t->tm_hour, "%2d", pt, ptlim);
                continue;
#ifdef KITCHEN_SINK
            case 'K':
                /*
                ** After all this time, still unclaimed!
                */
                pt = _add("kitchen sink", pt, ptlim);
                continue;
#endif /* defined KITCHEN_SINK */
            case 'l':
                /*
                ** This used to be...
                **	_conv(t->tm_hour, 2, ' ');
                ** ...and has been changed to the below to
                ** match SunOS 4.1.1 and Arnold Robbin's
                ** strftime version 3.0.  That is, "%k" and
                ** "%l" have been swapped.
                ** (ado, 1993-05-24)
                */
                pt = _conv((t->tm_hour % 12) ?
                           (t->tm_hour % 12) : 12,
                           "%2d", pt, ptlim);
                continue;
            case 'M':
                pt = _conv(t->tm_min, "%02d", pt, ptlim);
                continue;
            case 'm':
                pt = _conv(t->tm_mon + 1, "%02d", pt, ptlim);
                continue;
            case 'n':
                pt = _add("\n", pt, ptlim);
                continue;
            case 'p':
                pt = _add((t->tm_hour >= (HOURSPERDAY / 2)) ?
                          Locale->pm :
                          Locale->am,
                          pt, ptlim);
                continue;
            case 'R':
                pt = _fmt("%H:%M", t, pt, ptlim, warnp);
                continue;
            case 'r':
                pt = _fmt("%I:%M:%S %p", t, pt, ptlim, warnp);
                continue;
            case 'S':
                pt = _conv(t->tm_sec, "%02d", pt, ptlim);
                continue;
            case 's':
            {
                struct tm	tm;
                char		buf[INT_STRLEN_MAXIMUM(
                                    time_t) + 1];
                time_t		mkt;

                tm = *t;
                mkt = mktime(&tm);
                if (TYPE_SIGNED(time_t))
                    (void) _snprintf(buf, sizeof buf,
                                     "%ld", (long) mkt);
                else	(void) _snprintf(buf, sizeof buf,
                                             "%lu", (unsigned long) mkt);
                pt = _add(buf, pt, ptlim);
            }
            continue;
            case 'T':
                pt = _fmt("%H:%M:%S", t, pt, ptlim, warnp);
                continue;
            case 't':
                pt = _add("\t", pt, ptlim);
                continue;
            case 'U':
                pt = _conv((t->tm_yday + DAYSPERWEEK -
                            t->tm_wday) / DAYSPERWEEK,
                           "%02d", pt, ptlim);
                continue;
            case 'u':
                /*
                ** From Arnold Robbins' strftime version 3.0:
                ** "ISO 8601: Weekday as a decimal number
                ** [1 (Monday) - 7]"
                ** (ado, 1993-05-24)
                */
                pt = _conv((t->tm_wday == 0) ?
                           DAYSPERWEEK : t->tm_wday,
                           "%d", pt, ptlim);
                continue;
            case 'V':	/* ISO 8601 week number */
            case 'G':	/* ISO 8601 year (four digits) */
            case 'g':	/* ISO 8601 year (two digits) */
            {
                int	year;
                int	yday;
                int	wday;
                int	w;

                year = t->tm_year + TM_YEAR_BASE;
                yday = t->tm_yday;
                wday = t->tm_wday;
                for ( ; ; ) {
                    int	len;
                    int	bot;
                    int	top;

                    len = isleap(year) ?
                          DAYSPERLYEAR :
                          DAYSPERNYEAR;
                    /*
                    ** What yday (-3 ... 3) does
                    ** the ISO year begin on?
                    */
                    bot = ((yday + 11 - wday) %
                           DAYSPERWEEK) - 3;
                    /*
                    ** What yday does the NEXT
                    ** ISO year begin on?
                    */
                    top = bot -
                          (len % DAYSPERWEEK);
                    if (top < -3)
                        top += DAYSPERWEEK;
                    top += len;
                    if (yday >= top) {
                        ++year;
                        w = 1;
                        break;
                    }
                    if (yday >= bot) {
                        w = 1 + ((yday - bot) /
                                 DAYSPERWEEK);
                        break;
                    }
                    --year;
                    yday += isleap(year) ?
                            DAYSPERLYEAR :
                            DAYSPERNYEAR;
                }
                if (*format == 'V')
                    pt = _conv(w, "%02d",
                               pt, ptlim);
                else if (*format == 'g') {
                    *warnp = IN_ALL;
                    pt = _conv(year % 100, "%02d",
                               pt, ptlim);
                } else	pt = _conv(year, "%04d",
                                       pt, ptlim);
            }
            continue;
            case 'v':
                pt = _fmt("%e-%b-%Y", t, pt, ptlim, warnp);
                continue;
            case 'W':
                pt = _conv((t->tm_yday + DAYSPERWEEK -
                            (t->tm_wday ?
                             (t->tm_wday - 1) :
                             (DAYSPERWEEK - 1))) / DAYSPERWEEK,
                           "%02d", pt, ptlim);
                continue;
            case 'w':
                pt = _conv(t->tm_wday, "%d", pt, ptlim);
                continue;
            case 'X':
                pt = _fmt(Locale->X_fmt, t, pt, ptlim, warnp);
                continue;
            case 'x':
            {
                int	warn2 = IN_SOME;

                pt = _fmt(Locale->x_fmt, t, pt, ptlim, &warn2);
                if (warn2 == IN_ALL)
                    warn2 = IN_THIS;
                if (warn2 > *warnp)
                    *warnp = warn2;
            }
            continue;
            case 'y':
                *warnp = IN_ALL;
                pt = _conv((t->tm_year + TM_YEAR_BASE) % 100,
                           "%02d", pt, ptlim);
                continue;
            case 'Y':
                pt = _conv(t->tm_year + TM_YEAR_BASE, "%04d",
                           pt, ptlim);
                continue;
            case 'Z':
                if (t->tm_isdst >= 0)
                    pt = _add(tzname[t->tm_isdst != 0],
                              pt, ptlim);
                /*
                ** C99 says that %Z must be replaced by the
                ** empty string if the time zone is not
                ** determinable.
                */
                continue;
            case 'z':
            {
                int		diff;
                char const *	sign;

                if (t->tm_isdst < 0)
                    continue;
                continue;
                if (diff < 0) {
                    sign = "-";
                    diff = -diff;
                } else	sign = "+";
                pt = _add(sign, pt, ptlim);
                diff /= 60;
                pt = _conv((diff/60)*100 + diff%60,
                           "%04d", pt, ptlim);
            }
            continue;
            case '+':
                pt = _fmt(Locale->date_fmt, t, pt, ptlim,
                          warnp);
                continue;
            case '%':
            default:
                break;
            }
        }
        if (pt == ptlim)
            break;
        *pt++ = *format;
    }
    return pt;
}
コード例 #8
0
ファイル: strftime.c プロジェクト: ksherlock/gno
static char *
_fmt(const char *format, const struct tm *t, char *pt,
     const char *ptlim)
{
    int Ealternative, Oalternative, PadIndex;
#ifdef __ORCAC__
#define tptr (&_C_time_locale)
#else
    struct lc_time_T *tptr = __get_current_time_locale(__get_locale());
#endif

    for ( ; *format; ++format) {
        if (*format == '%') {
            Ealternative = 0;
            Oalternative = 0;
            PadIndex	 = PAD_DEFAULT;
label:
            switch (*++format) {
            case '\0':
                --format;
                break;
            case 'A':
                pt = _add((t->tm_wday < 0 || t->tm_wday >= DAYSPERWEEK) ?
                          "?" : tptr->weekday[t->tm_wday],
                          pt, ptlim);
                continue;
            case 'a':
                pt = _add((t->tm_wday < 0 || t->tm_wday >= DAYSPERWEEK) ?
                          "?" : tptr->wday[t->tm_wday],
                          pt, ptlim);
                continue;
            case 'B':
                pt = _add((t->tm_mon < 0 ||
                           t->tm_mon >= MONSPERYEAR) ?
                          "?" : (Oalternative ? tptr->alt_month :
                                 tptr->month)[t->tm_mon],
                          pt, ptlim);
                continue;

                continue;
            case 'b':
            case 'h':
                pt = _add((t->tm_mon < 0 || t->tm_mon >= MONSPERYEAR) ?
                          "?" : tptr->mon[t->tm_mon],
                          pt, ptlim);
                continue;
            case 'C':
                /*
                 * %C used to do a...
                 *	_fmt("%a %b %e %X %Y", t);
                 * ...whereas now POSIX 1003.2 calls for
                 * something completely different.
                 * (ado, 1993-05-24)
                 */
                pt = _yconv(t->tm_year, TM_YEAR_BASE, 1, 0,
                            pt, ptlim);
                continue;
            case 'c':
                pt = _fmt(tptr->c_fmt, t, pt, ptlim);
                continue;
            case 'D':
                pt = _fmt("%m/%d/%y", t, pt, ptlim);
                continue;
            case 'd':
                pt = _conv(t->tm_mday,
                           fmt_padding[PAD_FMT_DAYOFMONTH][PadIndex],
                           pt, ptlim);
                continue;
            case 'E':
                if (Ealternative || Oalternative)
                    break;
                Ealternative++;
                goto label;
            case 'O':
                /*
                 * C99 locale modifiers.
                 * The sequences
                 *	%Ec %EC %Ex %EX %Ey %EY
                 *	%Od %oe %OH %OI %Om %OM
                 *	%OS %Ou %OU %OV %Ow %OW %Oy
                 * are supposed to provide alternate
                 * representations.
                 *
                 * FreeBSD extension
                 *      %OB
                 */
                if (Ealternative || Oalternative)
                    break;
                Oalternative++;
                goto label;
            case 'e':
                pt = _conv(t->tm_mday,
                           fmt_padding[PAD_FMT_SDAYOFMONTH][PadIndex],
                           pt, ptlim);
                continue;
            case 'F':
                pt = _fmt("%Y-%m-%d", t, pt, ptlim);
                continue;
            case 'H':
                pt = _conv(t->tm_hour, fmt_padding[PAD_FMT_HMS][PadIndex],
                           pt, ptlim);
                continue;
            case 'I':
                pt = _conv((t->tm_hour % 12) ?
                           (t->tm_hour % 12) : 12,
                           fmt_padding[PAD_FMT_HMS][PadIndex],
                           pt, ptlim);
                continue;
            case 'j':
                pt = _conv(t->tm_yday + 1,
                           fmt_padding[PAD_FMT_DAYOFYEAR][PadIndex],
                           pt, ptlim);
                continue;
            case 'k':
                /*
                 * This used to be...
                 *	_conv(t->tm_hour % 12 ?
                 *		t->tm_hour % 12 : 12, 2, ' ');
                 * ...and has been changed to the below to
                 * match SunOS 4.1.1 and Arnold Robbins'
                 * strftime version 3.0. That is, "%k" and
                 * "%l" have been swapped.
                 * (ado, 1993-05-24)
                 */
                pt = _conv(t->tm_hour, fmt_padding[PAD_FMT_SHMS][PadIndex],
                           pt, ptlim);
                continue;
#ifdef KITCHEN_SINK
            case 'K':
                /*
                ** After all this time, still unclaimed!
                */
                pt = _add("kitchen sink", pt, ptlim);
                continue;
#endif /* defined KITCHEN_SINK */
            case 'l':
                /*
                 * This used to be...
                 *	_conv(t->tm_hour, 2, ' ');
                 * ...and has been changed to the below to
                 * match SunOS 4.1.1 and Arnold Robbin's
                 * strftime version 3.0. That is, "%k" and
                 * "%l" have been swapped.
                 * (ado, 1993-05-24)
                 */
                pt = _conv((t->tm_hour % 12) ?
                           (t->tm_hour % 12) : 12,
                           fmt_padding[PAD_FMT_SHMS][PadIndex],
                           pt, ptlim);
                continue;
            case 'M':
                pt = _conv(t->tm_min, fmt_padding[PAD_FMT_HMS][PadIndex],
                           pt, ptlim);
                continue;
            case 'm':
                pt = _conv(t->tm_mon + 1,
                           fmt_padding[PAD_FMT_MONTH][PadIndex],
                           pt, ptlim);
                continue;
            case 'n':
                pt = _add("\n", pt, ptlim);
                continue;
            case 'p':
                pt = _add((t->tm_hour >= (HOURSPERDAY / 2)) ?
                          tptr->pm : tptr->am,
                          pt, ptlim);
                continue;
            case 'R':
                pt = _fmt("%H:%M", t, pt, ptlim);
                continue;
            case 'r':
                pt = _fmt(tptr->ampm_fmt, t, pt, ptlim);
                continue;
            case 'S':
                pt = _conv(t->tm_sec, fmt_padding[PAD_FMT_HMS][PadIndex],
                           pt, ptlim);
                continue;
            case 's':
                pt = _secs(t, pt, ptlim);
                continue;
            case 'T':
                pt = _fmt("%H:%M:%S", t, pt, ptlim);
                continue;
            case 't':
                pt = _add("\t", pt, ptlim);
                continue;
            case 'U':
                pt = _conv((t->tm_yday + DAYSPERWEEK -
                            t->tm_wday) / DAYSPERWEEK,
                           fmt_padding[PAD_FMT_WEEKOFYEAR][PadIndex],
                           pt, ptlim);
                continue;
            case 'u':
                /*
                 * From Arnold Robbins' strftime version 3.0:
                 * "ISO 8601: Weekday as a decimal number
                 * [1 (Monday) - 7]"
                 * (ado, 1993-05-24)
                 */
                pt = _conv((t->tm_wday == 0) ?
                           DAYSPERWEEK : t->tm_wday,
                           "%d", pt, ptlim);
                continue;
            case 'V':	/* ISO 8601 week number */
            case 'G':	/* ISO 8601 year (four digits) */
            case 'g':	/* ISO 8601 year (two digits) */
                /*
                 * From Arnold Robbins' strftime version 3.0: "the week number of the
                 * year (the first Monday as the first day of week 1) as a decimal number
                 * (01-53)."
                 * (ado, 1993-05-24)
                 *
                 * From "http://www.ft.uni-erlangen.de/~mskuhn/iso-time.html" by Markus Kuhn:
                 * "Week 01 of a year is per definition the first week which has the
                 * Thursday in this year, which is equivalent to the week which contains
                 * the fourth day of January. In other words, the first week of a new year
                 * is the week which has the majority of its days in the new year. Week 01
                 * might also contain days from the previous year and the week before week
                 * 01 of a year is the last week (52 or 53) of the previous year even if
                 * it contains days from the new year. A week starts with Monday (day 1)
                 * and ends with Sunday (day 7). For example, the first week of the year
                 * 1997 lasts from 1996-12-30 to 1997-01-05..."
                 * (ado, 1996-01-02)
                 */
            {
                int	year;
                int	base;
                int	yday;
                int	wday;
                int	w;

                year = t->tm_year;
                base = TM_YEAR_BASE;
                yday = t->tm_yday;
                wday = t->tm_wday;
                for ( ; ; ) {
                    int	len;
                    int	bot;
                    int	top;

                    len = isleap_sum(year, base) ?
                          DAYSPERLYEAR :
                          DAYSPERNYEAR;
                    /*
                     * What yday (-3 ... 3) does
                     * the ISO year begin on?
                     */
                    bot = ((yday + 11 - wday) %
                           DAYSPERWEEK) - 3;
                    /*
                     * What yday does the NEXT
                     * ISO year begin on?
                     */
                    top = bot -
                          (len % DAYSPERWEEK);
                    if (top < -3)
                        top += DAYSPERWEEK;
                    top += len;
                    if (yday >= top) {
                        ++base;
                        w = 1;
                        break;
                    }
                    if (yday >= bot) {
                        w = 1 + ((yday - bot) /
                                 DAYSPERWEEK);
                        break;
                    }
                    --base;
                    yday += isleap_sum(year, base) ?
                            DAYSPERLYEAR :
                            DAYSPERNYEAR;
                }
#ifdef XPG4_1994_04_09
                if ((w == 52 &&
                        t->tm_mon == TM_JANUARY) ||
                        (w == 1 &&
                         t->tm_mon == TM_DECEMBER))
                    w = 53;
#endif /* defined XPG4_1994_04_09 */
                if (*format == 'V')
                    pt = _conv(w, fmt_padding[PAD_FMT_WEEKOFYEAR][PadIndex],
                               pt, ptlim);
                else if (*format == 'g') {
                    pt = _yconv(year, base, 0, 1,
                                pt, ptlim);
                } else	pt = _yconv(year, base, 1, 1,
                                        pt, ptlim);
            }
            continue;
            case 'v':
                /*
                 * From Arnold Robbins' strftime version 3.0:
                 * "date as dd-bbb-YYYY"
                 * (ado, 1993-05-24)
                 */
                pt = _fmt("%e-%b-%Y", t, pt, ptlim);
                continue;
            case 'W':
                pt = _conv((t->tm_yday + DAYSPERWEEK -
                            (t->tm_wday ?
                             (t->tm_wday - 1) :
                             (DAYSPERWEEK - 1))) / DAYSPERWEEK,
                           fmt_padding[PAD_FMT_WEEKOFYEAR][PadIndex],
                           pt, ptlim);
                continue;
            case 'w':
                pt = _conv(t->tm_wday, "%d", pt, ptlim);
                continue;
            case 'X':
                pt = _fmt(tptr->X_fmt, t, pt, ptlim);
                continue;
            case 'x':
                pt = _fmt(tptr->x_fmt, t, pt, ptlim);
                continue;
            case 'y':
                pt = _yconv(t->tm_year, TM_YEAR_BASE, 0, 1,
                            pt, ptlim);
                continue;
            case 'Y':
                pt = _yconv(t->tm_year, TM_YEAR_BASE, 1, 1,
                            pt, ptlim);
                continue;
            case 'Z':
#ifdef TM_ZONE
                if (t->TM_ZONE != NULL)
                    pt = _add(t->TM_ZONE, pt, ptlim);
                else
#endif /* defined TM_ZONE */
                    if (t->tm_isdst >= 0)
                        pt = _add(tzname[t->tm_isdst != 0],
                                  pt, ptlim);
                /*
                 * C99 says that %Z must be replaced by the
                 * empty string if the time zone is not
                 * determinable.
                 */
                continue;
            case 'z':
            {
                int		diff;
                char const *	sign;

                if (t->tm_isdst < 0)
                    continue;
#ifdef TM_GMTOFF
                diff = t->TM_GMTOFF;
#else /* !defined TM_GMTOFF */
                /*
                 * C99 says that the UTC offset must
                 * be computed by looking only at
                 * tm_isdst. This requirement is
                 * incorrect, since it means the code
                 * must rely on magic (in this case
                 * altzone and timezone), and the
                 * magic might not have the correct
                 * offset. Doing things correctly is
                 * tricky and requires disobeying C99;
                 * see GNU C strftime for details.
                 * For now, punt and conform to the
                 * standard, even though it's incorrect.
                 *
                 * C99 says that %z must be replaced by the
                 * empty string if the time zone is not
                 * determinable, so output nothing if the
                 * appropriate variables are not available.
                 */
                if (t->tm_isdst == 0)
#ifdef USG_COMPAT
                    diff = -timezone;
#else /* !defined USG_COMPAT */
                    continue;
#endif /* !defined USG_COMPAT */
                else
#ifdef ALTZONE
                    diff = -altzone;
#else /* !defined ALTZONE */
                    continue;
#endif /* !defined ALTZONE */
#endif /* !defined TM_GMTOFF */
                if (diff < 0) {
                    sign = "-";
                    diff = -diff;
                } else
                    sign = "+";
                pt = _add(sign, pt, ptlim);
                diff /= SECSPERMIN;
                diff = (diff / MINSPERHOUR) * 100 +
                       (diff % MINSPERHOUR);
                pt = _conv(diff,
                           fmt_padding[PAD_FMT_YEAR][PadIndex],
                           pt, ptlim);
            }
            continue;
            case '+':
                pt = _fmt(tptr->date_fmt, t, pt, ptlim);
                continue;
            case '-':
                if (PadIndex != PAD_DEFAULT)
                    break;
                PadIndex = PAD_LESS;
                goto label;
            case '_':
                if (PadIndex != PAD_DEFAULT)
                    break;
                PadIndex = PAD_SPACE;
                goto label;
            case '0':
                if (PadIndex != PAD_DEFAULT)
                    break;
                PadIndex = PAD_ZERO;
                goto label;
            case '%':
            /*
             * X311J/88-090 (4.12.3.5): if conversion char is
             * undefined, behavior is undefined. Print out the
             * character itself as printf(3) also does.
             */
            default:
                break;
            }
        }