From f56addc009bbb29e19c4385f309084f76b06f521 Mon Sep 17 00:00:00 2001 From: Bob Frost Date: Fri, 27 Dec 2019 17:52:26 -0500 Subject: [PATCH] issue-310 off by one error correct comment correct comment correct comment --- lib/roo/excelx/cell/time.rb | 2 +- spec/lib/roo/excelx/cell/time_spec.rb | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 spec/lib/roo/excelx/cell/time_spec.rb diff --git a/lib/roo/excelx/cell/time.rb b/lib/roo/excelx/cell/time.rb index a1f08642..5fed1e2f 100644 --- a/lib/roo/excelx/cell/time.rb +++ b/lib/roo/excelx/cell/time.rb @@ -13,7 +13,7 @@ def initialize(value, formula, excelx_type, style, link, base_date, coordinate) super @format = excelx_type.last @datetime = create_datetime(base_date, value) - @value = link ? Roo::Link.new(link, value) : (value.to_f * 86_400).to_i + @value = link ? Roo::Link.new(link, value) : (value.to_f * 86_400).round.to_i end def formatted_value diff --git a/spec/lib/roo/excelx/cell/time_spec.rb b/spec/lib/roo/excelx/cell/time_spec.rb new file mode 100644 index 00000000..214443f6 --- /dev/null +++ b/spec/lib/roo/excelx/cell/time_spec.rb @@ -0,0 +1,15 @@ +require 'spec_helper' + +RSpec.describe Roo::Excelx::Cell::Time do + it "should set the cell value to the correct number of seconds" do + value = 0.05513888888888888 # '1:19:24' + excelx_type = [:numeric_or_formula, "h:mm:ss"] + base_timestamp = Date.new(1899, 12, 30).to_time.to_i + time_cell = Roo::Excelx::Cell::Time.new(value, nil, excelx_type, 1, nil, base_timestamp, nil) + expect(time_cell.value).to eq(1*60*60 + 19*60 + 24) # '1:19:24' in seconds + # use case from https://github.com/roo-rb/roo/issues/310 + value = 0.523761574074074 # '12:34:13' in seconds + time_cell = Roo::Excelx::Cell::Time.new(value, nil, excelx_type, 1, nil, base_timestamp, nil) + expect(time_cell.value).to eq(12*60*60 + 34*60 + 13) # 12:34:13 in seconds + end +end \ No newline at end of file