From 5da13d60508f0ebacb3ba5afd3d29eb00c7ca59e Mon Sep 17 00:00:00 2001 From: Soutaro Matsumoto Date: Wed, 29 Oct 2014 01:28:52 +0900 Subject: Skip to_time if the value is a Time instance This skips calling `#to_time` if the value is a instance of subclass of `Time`, like `ActiveSupport::TimeWithZone`. --- lib/axlsx/workbook/worksheet/cell.rb | 6 +++++- test/workbook/worksheet/tc_cell.rb | 13 +++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/axlsx/workbook/worksheet/cell.rb b/lib/axlsx/workbook/worksheet/cell.rb index cd9e1829..3136d0e0 100644 --- a/lib/axlsx/workbook/worksheet/cell.rb +++ b/lib/axlsx/workbook/worksheet/cell.rb @@ -452,7 +452,11 @@ module Axlsx v when :time self.style = STYLE_DATE if self.style == 0 - v.respond_to?(:to_time) ? v.to_time : v + if !v.is_a?(Time) && v.respond_to?(:to_time) + v.to_time + else + v + end when :float v.to_f when :integer diff --git a/test/workbook/worksheet/tc_cell.rb b/test/workbook/worksheet/tc_cell.rb index f9761330..5ae2a51f 100644 --- a/test/workbook/worksheet/tc_cell.rb +++ b/test/workbook/worksheet/tc_cell.rb @@ -119,6 +119,19 @@ class TestCell < Test::Unit::TestCase assert_equal("2012-10-10T12:24", @c.send(:cast_value, "2012-10-10T12:24")) end + def test_cast_time_subclass + subtime = Class.new(Time) do + def to_time + raise "#to_time of Time subclass should not be called" + end + end + + time = subtime.now + + @c.type = :time + assert_equal(time, @c.send(:cast_value, time)) + end + def test_color assert_raise(ArgumentError) { @c.color = -1.1 } assert_nothing_raised { @c.color = "FF00FF00" } -- cgit v1.2.3