Skip to content

Commit

Permalink
Fixed edge case involving date time string with seconds.
Browse files Browse the repository at this point in the history
When supplying a date time string with seconds (11/5/2013 11:45:00), the resulting DateTime object would not round the fractional seconds resulting in a time string of 11/5/2013 11:44:59. 

Time is now properly rounded before returning the DateTime object.
  • Loading branch information
Jeremy Ward committed Feb 4, 2014
1 parent ce1767d commit c9508fe
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 5 deletions.
20 changes: 15 additions & 5 deletions lib/roo/excelx.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,7 @@ def cell(row, col, sheet=nil)
yyyy,mm,dd = @cell[sheet][[row,col]].split('-')
return Date.new(yyyy.to_i,mm.to_i,dd.to_i)
elsif celltype(row,col,sheet) == :datetime
date_part,time_part = @cell[sheet][[row,col]].split(' ')
yyyy,mm,dd = date_part.split('-')
hh,mi,ss = time_part.split(':')
return DateTime.civil(yyyy.to_i,mm.to_i,dd.to_i,hh.to_i,mi.to_i,ss.to_i)
return create_datetime_from( @cell[sheet][[row,col]] )
end
@cell[sheet][[row,col]]
end
Expand Down Expand Up @@ -367,7 +364,7 @@ def set_cell_values(sheet,x,y,i,v,value_type,formula,
when :date
(base_date+v.to_i).strftime("%Y-%m-%d")
when :datetime
(base_date+v.to_f.round(6)).strftime("%Y-%m-%d %H:%M:%S")
(base_date+v.to_f.round(6)).strftime("%Y-%m-%d %H:%M:%S.%N")
when :percentage
v.to_f
when :time
Expand Down Expand Up @@ -671,4 +668,17 @@ def read_base_date
base_date
end

def create_datetime_from(datetime_string)
date_part,time_part = round_time_from(datetime_string).split(' ')
yyyy,mm,dd = date_part.split('-')
hh,mi,ss = time_part.split(':')
return DateTime.civil(yyyy.to_i,mm.to_i,dd.to_i,hh.to_i,mi.to_i,ss.to_i)
end

def round_time_from(datetime_string)
date_part,time_part = datetime_string.split(' ')
yyyy,mm,dd = date_part.split('-')
hh,mi,ss = time_part.split(':')
Time.new(yyyy.to_i, mm.to_i, dd.to_i, hh.to_i, mi.to_i, ss.to_r).round(0).strftime("%Y-%m-%d %H:%M:%S")
end
end # class
Binary file modified test/files/datetime.ods
Binary file not shown.
Binary file modified test/files/datetime.xls
Binary file not shown.
Binary file modified test/files/datetime.xlsx
Binary file not shown.
5 changes: 5 additions & 0 deletions test/files/datetime.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@
<Cell ss:StyleID="s21"><Data ss:Type="DateTime">1961-11-21T00:00:00.000</Data></Cell>
<Cell ss:StyleID="s21"><Data ss:Type="DateTime">1961-11-21T00:00:00.000</Data></Cell>
</Row>
<Row>
<Cell ss:StyleID="s20"><Data ss:Type="DateTime">2013-11-05T11:45:00.000</Data></Cell>
<Cell ss:StyleID="s20"><Data ss:Type="DateTime">2013-11-05T11:45:00.000</Data></Cell>
<Cell ss:StyleID="s20"><Data ss:Type="DateTime">2013-11-05T11:45:00.000</Data></Cell>
</Row>
</Table>
<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
<PageSetup>
Expand Down
3 changes: 3 additions & 0 deletions test/test_roo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1413,6 +1413,9 @@ def test_datetime
assert_equal Date.new(1961,11,21), oo.cell('a',7)
assert_equal Date.new(1961,11,21), oo.cell('b',7)
assert_equal Date.new(1961,11,21), oo.cell('c',7)
assert_equal DateTime.new(2013,11,5,11,45,00), oo.cell('a',8)
assert_equal DateTime.new(2013,11,5,11,45,00), oo.cell('b',8)
assert_equal DateTime.new(2013,11,5,11,45,00), oo.cell('c',8)
end
end

Expand Down

0 comments on commit c9508fe

Please sign in to comment.