diff options
author | akr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2010-07-07 21:07:01 +0000 |
---|---|---|
committer | akr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2010-07-07 21:07:01 +0000 |
commit | a5d37d10a9e18952badcf821679104613590e779 (patch) | |
tree | b45b339a3009fdbbbc8b9999ff08f88cef1da56f /strftime.c | |
parent | 8f11f4bcb4e3ed3b087d19a163f3efda7de07821 (diff) |
* strftime.c (rb_strftime_with_timespec): support %:z and %::z.
[ruby-dev:41841]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@28572 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'strftime.c')
-rw-r--r-- | strftime.c | 49 |
1 files changed, 42 insertions, 7 deletions
diff --git a/strftime.c b/strftime.c index 2b98e29148..ac83f5d0e2 100644 --- a/strftime.c +++ b/strftime.c @@ -215,7 +215,7 @@ rb_strftime_with_timespec(char *s, size_t maxsize, const char *format, const str #endif #endif /* HAVE_TM_NAME */ #endif /* HAVE_TM_ZONE */ - int precision, flags; + int precision, flags, colons; char padding; enum {LEFT, CHCASE, LOWER, UPPER, LOCALE_O, LOCALE_E}; #define BIT_OF(n) (1U<<(n)) @@ -348,6 +348,7 @@ rb_strftime_with_timespec(char *s, size_t maxsize, const char *format, const str precision = -1; flags = 0; padding = 0; + colons = 0; again: switch (*++format) { case '\0': @@ -530,13 +531,31 @@ rb_strftime_with_timespec(char *s, size_t maxsize, const char *format, const str * us that muck around with various message processors. */ case 'z': /* time zone offset east of GMT e.g. -0600 */ - if (precision < 4) precision = 4; - NEEDS(precision + 1); + switch (colons) { + case 0: /* %z -> +hhmm */ + precision = precision <= 5 ? 2 : precision-3; + NEEDS(precision + 3); + break; + + case 1: /* %:z -> +hh:mm */ + precision = precision <= 5 ? 2 : precision-3; + NEEDS(precision + 4); + break; + + case 2: /* %::z -> +hh:mm:ss */ + precision = precision <= 5 ? 2 : precision-3; + NEEDS(precision + 7); + break; + + default: + format--; + goto unknown; + } if (gmt) { off = 0; } else { - off = NUM2LONG(rb_funcall(quo(vtm->utc_offset, INT2FIX(60)), rb_intern("round"), 0)); + off = NUM2LONG(rb_funcall(vtm->utc_offset, rb_intern("round"), 0)); #if 0 #ifdef HAVE_TM_NAME /* @@ -583,11 +602,22 @@ rb_strftime_with_timespec(char *s, size_t maxsize, const char *format, const str } else { *s++ = '+'; } - off = off/60*100 + off%60; - i = snprintf(s, endp - s, (padding == ' ' ? "%*ld" : "%.*ld"), - precision - (precision > 4), off); + i = snprintf(s, endp - s, (padding == ' ' ? "%*ld" : "%.*ld"), precision, off / 3600); + if (i < 0) goto err; + s += i; + off = off % 3600; + if (1 <= colons) + *s++ = ':'; + i = snprintf(s, endp - s, "%02d", off / 60); if (i < 0) goto err; s += i; + off = off % 60; + if (2 <= colons) { + *s++ = ':'; + i = snprintf(s, endp - s, "%02d", off); + if (i < 0) goto err; + s += i; + } continue; #endif /* MAILHEADER_EXT */ @@ -839,6 +869,11 @@ rb_strftime_with_timespec(char *s, size_t maxsize, const char *format, const str padding = ' '; goto again; + case ':': + FLAG_FOUND(); + colons++; + goto again; + case '0': padding = '0'; case '1': case '2': case '3': case '4': |