[ruby-oci8-commit] [529] trunk/ruby-oci8: refactor test code by adding global functions convert_to_time()

nobody at rubyforge.org nobody at rubyforge.org
Tue Jul 31 11:21:28 UTC 2012


Revision: 529
Author:   kubo
Date:     2012-07-31 11:21:28 +0000 (Tue, 31 Jul 2012)
Log Message:
-----------
refactor test code by adding global functions convert_to_time()
and convert_to_datetime(), which will be used by test_object.rb later.

Modified Paths:
--------------
    trunk/ruby-oci8/ChangeLog
    trunk/ruby-oci8/test/config.rb
    trunk/ruby-oci8/test/test_datetime.rb

Modified: trunk/ruby-oci8/ChangeLog
===================================================================
--- trunk/ruby-oci8/ChangeLog	2012-07-28 12:12:26 UTC (rev 528)
+++ trunk/ruby-oci8/ChangeLog	2012-07-31 11:21:28 UTC (rev 529)
@@ -1,3 +1,8 @@
+2012-07-31  KUBO Takehiro  <kubo at jiubao.org>
+	* test/config.rb, test/test_datetime.rb: refactor test code by adding
+	    global functions convert_to_time() and convert_to_datetime(),
+	    which will be used by test_object.rb later.
+
 2012-07-28  KUBO Takehiro  <kubo at jiubao.org>
 	* test/test_metadata.rb: fix a sequence test failure on Oracle 11gR2.
 	    Fix github issue #18 reported by Yasuo Honda.

Modified: trunk/ruby-oci8/test/config.rb
===================================================================
--- trunk/ruby-oci8/test/config.rb	2012-07-28 12:12:26 UTC (rev 528)
+++ trunk/ruby-oci8/test/config.rb	2012-07-31 11:21:28 UTC (rev 529)
@@ -44,6 +44,53 @@
   $test_clob = true
 end
 
+begin
+  Time.new(2001, 1, 1, 0, 0, 0, '+00:00')
+  # ruby 1.9.2 or later
+  def convert_to_time(year, month, day, hour, minute, sec, subsec, timezone)
+    subsec ||= 0
+    if timezone
+      # with time zone
+      Time.new(year, month, day, hour, minute, sec + subsec, timezone)
+    else
+      # without time zone
+      Time.local(year, month, day, hour, minute, sec, subsec * 1000000)
+    end
+  end
+rescue
+  # ruby 1.9.1 or former
+  def convert_to_time(year, month, day, hour, minute, sec, subsec, timezone)
+    subsec ||= 0
+    if timezone
+      # with time zone
+      /([+-])(\d+):(\d+)/ =~ timezone
+      offset = ($1 + '1').to_i * ($2.to_i * 60 + $3.to_i)
+      if offset == 0
+        Time.utc(year, month, day, hour, minute, sec, subsec * 1000000)
+      else
+        tm = Time.local(year, month, day, hour, minute, sec, subsec * 1000000)
+        raise "Failed to convert #{str} to Time" if tm.utc_offset != offset * 60
+        tm
+      end
+    else
+      # without time zone
+      Time.local(year, month, day, hour, minute, sec, subsec * 1000000)
+    end
+  end
+end
+
+def convert_to_datetime(year, month, day, hour, minute, sec, subsec, timezone)
+  subsec ||= 0
+  utc_offset = if timezone
+                 # with time zone
+                 /([+-])(\d+):(\d+)/ =~ timezone
+                 ($1 + '1').to_i * ($2.to_i * 60 + $3.to_i) * 60
+               else
+                 Time.local(year, month, day, hour, minute, sec).utc_offset
+               end
+  DateTime.civil(year, month, day, hour, minute, sec + subsec, utc_offset.to_r / 86400)
+end
+
 module Test
   module Unit
     class TestCase

Modified: trunk/ruby-oci8/test/test_datetime.rb
===================================================================
--- trunk/ruby-oci8/test/test_datetime.rb	2012-07-28 12:12:26 UTC (rev 528)
+++ trunk/ruby-oci8/test/test_datetime.rb	2012-07-31 11:21:28 UTC (rev 529)
@@ -13,59 +13,20 @@
     end
   end
 
-  @@time_new_accepts_timezone = begin
-                                  Time.new(2001, 1, 1, 0, 0, 0, '+00:00')
-                                  true
-                                rescue
-                                  false
-                                end
-
   def string_to_time(str)
-    /(\d+)-(\d+)-(\d+) ?(?:(\d+):(\d+):(\d+))?(?:\.(\d+))? ?(([+-])(\d+):(\d+))?/ =~ str
-    args = []
-    args << $1.to_i # year
-    args << $2.to_i # month
-    args << $3.to_i # day
-    args << $4.to_i if $4 # hour
-    args << $5.to_i if $5 # minute
-    if $8 and @@time_new_accepts_timezone
-      args << $6.to_i + $7.to_i.to_r / ('1' + '0' * ($7.length)).to_i
-      args << $8
-      Time.new(*args)
-    else
-      if $6
-        args << $6.to_i
-      end
-      if $7
-        args << $7.to_i.to_r * 1000000 / ('1' + '0' * ($7.length)).to_i
-      end
-      if $8
-        # with time zone
-        offset = ($9 + '1').to_i * ($10.to_i * 60 + $11.to_i)
-        if offset == 0
-          Time.utc(*args)
-        else
-          tm = Time.local(*args)
-          raise "Failed to convert #{str} to Time" if tm.utc_offset != offset * 60
-          tm
-        end
-      else
-        # without time zone
-        Time.local(*args)
-      end
+    /(\d+)-(\d+)-(\d+) ?(?:(\d+):(\d+):(\d+))?(?:\.(\d+))? ?([+-]\d+:\d+)?/ =~ str
+    if $7
+      subsec = $7.to_i.to_r / ('1' + '0' * ($7.length)).to_i
     end
-    #Time.local(*str.split(/[- :\.]/).collect do |n| n.to_i; end)
+    convert_to_time($1.to_i, $2.to_i, $3.to_i, $4.to_i, $5.to_i, $6.to_i, subsec, $8)
   end
 
   def string_to_datetime(str)
-    /(\d+-\d+-\d+ ?(?:\d+:\d+:\d+)?(?:\.\d+)?) ?([+-]\d+:\d+)?/ =~ str
-    if $2
-      # with time zone
-      DateTime.parse(str)
-    else
-      tm = string_to_time(str)
-      DateTime.parse(str + timezone_string(*((tm.utc_offset / 60).divmod 60)))
+    /(\d+)-(\d+)-(\d+) ?(?:(\d+):(\d+):(\d+))?(?:\.(\d+))? ?([+-]\d+:\d+)?/ =~ str
+    if $7
+      subsec = $7.to_i.to_r / ('1' + '0' * ($7.length)).to_i
     end
+    convert_to_datetime($1.to_i, $2.to_i, $3.to_i, $4.to_i, $5.to_i, $6.to_i, subsec, $8)
   end
 
   def setup



More information about the ruby-oci8-commit mailing list