From 39a3d1793b67df8ef1aa48e2f813dcac13814544 Mon Sep 17 00:00:00 2001 From: nobu Date: Tue, 29 May 2012 08:28:13 +0000 Subject: strftime.c: triple colons modifier partially borrowed from ext/date. * strftime.c (rb_strftime_with_timespec): support GNU extension triple colons modifier. [EXPERIMENTAL] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@35836 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- strftime.c | 43 +++++++++++++++++++++++++++++++------------ 1 file changed, 31 insertions(+), 12 deletions(-) (limited to 'strftime.c') diff --git a/strftime.c b/strftime.c index 8d301e1115..ccb180b57b 100644 --- a/strftime.c +++ b/strftime.c @@ -460,6 +460,18 @@ rb_strftime_with_timespec(char *s, size_t maxsize, const char *format, rb_encodi #ifdef MAILHEADER_EXT case 'z': /* time zone offset east of GMT e.g. -0600 */ + if (gmt) { + off = 0; + } + else { + off = NUM2LONG(rb_funcall(vtm->utc_offset, rb_intern("round"), 0)); + } + if (off < 0) { + off = -off; + sign = -1; + } else { + sign = +1; + } switch (colons) { case 0: /* %z -> +hhmm */ precision = precision <= 5 ? 2 : precision-3; @@ -476,22 +488,25 @@ rb_strftime_with_timespec(char *s, size_t maxsize, const char *format, rb_encodi NEEDS(precision + 7); break; + case 3: /* %:::z -> +hh[:mm[:ss]] */ + if (off % 3600 == 0) { + precision = precision <= 3 ? 2 : precision-1; + NEEDS(precision + 3); + } + else if (off % 60 == 0) { + precision = precision <= 6 ? 2 : precision-4; + NEEDS(precision + 4); + } + else { + precision = precision <= 9 ? 2 : precision-7; + NEEDS(precision + 9); + } + break; + default: format--; goto unknown; } - if (gmt) { - off = 0; - } - else { - off = NUM2LONG(rb_funcall(vtm->utc_offset, rb_intern("round"), 0)); - } - if (off < 0) { - off = -off; - sign = -1; - } else { - sign = +1; - } i = snprintf(s, endp - s, (padding == ' ' ? "%+*ld" : "%+.*ld"), precision + 1, sign * (off / 3600)); if (i < 0) goto err; @@ -500,12 +515,16 @@ rb_strftime_with_timespec(char *s, size_t maxsize, const char *format, rb_encodi } s += i; off = off % 3600; + if (colons == 3 && off == 0) + continue; if (1 <= colons) *s++ = ':'; i = snprintf(s, endp - s, "%02d", (int)(off / 60)); if (i < 0) goto err; s += i; off = off % 60; + if (colons == 3 && off == 0) + continue; if (2 <= colons) { *s++ = ':'; i = snprintf(s, endp - s, "%02d", (int)off); -- cgit v1.2.3