* strftime.c (rb_strftime): supported flags and precision for most
conversions. [ruby-dev:35906] * lib/date/format.rb (Date#strftime): left-justifies %L and %N. [ruby-dev:35909] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@18773 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
5dd5311fdf
commit
ce2b7d3a58
@ -1,3 +1,11 @@
|
|||||||
|
Fri Aug 22 14:28:05 2008 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
|
* strftime.c (rb_strftime): supported flags and precision for most
|
||||||
|
conversions. [ruby-dev:35906]
|
||||||
|
|
||||||
|
* lib/date/format.rb (Date#strftime): left-justifies %L and %N.
|
||||||
|
[ruby-dev:35909]
|
||||||
|
|
||||||
Fri Aug 22 14:04:04 2008 Martin Duerst <duerst@it.aoyama.ac.jp>
|
Fri Aug 22 14:04:04 2008 Martin Duerst <duerst@it.aoyama.ac.jp>
|
||||||
|
|
||||||
* test/ruby/test_transcode.rb: test_shift_jis:
|
* test/ruby/test_transcode.rb: test_shift_jis:
|
||||||
|
@ -153,8 +153,8 @@ class Date
|
|||||||
s[0,0] = sign
|
s[0,0] = sign
|
||||||
end
|
end
|
||||||
|
|
||||||
if f[:p] != '-'
|
if f[:w]
|
||||||
s = s.rjust(f[:w], f[:p])
|
s = f[:n] ? s.ljust(f[:w], f[:p]) : s.rjust(f[:w], f[:p])
|
||||||
end
|
end
|
||||||
|
|
||||||
if f[:s] && f[:p] != "\s"
|
if f[:s] && f[:p] != "\s"
|
||||||
@ -257,12 +257,18 @@ class Date
|
|||||||
when 'j'; emit_n(yday, 3, f)
|
when 'j'; emit_n(yday, 3, f)
|
||||||
when 'k'; emit_a(hour, 2, f)
|
when 'k'; emit_a(hour, 2, f)
|
||||||
when 'L'
|
when 'L'
|
||||||
emit_n((sec_fraction / MILLISECONDS_IN_SECOND).floor, 3, f)
|
f[:n] = true
|
||||||
|
w = f[:w]
|
||||||
|
s = emit_n((sec_fraction / MILLISECONDS_IN_SECOND).floor, 3, f)
|
||||||
|
w ? s[0, w] : s
|
||||||
when 'l'; emit_a((hour % 12).nonzero? || 12, 2, f)
|
when 'l'; emit_a((hour % 12).nonzero? || 12, 2, f)
|
||||||
when 'M', 'OM'; emit_n(min, 2, f)
|
when 'M', 'OM'; emit_n(min, 2, f)
|
||||||
when 'm', 'Om'; emit_n(mon, 2, f)
|
when 'm', 'Om'; emit_n(mon, 2, f)
|
||||||
when 'N'
|
when 'N'
|
||||||
emit_n((sec_fraction / NANOSECONDS_IN_SECOND).floor, 9, f)
|
f[:n] = true
|
||||||
|
w = f[:w]
|
||||||
|
s = emit_n((sec_fraction / NANOSECONDS_IN_SECOND).floor, 9, f)
|
||||||
|
w ? s[0, w] : s
|
||||||
when 'n'; "\n"
|
when 'n'; "\n"
|
||||||
when 'P'; emit_ad(strftime('%p').downcase, 0, f)
|
when 'P'; emit_ad(strftime('%p').downcase, 0, f)
|
||||||
when 'p'; emit_au(if hour < 12 then 'AM' else 'PM' end, 0, f)
|
when 'p'; emit_au(if hour < 12 then 'AM' else 'PM' end, 0, f)
|
||||||
|
328
strftime.c
328
strftime.c
@ -1,3 +1,5 @@
|
|||||||
|
/* -*- c-file-style: "linux" -*- */
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* strftime.c
|
* strftime.c
|
||||||
*
|
*
|
||||||
@ -176,6 +178,7 @@ rb_strftime(char *s, size_t maxsize, const char *format, const struct tm *timept
|
|||||||
{
|
{
|
||||||
char *endp = s + maxsize;
|
char *endp = s + maxsize;
|
||||||
char *start = s;
|
char *start = s;
|
||||||
|
const char *sp, *tp;
|
||||||
auto char tbuf[100];
|
auto char tbuf[100];
|
||||||
long off;
|
long off;
|
||||||
int i, w;
|
int i, w;
|
||||||
@ -194,33 +197,36 @@ rb_strftime(char *s, size_t maxsize, const char *format, const struct tm *timept
|
|||||||
#endif /* HAVE_TIMEZONE */
|
#endif /* HAVE_TIMEZONE */
|
||||||
#endif /* HAVE_TM_NAME */
|
#endif /* HAVE_TM_NAME */
|
||||||
#endif /* HAVE_TM_ZONE */
|
#endif /* HAVE_TM_ZONE */
|
||||||
int precision = -1;
|
int precision, flags;
|
||||||
|
enum {LEFT, CHCASE, LOWER, UPPER, SPACE, ZERO};
|
||||||
|
#define BIT_OF(n) (1U<<(n))
|
||||||
|
|
||||||
/* various tables, useful in North America */
|
/* various tables, useful in North America */
|
||||||
static const char *days_a[] = {
|
static const char days_a[][4] = {
|
||||||
"Sun", "Mon", "Tue", "Wed",
|
"Sun", "Mon", "Tue", "Wed",
|
||||||
"Thu", "Fri", "Sat",
|
"Thu", "Fri", "Sat",
|
||||||
};
|
};
|
||||||
static const char *days_l[] = {
|
static const char days_l[][10] = {
|
||||||
"Sunday", "Monday", "Tuesday", "Wednesday",
|
"Sunday", "Monday", "Tuesday", "Wednesday",
|
||||||
"Thursday", "Friday", "Saturday",
|
"Thursday", "Friday", "Saturday",
|
||||||
};
|
};
|
||||||
static const char *months_a[] = {
|
static const char months_a[][4] = {
|
||||||
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
|
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
|
||||||
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
|
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
|
||||||
};
|
};
|
||||||
static const char *months_l[] = {
|
static const char months_l[][10] = {
|
||||||
"January", "February", "March", "April",
|
"January", "February", "March", "April",
|
||||||
"May", "June", "July", "August", "September",
|
"May", "June", "July", "August", "September",
|
||||||
"October", "November", "December",
|
"October", "November", "December",
|
||||||
};
|
};
|
||||||
static const char *ampm[] = { "AM", "PM", };
|
static const char ampm[][3] = { "AM", "PM", };
|
||||||
|
|
||||||
if (s == NULL || format == NULL || timeptr == NULL || maxsize == 0)
|
if (s == NULL || format == NULL || timeptr == NULL || maxsize == 0)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
/* quick check if we even need to bother */
|
/* quick check if we even need to bother */
|
||||||
if (strchr(format, '%') == NULL && strlen(format) + 1 >= maxsize) {
|
if (strchr(format, '%') == NULL && strlen(format) + 1 >= maxsize) {
|
||||||
|
err:
|
||||||
errno = ERANGE;
|
errno = ERANGE;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -261,12 +267,31 @@ rb_strftime(char *s, size_t maxsize, const char *format, const struct tm *timept
|
|||||||
#endif /* POSIX_SEMANTICS */
|
#endif /* POSIX_SEMANTICS */
|
||||||
|
|
||||||
for (; *format && s < endp - 1; format++) {
|
for (; *format && s < endp - 1; format++) {
|
||||||
tbuf[0] = '\0';
|
#define NEEDS(n) do if (s + (n) >= endp - 1) goto err; while (0)
|
||||||
|
#define FMT(fmt, def_prec, val) \
|
||||||
|
do { \
|
||||||
|
int l; \
|
||||||
|
if (precision == 0) break; \
|
||||||
|
if (precision < 0) precision = (def_prec); \
|
||||||
|
l = snprintf(s, endp - s, fmt, precision, val); \
|
||||||
|
if (l < 0) goto err; \
|
||||||
|
s += l; \
|
||||||
|
} while (0)
|
||||||
|
#define STRFTIME(fmt, tm) \
|
||||||
|
do { \
|
||||||
|
i = strftime(s, endp - s, fmt, tm); \
|
||||||
|
if (!i) return 0; \
|
||||||
|
s += i; \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
if (*format != '%') {
|
if (*format != '%') {
|
||||||
*s++ = *format;
|
*s++ = *format;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
tp = tbuf;
|
||||||
|
sp = format;
|
||||||
precision = -1;
|
precision = -1;
|
||||||
|
flags = 0;
|
||||||
again:
|
again:
|
||||||
switch (*++format) {
|
switch (*++format) {
|
||||||
case '\0':
|
case '\0':
|
||||||
@ -279,16 +304,16 @@ rb_strftime(char *s, size_t maxsize, const char *format, const struct tm *timept
|
|||||||
|
|
||||||
case 'a': /* abbreviated weekday name */
|
case 'a': /* abbreviated weekday name */
|
||||||
if (timeptr->tm_wday < 0 || timeptr->tm_wday > 6)
|
if (timeptr->tm_wday < 0 || timeptr->tm_wday > 6)
|
||||||
strcpy(tbuf, "?");
|
tp = "?";
|
||||||
else
|
else
|
||||||
strcpy(tbuf, days_a[timeptr->tm_wday]);
|
tp = days_a[timeptr->tm_wday];
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'A': /* full weekday name */
|
case 'A': /* full weekday name */
|
||||||
if (timeptr->tm_wday < 0 || timeptr->tm_wday > 6)
|
if (timeptr->tm_wday < 0 || timeptr->tm_wday > 6)
|
||||||
strcpy(tbuf, "?");
|
tp = "?";
|
||||||
else
|
else
|
||||||
strcpy(tbuf, days_l[timeptr->tm_wday]);
|
tp = days_l[timeptr->tm_wday];
|
||||||
break;
|
break;
|
||||||
|
|
||||||
#ifdef SYSV_EXT
|
#ifdef SYSV_EXT
|
||||||
@ -296,31 +321,31 @@ rb_strftime(char *s, size_t maxsize, const char *format, const struct tm *timept
|
|||||||
#endif
|
#endif
|
||||||
case 'b': /* abbreviated month name */
|
case 'b': /* abbreviated month name */
|
||||||
if (timeptr->tm_mon < 0 || timeptr->tm_mon > 11)
|
if (timeptr->tm_mon < 0 || timeptr->tm_mon > 11)
|
||||||
strcpy(tbuf, "?");
|
tp = "?";
|
||||||
else
|
else
|
||||||
strcpy(tbuf, months_a[timeptr->tm_mon]);
|
tp = months_a[timeptr->tm_mon];
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'B': /* full month name */
|
case 'B': /* full month name */
|
||||||
if (timeptr->tm_mon < 0 || timeptr->tm_mon > 11)
|
if (timeptr->tm_mon < 0 || timeptr->tm_mon > 11)
|
||||||
strcpy(tbuf, "?");
|
tp = "?";
|
||||||
else
|
else
|
||||||
strcpy(tbuf, months_l[timeptr->tm_mon]);
|
tp = months_l[timeptr->tm_mon];
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'c': /* appropriate date and time representation */
|
case 'c': /* appropriate date and time representation */
|
||||||
strftime(tbuf, sizeof tbuf, "%a %b %e %H:%M:%S %Y", timeptr);
|
STRFTIME("%a %b %e %H:%M:%S %Y", timeptr);
|
||||||
break;
|
continue;
|
||||||
|
|
||||||
case 'd': /* day of the month, 01 - 31 */
|
case 'd': /* day of the month, 01 - 31 */
|
||||||
i = range(1, timeptr->tm_mday, 31);
|
i = range(1, timeptr->tm_mday, 31);
|
||||||
sprintf(tbuf, "%02d", i);
|
FMT("%.*d", 2, i);
|
||||||
break;
|
continue;
|
||||||
|
|
||||||
case 'H': /* hour, 24-hour clock, 00 - 23 */
|
case 'H': /* hour, 24-hour clock, 00 - 23 */
|
||||||
i = range(0, timeptr->tm_hour, 23);
|
i = range(0, timeptr->tm_hour, 23);
|
||||||
sprintf(tbuf, "%02d", i);
|
FMT("%.*d", 2, i);
|
||||||
break;
|
continue;
|
||||||
|
|
||||||
case 'I': /* hour, 12-hour clock, 01 - 12 */
|
case 'I': /* hour, 12-hour clock, 01 - 12 */
|
||||||
i = range(0, timeptr->tm_hour, 23);
|
i = range(0, timeptr->tm_hour, 23);
|
||||||
@ -328,65 +353,69 @@ rb_strftime(char *s, size_t maxsize, const char *format, const struct tm *timept
|
|||||||
i = 12;
|
i = 12;
|
||||||
else if (i > 12)
|
else if (i > 12)
|
||||||
i -= 12;
|
i -= 12;
|
||||||
sprintf(tbuf, "%02d", i);
|
FMT("%.*d", 2, i);
|
||||||
break;
|
continue;
|
||||||
|
|
||||||
case 'j': /* day of the year, 001 - 366 */
|
case 'j': /* day of the year, 001 - 366 */
|
||||||
sprintf(tbuf, "%03d", timeptr->tm_yday + 1);
|
FMT("%.*d", 3, timeptr->tm_yday + 1);
|
||||||
break;
|
continue;
|
||||||
|
|
||||||
case 'm': /* month, 01 - 12 */
|
case 'm': /* month, 01 - 12 */
|
||||||
i = range(0, timeptr->tm_mon, 11);
|
i = range(0, timeptr->tm_mon, 11);
|
||||||
sprintf(tbuf, "%02d", i + 1);
|
FMT("%.*d", 2, i + 1);
|
||||||
break;
|
continue;
|
||||||
|
|
||||||
case 'M': /* minute, 00 - 59 */
|
case 'M': /* minute, 00 - 59 */
|
||||||
i = range(0, timeptr->tm_min, 59);
|
i = range(0, timeptr->tm_min, 59);
|
||||||
sprintf(tbuf, "%02d", i);
|
FMT("%.*d", 2, i);
|
||||||
break;
|
continue;
|
||||||
|
|
||||||
case 'p': /* am or pm based on 12-hour clock */
|
case 'p': /* am or pm based on 12-hour clock */
|
||||||
|
if (flags & BIT_OF(CHCASE)) {
|
||||||
|
flags &= ~(BIT_OF(UPPER)|BIT_OF(CHCASE));
|
||||||
|
flags |= BIT_OF(LOWER);
|
||||||
|
}
|
||||||
i = range(0, timeptr->tm_hour, 23);
|
i = range(0, timeptr->tm_hour, 23);
|
||||||
if (i < 12)
|
if (i < 12)
|
||||||
strcpy(tbuf, ampm[0]);
|
tp = ampm[0];
|
||||||
else
|
else
|
||||||
strcpy(tbuf, ampm[1]);
|
tp = ampm[1];
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'S': /* second, 00 - 60 */
|
case 'S': /* second, 00 - 60 */
|
||||||
i = range(0, timeptr->tm_sec, 60);
|
i = range(0, timeptr->tm_sec, 60);
|
||||||
sprintf(tbuf, "%02d", i);
|
FMT("%.*d", 2, i);
|
||||||
break;
|
continue;
|
||||||
|
|
||||||
case 'U': /* week of year, Sunday is first day of week */
|
case 'U': /* week of year, Sunday is first day of week */
|
||||||
sprintf(tbuf, "%02d", weeknumber(timeptr, 0));
|
FMT("%.*d", 2, weeknumber(timeptr, 0));
|
||||||
break;
|
continue;
|
||||||
|
|
||||||
case 'w': /* weekday, Sunday == 0, 0 - 6 */
|
case 'w': /* weekday, Sunday == 0, 0 - 6 */
|
||||||
i = range(0, timeptr->tm_wday, 6);
|
i = range(0, timeptr->tm_wday, 6);
|
||||||
sprintf(tbuf, "%d", i);
|
FMT("%.*d", 0, i);
|
||||||
break;
|
continue;
|
||||||
|
|
||||||
case 'W': /* week of year, Monday is first day of week */
|
case 'W': /* week of year, Monday is first day of week */
|
||||||
sprintf(tbuf, "%02d", weeknumber(timeptr, 1));
|
FMT("%.*d", 2, weeknumber(timeptr, 1));
|
||||||
break;
|
continue;
|
||||||
|
|
||||||
case 'x': /* appropriate date representation */
|
case 'x': /* appropriate date representation */
|
||||||
strftime(tbuf, sizeof tbuf, "%m/%d/%y", timeptr);
|
STRFTIME("%m/%d/%y", timeptr);
|
||||||
break;
|
continue;
|
||||||
|
|
||||||
case 'X': /* appropriate time representation */
|
case 'X': /* appropriate time representation */
|
||||||
strftime(tbuf, sizeof tbuf, "%H:%M:%S", timeptr);
|
STRFTIME("%H:%M:%S", timeptr);
|
||||||
break;
|
continue;
|
||||||
|
|
||||||
case 'y': /* year without a century, 00 - 99 */
|
case 'y': /* year without a century, 00 - 99 */
|
||||||
i = timeptr->tm_year % 100;
|
i = timeptr->tm_year % 100;
|
||||||
sprintf(tbuf, "%02d", i);
|
FMT("%.*d", 2, i);
|
||||||
break;
|
continue;
|
||||||
|
|
||||||
case 'Y': /* year with century */
|
case 'Y': /* year with century */
|
||||||
sprintf(tbuf, "%ld", 1900L + timeptr->tm_year);
|
FMT("%.*ld", 0, 1900L + timeptr->tm_year);
|
||||||
break;
|
continue;
|
||||||
|
|
||||||
#ifdef MAILHEADER_EXT
|
#ifdef MAILHEADER_EXT
|
||||||
/*
|
/*
|
||||||
@ -406,8 +435,8 @@ rb_strftime(char *s, size_t maxsize, const char *format, const struct tm *timept
|
|||||||
*/
|
*/
|
||||||
case 'z': /* time zone offset east of GMT e.g. -0600 */
|
case 'z': /* time zone offset east of GMT e.g. -0600 */
|
||||||
if (gmt) {
|
if (gmt) {
|
||||||
strcpy(tbuf, "+0000");
|
FMT("%+.*d", 4, 0);
|
||||||
break;
|
continue;
|
||||||
}
|
}
|
||||||
#ifdef HAVE_TM_NAME
|
#ifdef HAVE_TM_NAME
|
||||||
/*
|
/*
|
||||||
@ -438,37 +467,40 @@ rb_strftime(char *s, size_t maxsize, const char *format, const struct tm *timept
|
|||||||
#endif /* !HAVE_TM_ZONE */
|
#endif /* !HAVE_TM_ZONE */
|
||||||
#endif /* !HAVE_TM_NAME */
|
#endif /* !HAVE_TM_NAME */
|
||||||
if (off < 0) {
|
if (off < 0) {
|
||||||
tbuf[0] = '-';
|
|
||||||
off = -off;
|
off = -off;
|
||||||
|
off = -(off/60*100 + off%60);
|
||||||
} else {
|
} else {
|
||||||
tbuf[0] = '+';
|
off = off/60*100 + off%60;
|
||||||
}
|
}
|
||||||
sprintf(tbuf+1, "%02u%02u", (unsigned)off/60, (unsigned)off%60);
|
FMT("%+.*ld", 4, off);
|
||||||
break;
|
continue;
|
||||||
#endif /* MAILHEADER_EXT */
|
#endif /* MAILHEADER_EXT */
|
||||||
|
|
||||||
case 'Z': /* time zone name or abbrevation */
|
case 'Z': /* time zone name or abbrevation */
|
||||||
|
if (flags & BIT_OF(CHCASE)) {
|
||||||
|
flags &= ~(BIT_OF(UPPER)|BIT_OF(CHCASE));
|
||||||
|
flags |= BIT_OF(LOWER);
|
||||||
|
}
|
||||||
if (gmt) {
|
if (gmt) {
|
||||||
strcpy(tbuf, "UTC");
|
tp = "UTC";
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
#ifdef HAVE_TZNAME
|
#ifdef HAVE_TZNAME
|
||||||
i = (daylight && timeptr->tm_isdst > 0); /* 0 or 1 */
|
i = (daylight && timeptr->tm_isdst > 0); /* 0 or 1 */
|
||||||
strcpy(tbuf, tzname[i]);
|
tp = tzname[i];
|
||||||
#else
|
#else
|
||||||
#ifdef HAVE_TM_ZONE
|
#ifdef HAVE_TM_ZONE
|
||||||
strcpy(tbuf, timeptr->tm_zone);
|
tp = timeptr->tm_zone;
|
||||||
#else
|
#else
|
||||||
#ifdef HAVE_TM_NAME
|
#ifdef HAVE_TM_NAME
|
||||||
strcpy(tbuf, timeptr->tm_name);
|
tp = timeptr->tm_name;
|
||||||
#else
|
#else
|
||||||
#ifdef HAVE_TIMEZONE
|
#ifdef HAVE_TIMEZONE
|
||||||
gettimeofday(& tv, & zone);
|
gettimeofday(& tv, & zone);
|
||||||
#ifdef TIMEZONE_VOID
|
#ifdef TIMEZONE_VOID
|
||||||
strcpy(tbuf, timezone());
|
tp = timezone();
|
||||||
#else
|
#else
|
||||||
strcpy(tbuf, timezone(zone.tz_minuteswest,
|
tp = timezone(zone.tz_minuteswest, timeptr->tm_isdst > 0);
|
||||||
timeptr->tm_isdst > 0));
|
|
||||||
#endif /* TIMEZONE_VOID */
|
#endif /* TIMEZONE_VOID */
|
||||||
#endif /* HAVE_TIMEZONE */
|
#endif /* HAVE_TIMEZONE */
|
||||||
#endif /* HAVE_TM_NAME */
|
#endif /* HAVE_TM_NAME */
|
||||||
@ -478,40 +510,40 @@ rb_strftime(char *s, size_t maxsize, const char *format, const struct tm *timept
|
|||||||
|
|
||||||
#ifdef SYSV_EXT
|
#ifdef SYSV_EXT
|
||||||
case 'n': /* same as \n */
|
case 'n': /* same as \n */
|
||||||
tbuf[0] = '\n';
|
NEEDS(1);
|
||||||
tbuf[1] = '\0';
|
*s++ = '\n';
|
||||||
break;
|
continue;
|
||||||
|
|
||||||
case 't': /* same as \t */
|
case 't': /* same as \t */
|
||||||
tbuf[0] = '\t';
|
NEEDS(1);
|
||||||
tbuf[1] = '\0';
|
*s++ = '\t';
|
||||||
break;
|
continue;
|
||||||
|
|
||||||
case 'D': /* date as %m/%d/%y */
|
case 'D': /* date as %m/%d/%y */
|
||||||
strftime(tbuf, sizeof tbuf, "%m/%d/%y", timeptr);
|
STRFTIME("%m/%d/%y", timeptr);
|
||||||
break;
|
continue;
|
||||||
|
|
||||||
case 'e': /* day of month, blank padded */
|
case 'e': /* day of month, blank padded */
|
||||||
sprintf(tbuf, "%2d", range(1, timeptr->tm_mday, 31));
|
FMT("%.*d", 2, range(1, timeptr->tm_mday, 31));
|
||||||
break;
|
continue;
|
||||||
|
|
||||||
case 'r': /* time as %I:%M:%S %p */
|
case 'r': /* time as %I:%M:%S %p */
|
||||||
strftime(tbuf, sizeof tbuf, "%I:%M:%S %p", timeptr);
|
STRFTIME("%I:%M:%S %p", timeptr);
|
||||||
break;
|
continue;
|
||||||
|
|
||||||
case 'R': /* time as %H:%M */
|
case 'R': /* time as %H:%M */
|
||||||
strftime(tbuf, sizeof tbuf, "%H:%M", timeptr);
|
STRFTIME("%H:%M", timeptr);
|
||||||
break;
|
continue;
|
||||||
|
|
||||||
case 'T': /* time as %H:%M:%S */
|
case 'T': /* time as %H:%M:%S */
|
||||||
strftime(tbuf, sizeof tbuf, "%H:%M:%S", timeptr);
|
STRFTIME("%H:%M:%S", timeptr);
|
||||||
break;
|
continue;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef SUNOS_EXT
|
#ifdef SUNOS_EXT
|
||||||
case 'k': /* hour, 24-hour clock, blank pad */
|
case 'k': /* hour, 24-hour clock, blank pad */
|
||||||
sprintf(tbuf, "%2d", range(0, timeptr->tm_hour, 23));
|
FMT("%*d", 2, range(0, timeptr->tm_hour, 23));
|
||||||
break;
|
continue;
|
||||||
|
|
||||||
case 'l': /* hour, 12-hour clock, 1 - 12, blank pad */
|
case 'l': /* hour, 12-hour clock, 1 - 12, blank pad */
|
||||||
i = range(0, timeptr->tm_hour, 23);
|
i = range(0, timeptr->tm_hour, 23);
|
||||||
@ -519,28 +551,30 @@ rb_strftime(char *s, size_t maxsize, const char *format, const struct tm *timept
|
|||||||
i = 12;
|
i = 12;
|
||||||
else if (i > 12)
|
else if (i > 12)
|
||||||
i -= 12;
|
i -= 12;
|
||||||
sprintf(tbuf, "%2d", i);
|
FMT("%*d", 2, i);
|
||||||
break;
|
continue;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
#ifdef VMS_EXT
|
#ifdef VMS_EXT
|
||||||
case 'v': /* date as dd-bbb-YYYY */
|
case 'v': /* date as dd-bbb-YYYY */
|
||||||
sprintf(tbuf, "%2d-%3.3s-%4ld",
|
w = snprintf(s, endp - s, "%2d-%3.3s-%4ld",
|
||||||
range(1, timeptr->tm_mday, 31),
|
range(1, timeptr->tm_mday, 31),
|
||||||
months_a[range(0, timeptr->tm_mon, 11)],
|
months_a[range(0, timeptr->tm_mon, 11)],
|
||||||
timeptr->tm_year + 1900L);
|
timeptr->tm_year + 1900L);
|
||||||
|
if (w < 0) goto err;
|
||||||
for (i = 3; i < 6; i++)
|
for (i = 3; i < 6; i++)
|
||||||
if (islower(tbuf[i]))
|
if (islower(s[i]))
|
||||||
tbuf[i] = toupper(tbuf[i]);
|
s[i] = toupper(s[i]);
|
||||||
break;
|
s += w;
|
||||||
|
continue;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
#ifdef POSIX2_DATE
|
#ifdef POSIX2_DATE
|
||||||
case 'C':
|
case 'C':
|
||||||
sprintf(tbuf, "%02ld", (timeptr->tm_year + 1900L) / 100);
|
FMT("%.*ld", 2, (timeptr->tm_year + 1900L) / 100);
|
||||||
break;
|
continue;
|
||||||
|
|
||||||
|
|
||||||
case 'E':
|
case 'E':
|
||||||
@ -549,14 +583,13 @@ rb_strftime(char *s, size_t maxsize, const char *format, const struct tm *timept
|
|||||||
goto again;
|
goto again;
|
||||||
|
|
||||||
case 'V': /* week of year according ISO 8601 */
|
case 'V': /* week of year according ISO 8601 */
|
||||||
sprintf(tbuf, "%02d", iso8601wknum(timeptr));
|
FMT("%.*d", 2, iso8601wknum(timeptr));
|
||||||
break;
|
continue;
|
||||||
|
|
||||||
case 'u':
|
case 'u':
|
||||||
/* ISO 8601: Weekday as a decimal number [1 (Monday) - 7] */
|
/* ISO 8601: Weekday as a decimal number [1 (Monday) - 7] */
|
||||||
sprintf(tbuf, "%d", timeptr->tm_wday == 0 ? 7 :
|
FMT("%*d", 0, timeptr->tm_wday == 0 ? 7 : timeptr->tm_wday);
|
||||||
timeptr->tm_wday);
|
continue;
|
||||||
break;
|
|
||||||
#endif /* POSIX2_DATE */
|
#endif /* POSIX2_DATE */
|
||||||
|
|
||||||
#ifdef ISO_DATE_EXT
|
#ifdef ISO_DATE_EXT
|
||||||
@ -580,15 +613,16 @@ rb_strftime(char *s, size_t maxsize, const char *format, const struct tm *timept
|
|||||||
y = 1900L + timeptr->tm_year;
|
y = 1900L + timeptr->tm_year;
|
||||||
|
|
||||||
if (*format == 'G')
|
if (*format == 'G')
|
||||||
sprintf(tbuf, "%ld", y);
|
FMT("%.*ld", 0, y);
|
||||||
else
|
else
|
||||||
sprintf(tbuf, "%02ld", y % 100);
|
FMT("%.*ld", 2, y % 100);
|
||||||
break;
|
continue;
|
||||||
#endif /* ISO_DATE_EXT */
|
#endif /* ISO_DATE_EXT */
|
||||||
|
|
||||||
case 'L': /* millisecond, 000 - 999 */
|
|
||||||
sprintf(tbuf, "%03ld", ts->tv_nsec / 1000000);
|
case 'L':
|
||||||
break;
|
w = 3;
|
||||||
|
goto subsec;
|
||||||
|
|
||||||
case 'N':
|
case 'N':
|
||||||
/*
|
/*
|
||||||
@ -599,60 +633,84 @@ rb_strftime(char *s, size_t maxsize, const char *format, const struct tm *timept
|
|||||||
* %6N microsecond (6 digits)
|
* %6N microsecond (6 digits)
|
||||||
* %9N nanosecond (9 digits)
|
* %9N nanosecond (9 digits)
|
||||||
*/
|
*/
|
||||||
|
w = 9;
|
||||||
|
subsec:
|
||||||
{
|
{
|
||||||
char fmt[10];
|
|
||||||
long n = ts->tv_nsec;
|
long n = ts->tv_nsec;
|
||||||
|
|
||||||
if (precision < 0 || precision > 9) {
|
if (precision == 0) continue;
|
||||||
precision = 9;
|
if (precision < 0) {
|
||||||
|
precision = w;
|
||||||
}
|
}
|
||||||
if (precision == 0) break;
|
NEEDS(precision);
|
||||||
n /= pow(10, 9 - precision);
|
if (precision < w) {
|
||||||
sprintf(fmt, "%%0%dld", precision);
|
snprintf(tbuf, w + 1, "%.*ld", w, n);
|
||||||
sprintf(tbuf, fmt, n);
|
memcpy(s, tbuf, precision);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
snprintf(s, endp - s, "%.*ld", w, n);
|
||||||
|
memset(s + w, '0', precision - w);
|
||||||
|
}
|
||||||
|
s += precision;
|
||||||
}
|
}
|
||||||
break;
|
continue;
|
||||||
|
|
||||||
case 'F': /* Equivalent to %Y-%m-%d */
|
case 'F': /* Equivalent to %Y-%m-%d */
|
||||||
{
|
{
|
||||||
int mon, mday;
|
int mon, mday;
|
||||||
mon = range(0, timeptr->tm_mon, 11) + 1;
|
mon = range(0, timeptr->tm_mon, 11) + 1;
|
||||||
mday = range(1, timeptr->tm_mday, 31);
|
mday = range(1, timeptr->tm_mday, 31);
|
||||||
sprintf(tbuf, "%ld-%02d-%02d",
|
i = snprintf(s, endp - s, "%ld-%02d-%02d",
|
||||||
1900L + timeptr->tm_year, mon, mday);
|
1900L + timeptr->tm_year, mon, mday);
|
||||||
|
if (i < 0)
|
||||||
|
goto err;
|
||||||
|
s += i;
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
|
||||||
|
case '-':
|
||||||
|
flags |= BIT_OF(LEFT);
|
||||||
|
goto again;
|
||||||
|
|
||||||
|
case '^':
|
||||||
|
flags |= BIT_OF(UPPER);
|
||||||
|
goto again;
|
||||||
|
|
||||||
|
case '#':
|
||||||
|
flags |= BIT_OF(CHCASE);
|
||||||
|
goto again;
|
||||||
|
|
||||||
|
case '_':
|
||||||
|
flags |= BIT_OF(SPACE);
|
||||||
|
goto again;
|
||||||
|
|
||||||
|
case '0':
|
||||||
|
flags |= BIT_OF(ZERO);
|
||||||
|
case '1': case '2': case '3': case '4':
|
||||||
|
case '5': case '6': case '7': case '8': case '9':
|
||||||
|
{
|
||||||
|
char *e;
|
||||||
|
precision = (int)strtoul(format, &e, 10);
|
||||||
|
format = e - 1;
|
||||||
|
goto again;
|
||||||
}
|
}
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
default:
|
||||||
if (isdigit(*format)) {
|
tp = sp;
|
||||||
const char *p = format;
|
i = format - sp + 1;
|
||||||
while (isdigit(*p)) p++;
|
goto copy;
|
||||||
if (*p == 'N') {
|
|
||||||
precision = atoi(format);
|
|
||||||
format = p - 1;
|
|
||||||
goto again;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
tbuf[0] = '%';
|
|
||||||
tbuf[1] = *format;
|
|
||||||
tbuf[2] = '\0';
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
i = strlen(tbuf);
|
i = strlen(tp);
|
||||||
|
copy:
|
||||||
if (i) {
|
if (i) {
|
||||||
if (s + i < endp - 1) {
|
NEEDS(i);
|
||||||
strcpy(s, tbuf);
|
memcpy(s, tp, i);
|
||||||
s += i;
|
s += i;
|
||||||
} else {
|
|
||||||
errno = ERANGE;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
out:
|
out:
|
||||||
if (s >= endp) {
|
if (s >= endp) {
|
||||||
errno = ERANGE;
|
goto err;
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
if (*format == '\0') {
|
if (*format == '\0') {
|
||||||
*s = '\0';
|
*s = '\0';
|
||||||
|
@ -381,17 +381,12 @@ class TestTime < Test::Unit::TestCase
|
|||||||
assert_equal("Sat", Time.at(946684800).strftime("%a"))
|
assert_equal("Sat", Time.at(946684800).strftime("%a"))
|
||||||
|
|
||||||
t = Time.at(946684800, 123456.789)
|
t = Time.at(946684800, 123456.789)
|
||||||
assert_equal("123", t.strftime("%L"))
|
|
||||||
assert_equal("123456789", t.strftime("%N"))
|
|
||||||
assert_equal("123", t.strftime("%3N"))
|
assert_equal("123", t.strftime("%3N"))
|
||||||
assert_equal("123456", t.strftime("%6N"))
|
assert_equal("123456", t.strftime("%6N"))
|
||||||
assert_equal("123456789", t.strftime("%9N"))
|
assert_equal("123456789", t.strftime("%9N"))
|
||||||
assert_equal("123456789", t.strftime("%10N"))
|
assert_equal("1234567890", t.strftime("%10N"))
|
||||||
assert_equal("123456789", t.strftime("%1" + "0" * 100 + "N"))
|
|
||||||
assert_equal("", t.strftime("%0N"))
|
assert_equal("", t.strftime("%0N"))
|
||||||
assert_equal("%3S", t.strftime("%3S"))
|
assert_equal("000", t.strftime("%3S"))
|
||||||
fmt = "%1" + "0" * 100 + "S"
|
|
||||||
assert_equal(fmt, t.strftime(fmt))
|
|
||||||
|
|
||||||
t = Time.mktime(2001, 10, 1)
|
t = Time.mktime(2001, 10, 1)
|
||||||
assert_equal("2001-10-01", t.strftime("%F"))
|
assert_equal("2001-10-01", t.strftime("%F"))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user