[前][次][番号順一覧][スレッド一覧]

ruby-changes:16580

From: akr <ko1@a...>
Date: Thu, 8 Jul 2010 06:07:17 +0900 (JST)
Subject: [ruby-changes:16580] Ruby:r28572 (trunk): * strftime.c (rb_strftime_with_timespec): support %:z and %::z.

akr	2010-07-08 06:07:01 +0900 (Thu, 08 Jul 2010)

  New Revision: 28572

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=28572

  Log:
    * strftime.c (rb_strftime_with_timespec): support %:z and %::z.
      [ruby-dev:41841]

  Modified files:
    trunk/ChangeLog
    trunk/strftime.c
    trunk/test/ruby/test_time_tz.rb
    trunk/time.c

Index: time.c
===================================================================
--- time.c	(revision 28571)
+++ time.c	(revision 28572)
@@ -4346,7 +4346,9 @@
  *    %X - Preferred representation for the time alone, no date
  *    %y - Year without a century (00..99)
  *    %Y - Year with century
- *    %z - Time zone as  hour offset from UTC (e.g. +0900)
+ *    %z - Time zone as hour and minute offset from UTC (e.g. +0900)
+ *            %:z - hour and minute offset from UTC with a colon (e.g. +09:00)
+ *            %::z - hour, minute and second offset from UTC (e.g. +09:00:00)
  *    %Z - Time zone name
  *    %% - Literal ``%'' character
  *
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 28571)
+++ ChangeLog	(revision 28572)
@@ -1,3 +1,8 @@
+Thu Jul  8 06:05:58 2010  Tanaka Akira  <akr@f...>
+
+	* strftime.c (rb_strftime_with_timespec): support %:z and %::z.
+	  [ruby-dev:41841]
+
 Thu Jul  8 00:15:50 2010  Yusuke Endoh  <mame@t...>
 
 	* gem_prelude.rb: provide workaround for gem activation.  Currently,
Index: strftime.c
===================================================================
--- strftime.c	(revision 28571)
+++ strftime.c	(revision 28572)
@@ -215,7 +215,7 @@
 #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 @@
 		precision = -1;
 		flags = 0;
 		padding = 0;
+                colons = 0;
 	again:
 		switch (*++format) {
 		case '\0':
@@ -530,13 +531,31 @@
 		 * 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 @@
 			} 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 @@
 			padding = ' ';
 			goto again;
 
+		case ':':
+			FLAG_FOUND();
+                        colons++;
+			goto again;
+
 		case '0':
 			padding = '0';
 		case '1':  case '2': case '3': case '4':
Index: test/ruby/test_time_tz.rb
===================================================================
--- test/ruby/test_time_tz.rb	(revision 28571)
+++ test/ruby/test_time_tz.rb	(revision 28572)
@@ -18,7 +18,7 @@
   end
 
   module Util
-    def format_gmtoff(gmtoff)
+    def format_gmtoff(gmtoff, colon=false)
       if gmtoff < 0
         expected = "-"
         gmtoff = -gmtoff
@@ -26,10 +26,23 @@
         expected = "+"
       end
       gmtoff /= 60
-      expected << "%02d%02d" % [gmtoff / 60, gmtoff % 60]
+      expected << "%02d" % [gmtoff / 60]
+      expected << ":" if colon
+      expected << "%02d" % [gmtoff % 60]
       expected
     end
 
+    def format_gmtoff2(gmtoff)
+      if gmtoff < 0
+        expected = "-"
+        gmtoff = -gmtoff
+      else
+        expected = "+"
+      end
+      expected << "%02d:%02d:%02d" % [gmtoff / 3600, gmtoff % 3600 / 60, gmtoff % 60]
+      expected
+    end
+
     def group_by(e, &block)
       if e.respond_to? :group_by
         e.group_by(&block)
@@ -189,6 +202,9 @@
           assert_nothing_raised(mesg) { t.localtime }
           assert_equal(expected, time_to_s(t), mesg)
           assert_equal(gmtoff, t.gmtoff)
+          assert_equal(format_gmtoff(gmtoff), t.strftime("%z"))
+          assert_equal(format_gmtoff(gmtoff, true), t.strftime("%:z"))
+          assert_equal(format_gmtoff2(gmtoff), t.strftime("%::z"))
         }
       }
     }
@@ -289,6 +305,7 @@
 #right/Asia/Tokyo  Sat Dec 31 23:59:60 2005 UTC = Sun Jan  1 08:59:60 2006 JST isdst=0 gmtoff=32400
 right/Europe/Paris  Fri Jun 30 23:59:60 1972 UTC = Sat Jul  1 00:59:60 1972 CET isdst=0 gmtoff=3600
 right/Europe/Paris  Wed Dec 31 23:59:60 2008 UTC = Thu Jan  1 00:59:60 2009 CET isdst=0 gmtoff=3600
+Europe/Lisbon  Mon Jan  1 00:36:31 1912 UTC = Sun Dec 31 23:59:59 1911 LMT isdst=0 gmtoff=-2192
 End
   gen_zdump_test
 end

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]