OSDN Git Service

Added some module functions related to DateTime or Time.
authordaigo <beatles@users.sourceforge.jp>
Sat, 26 Jun 2010 06:14:14 +0000 (15:14 +0900)
committerDaigo Moriwaki <daigo@debian.org>
Sat, 26 Jun 2010 06:14:14 +0000 (15:14 +0900)
shogi_server/util.rb
test/TC_ALL.rb
test/TC_util.rb [new file with mode: 0644]

index ebf8c9d..cd477af 100644 (file)
@@ -17,6 +17,7 @@
 ## along with this program; if not, write to the Free Software
 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 ## along with this program; if not, write to the Free Software
 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
+require 'date'
 require 'fileutils'
 require 'pathname'
 
 require 'fileutils'
 require 'pathname'
 
@@ -67,4 +68,28 @@ module ShogiServer
   end
   module_function :is_writable_file?
 
   end
   module_function :is_writable_file?
 
+  # Convert a DateTime insntace to a Time instance.
+  #
+  def datetime2time(dt)
+    return Time.mktime dt.year, dt.month, dt.day, dt.hour, dt.min, dt.sec
+  end
+  module_function :datetime2time
+
+  # Convert a Time instance to a DateTime instance
+  #
+  def time2datetime(time)
+    return DateTime.new(time.year, time.mon, time.mday,
+                        time.hour, time.min, time.sec)
+  end
+  module_function :time2datetime
+
+  # Parse string representing a day-of-week and return a coresponding
+  # integer value: 1 (Monday) - 7 (Sunday)
+  #
+  def parse_dow(str)
+    index = Date::DAYNAMES.index(str) || Date::ABBR_DAYNAMES.index(str)
+    return nil if index.nil?
+    return index == 0 ? 7 : index
+  end
+  module_function :parse_dow
 end
 end
index c8463be..9c2f290 100644 (file)
@@ -22,3 +22,4 @@ require 'TC_player'
 require 'TC_rating'
 require 'TC_uchifuzume'
 require 'TC_usi'
 require 'TC_rating'
 require 'TC_uchifuzume'
 require 'TC_usi'
+require 'TC_util'
diff --git a/test/TC_util.rb b/test/TC_util.rb
new file mode 100644 (file)
index 0000000..d3d4931
--- /dev/null
@@ -0,0 +1,37 @@
+$:.unshift File.join(File.dirname(__FILE__), "..")
+require 'test/unit'
+require 'shogi_server/util'
+
+class TestShogiServer < Test::Unit::TestCase
+
+  def test_datetime2time
+    now = DateTime.new(2010, 6, 10, 21, 20, 15) # Thu
+
+    assert_instance_of Time, ShogiServer::datetime2time(now) 
+    assert_equal Time.mktime(2010, 6, 10, 21, 20, 15), ShogiServer::datetime2time(now) 
+  end
+
+  def test_time2datetime
+    now = Time.mktime(2010, 6, 10, 21, 20, 15)
+    assert_instance_of DateTime, ShogiServer::time2datetime(now)
+    assert_equal DateTime.new(2010, 6, 10, 21, 20, 15), ShogiServer::time2datetime(now)
+  end
+
+  def test_parse_dow
+    assert_equal 7, ShogiServer.parse_dow("Sun")
+    assert_equal 1, ShogiServer.parse_dow("Mon")
+    assert_equal 2, ShogiServer.parse_dow("Tue")
+    assert_equal 3, ShogiServer.parse_dow("Wed")
+    assert_equal 4, ShogiServer.parse_dow("Thu")
+    assert_equal 5, ShogiServer.parse_dow("Fri")
+    assert_equal 6, ShogiServer.parse_dow("Sat")
+    assert_equal 7, ShogiServer.parse_dow("Sunday")
+    assert_equal 1, ShogiServer.parse_dow("Monday")
+    assert_equal 2, ShogiServer.parse_dow("Tuesday")
+    assert_equal 3, ShogiServer.parse_dow("Wednesday")
+    assert_equal 4, ShogiServer.parse_dow("Thursday")
+    assert_equal 5, ShogiServer.parse_dow("Friday")
+    assert_equal 6, ShogiServer.parse_dow("Saturday")
+  end
+
+end