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

ruby-changes:65354

From: nagachika <ko1@a...>
Date: Sat, 27 Feb 2021 13:49:36 +0900 (JST)
Subject: [ruby-changes:65354] 190ffd8761 (ruby_2_7): merge revision(s) f4be7a510eebbe6507ba41d138d7d252f4a68e90,9441f3f97087a4325ee80911859d37da41fa5050: [Backport #17504]

https://git.ruby-lang.org/ruby.git/commit/?id=190ffd8761

From 190ffd8761bec206582095028e5752ae5ccd7587 Mon Sep 17 00:00:00 2001
From: nagachika <nagachika@r...>
Date: Sat, 27 Feb 2021 13:46:28 +0900
Subject: merge revision(s)
 f4be7a510eebbe6507ba41d138d7d252f4a68e90,9441f3f97087a4325ee80911859d37da41fa5050:
 [Backport #17504]

	Added tests for Time#getlocal with UTC offset

	---
	 test/ruby/test_time.rb | 8 ++++++++
	 1 file changed, 8 insertions(+)

	Allow UTC offset without colons per ISO-8601 [Bug #17504]

	---
	 test/ruby/test_time.rb |  6 ++++++
	 time.c                 | 44 +++++++++++++++++++++++++++++---------------
	 2 files changed, 35 insertions(+), 15 deletions(-)
---
 test/ruby/test_time.rb | 14 ++++++++++++++
 time.c                 | 44 +++++++++++++++++++++++++++++---------------
 version.h              |  4 ++--
 3 files changed, 45 insertions(+), 17 deletions(-)

diff --git a/test/ruby/test_time.rb b/test/ruby/test_time.rb
index ad8fe6c..1749d92 100644
--- a/test/ruby/test_time.rb
+++ b/test/ruby/test_time.rb
@@ -1194,6 +1194,20 @@ class TestTime < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_time.rb#L1194
     }
   end
 
+  def test_getlocal_utc_offset
+    t = Time.gm(2000)
+    assert_equal [00, 30, 21, 31, 12, 1999], t.getlocal("-02:30").to_a[0, 6]
+    assert_equal [00, 00,  9,  1,  1, 2000], t.getlocal("+09:00").to_a[0, 6]
+    assert_equal [20, 29, 21, 31, 12, 1999], t.getlocal("-02:30:40").to_a[0, 6]
+    assert_equal [35, 10,  9,  1,  1, 2000], t.getlocal("+09:10:35").to_a[0, 6]
+    assert_equal [00, 30, 21, 31, 12, 1999], t.getlocal("-0230").to_a[0, 6]
+    assert_equal [00, 00,  9,  1,  1, 2000], t.getlocal("+0900").to_a[0, 6]
+    assert_equal [20, 29, 21, 31, 12, 1999], t.getlocal("-023040").to_a[0, 6]
+    assert_equal [35, 10,  9,  1,  1, 2000], t.getlocal("+091035").to_a[0, 6]
+    assert_raise(ArgumentError) {t.getlocal("-02:3040")}
+    assert_raise(ArgumentError) {t.getlocal("+0910:35")}
+  end
+
   def test_getlocal_nil
     now = Time.now
     now2 = nil
diff --git a/time.c b/time.c
index d76e015..5c252ad 100644
--- a/time.c
+++ b/time.c
@@ -2071,7 +2071,7 @@ utc_offset_arg(VALUE arg) https://github.com/ruby/ruby/blob/trunk/time.c#L2071
     VALUE tmp;
     if (!NIL_P(tmp = rb_check_string_type(arg))) {
         int n = 0;
-        char *s = RSTRING_PTR(tmp);
+        const char *s = RSTRING_PTR(tmp), *min = NULL, *sec = NULL;
         if (!rb_enc_str_asciicompat_p(tmp)) {
 	  invalid_utc_offset:
             return Qnil;
@@ -2100,24 +2100,38 @@ utc_offset_arg(VALUE arg) https://github.com/ruby/ruby/blob/trunk/time.c#L2100
             if (STRNCASECMP("UTC", s, 3) == 0) {
                 return UTC_ZONE;
             }
-            goto invalid_utc_offset;
-	  case 9:
-	    if (s[6] != ':') goto invalid_utc_offset;
-	    if (!ISDIGIT(s[7]) || !ISDIGIT(s[8])) goto invalid_utc_offset;
-	    n += (s[7] * 10 + s[8] - '0' * 11);
-            /* fall through */
-	  case 6:
-	    if (s[0] != '+' && s[0] != '-') goto invalid_utc_offset;
-	    if (!ISDIGIT(s[1]) || !ISDIGIT(s[2])) goto invalid_utc_offset;
-	    if (s[3] != ':') goto invalid_utc_offset;
-	    if (!ISDIGIT(s[4]) || !ISDIGIT(s[5])) goto invalid_utc_offset;
-	    if (s[4] > '5') goto invalid_utc_offset;
-	    break;
+            break; /* +HH */
+          case 5: /* +HHMM */
+            min = s+3;
+            break;
+          case 6: /* +HH:MM */
+            min = s+4;
+            break;
+          case 7: /* +HHMMSS */
+            sec = s+5;
+            min = s+3;
+            break;
+          case 9: /* +HH:MM:SS */
+            sec = s+7;
+            min = s+4;
+            break;
 	  default:
 	    goto invalid_utc_offset;
 	}
+        if (sec) {
+            if (sec == s+7 && *(sec-1) != ':') goto invalid_utc_offset;
+            if (!ISDIGIT(sec[0]) || !ISDIGIT(sec[1])) goto invalid_utc_offset;
+            n += (sec[0] * 10 + sec[1] - '0' * 11);
+        }
+        if (min) {
+            if (min == s+4 && *(min-1) != ':') goto invalid_utc_offset;
+            if (!ISDIGIT(min[0]) || !ISDIGIT(min[1])) goto invalid_utc_offset;
+            if (min[0] > '5') goto invalid_utc_offset;
+            n += (min[0] * 10 + min[1] - '0' * 11) * 60;
+        }
+        if (s[0] != '+' && s[0] != '-') goto invalid_utc_offset;
+        if (!ISDIGIT(s[1]) || !ISDIGIT(s[2])) goto invalid_utc_offset;
         n += (s[1] * 10 + s[2] - '0' * 11) * 3600;
-        n += (s[4] * 10 + s[5] - '0' * 11) * 60;
         if (s[0] == '-')
             n = -n;
         return INT2FIX(n);
diff --git a/version.h b/version.h
index 50a3682..14e637c 100644
--- a/version.h
+++ b/version.h
@@ -2,11 +2,11 @@ https://github.com/ruby/ruby/blob/trunk/version.h#L2
 # define RUBY_VERSION_MINOR RUBY_API_VERSION_MINOR
 #define RUBY_VERSION_TEENY 3
 #define RUBY_RELEASE_DATE RUBY_RELEASE_YEAR_STR"-"RUBY_RELEASE_MONTH_STR"-"RUBY_RELEASE_DAY_STR
-#define RUBY_PATCHLEVEL 160
+#define RUBY_PATCHLEVEL 161
 
 #define RUBY_RELEASE_YEAR 2021
 #define RUBY_RELEASE_MONTH 2
-#define RUBY_RELEASE_DAY 20
+#define RUBY_RELEASE_DAY 27
 
 #include "ruby/version.h"
 
-- 
cgit v1.1


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

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