Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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
  • Loading branch information...
commit 39a3d1793b67df8ef1aa48e2f813dcac13814544 1 parent 241902e
@nobu nobu authored
Showing with 53 additions and 13 deletions.
  1. +4 −1 ChangeLog
  2. +31 −12 strftime.c
  3. +18 −0 test/ruby/test_time.rb
View
5 ChangeLog
@@ -1,4 +1,7 @@
-Tue May 29 17:28:15 2012 Nobuyoshi Nakada <nobu@ruby-lang.org>
+Tue May 29 17:28:20 2012 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * strftime.c (rb_strftime_with_timespec): support GNU extension triple
+ colons modifier. [EXPERIMENTAL]
* strftime.c (rb_strftime_with_timespec): check conversion with locale
modifier.
View
43 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);
View
18 test/ruby/test_time.rb
@@ -695,6 +695,20 @@ def test_strftime_too_wide
assert_raise(Errno::ERANGE, bug4457) {Time.now.strftime('%8192z')}
end
+ def test_strfimte_zoneoffset
+ t = T2000.getlocal("+09:00:00")
+ assert_equal("+0900", t.strftime("%z"))
+ assert_equal("+09:00", t.strftime("%:z"))
+ assert_equal("+09:00:01", t.strftime("%::z"))
+ assert_equal("+09", t.strftime("%:::z"))
+
+ t = T2000.getlocal("+09:00:01")
+ assert_equal("+0900", t.strftime("%z"))
+ assert_equal("+09:00", t.strftime("%:z"))
+ assert_equal("+09:00:01", t.strftime("%::z"))
+ assert_equal("+09:00:01", t.strftime("%:::z"))
+ end
+
def test_strftime_padding
bug4458 = '[ruby-dev:43287]'
t = T2000.getlocal("+09:00")
@@ -706,6 +720,7 @@ def test_strftime_padding
assert_equal("+000009:00", t.strftime("%10:z"), bug4458)
assert_equal(" +9:00:00", t.strftime("%_10::z"), bug4458)
assert_equal("+009:00:00", t.strftime("%10::z"), bug4458)
+ assert_equal("+000000009", t.strftime("%10:::z"))
t = T2000.getlocal("-05:00")
assert_equal("-0500", t.strftime("%z"))
assert_equal("-05:00", t.strftime("%:z"))
@@ -715,6 +730,7 @@ def test_strftime_padding
assert_equal("-000005:00", t.strftime("%10:z"), bug4458)
assert_equal(" -5:00:00", t.strftime("%_10::z"), bug4458)
assert_equal("-005:00:00", t.strftime("%10::z"), bug4458)
+ assert_equal("-000000005", t.strftime("%10:::z"))
bug6323 = '[ruby-core:44447]'
t = T2000.getlocal("+00:36")
@@ -724,6 +740,7 @@ def test_strftime_padding
assert_equal("+000000:36", t.strftime("%10:z"), bug6323)
assert_equal(" +0:36:00", t.strftime("%_10::z"), bug6323)
assert_equal("+000:36:00", t.strftime("%10::z"), bug6323)
+ assert_equal("+000000:36", t.strftime("%10:::z"))
t = T2000.getlocal("-00:55")
assert_equal(" -055", t.strftime("%_10z"), bug6323)
assert_equal("-000000055", t.strftime("%10z"), bug6323)
@@ -731,6 +748,7 @@ def test_strftime_padding
assert_equal("-000000:55", t.strftime("%10:z"), bug6323)
assert_equal(" -0:55:00", t.strftime("%_10::z"), bug6323)
assert_equal("-000:55:00", t.strftime("%10::z"), bug6323)
+ assert_equal("-000000:55", t.strftime("%10:::z"))
end
def test_strftime_invalid_modifier
Please sign in to comment.
Something went wrong with that request. Please try again.