Skip to content

Fix timestamps with timezones #80

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/fast_excel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -451,11 +451,11 @@ def write_value(row_number, cell_number, value, format = nil)
if value.is_a?(Numeric)
write_number(row_number, cell_number, value, format)
elsif defined?(Date) && value.is_a?(Date)
write_datetime(row_number, cell_number, FastExcel.lxw_datetime(value.to_datetime), format)
write_datetime(row_number, cell_number, FastExcel.lxw_datetime(value.to_datetime.new_offset('+0000')), format)
elsif value.is_a?(Time)
write_number(row_number, cell_number, FastExcel.date_num(value), format)
write_number(row_number, cell_number, FastExcel.date_num(value.getutc, 0), format)
elsif defined?(DateTime) && value.is_a?(DateTime)
write_number(row_number, cell_number, FastExcel.date_num(value), format)
write_number(row_number, cell_number, FastExcel.date_num(value.new_offset('+0000'), 0), format)
elsif value.is_a?(TrueClass) || value.is_a?(FalseClass)
write_boolean(row_number, cell_number, value ? 1 : 0, format)
elsif value.is_a?(FastExcel::Formula)
Expand Down
72 changes: 50 additions & 22 deletions test/date_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,83 @@

describe "FastExcel.date_num" do

it "should save correct date" do
it "should correct save a Date" do
workbook = FastExcel.open("test.xlsx", constant_memory: true)
worksheet = workbook.add_worksheet("Payments Report")

date_format = workbook.number_format("[$-409]m/d/yy h:mm AM/PM;@")
worksheet.set_column(0, 0, 20, date_format)

date = DateTime.parse('2017-03-01 15:15:15 +0000').to_time
value = DateTime.parse('2017-03-01 15:15:15 +0000').to_time

worksheet.write_number(0, 0, FastExcel.date_num(date), nil)
worksheet.write_number(0, 0, FastExcel.date_num(value), nil)
workbook.close

data = parse_xlsx_as_matrix("test.xlsx")

assert_equal(data[0][0].to_time, date)
assert_equal(value, data[0][0].to_time)
end

end

describe "FastExcel.write_value" do
# Write a value to XLSX in a specific format, then parse it and return it.
def convert_via_xlsx(value, number_format)
tmp_filename = 'test.xlsx'
workbook = FastExcel.open(tmp_filename, constant_memory: true)
worksheet = workbook.add_worksheet("Conversion test")

it "should save correct datetime" do
workbook = FastExcel.open(constant_memory: true)
worksheet = workbook.add_worksheet

format = workbook.number_format("yyyy-mm-dd hh:mm:ss")
value = DateTime.parse('2017-03-01 15:15:15 +0000')

format = workbook.number_format(number_format)
worksheet.write_value(0, 0, value, format)
workbook.close

data = parse_xlsx_as_matrix(workbook.filename)
data = parse_xlsx_as_matrix(tmp_filename)

assert_equal(data[0][0], value)
return data[0][0]
end

it "should save correct date" do
workbook = FastExcel.open(constant_memory: true)
worksheet = workbook.add_worksheet
it "should correctly save a DateTime without zone offset" do
value = DateTime.parse('2017-03-01 15:15:15 +0000')
xlsx_value = convert_via_xlsx(value, "yyyy-mm-dd hh:mm:ss")
assert_equal(value, xlsx_value)
end

format = workbook.number_format("yyyy-mm-dd")
value = Date.parse('2017-03-01')
it "should correctly save and convert a DateTime with offset to +0000" do
value = DateTime.parse('2017-01-01 15:11:22 +0100')
utc_value = DateTime.parse('2017-01-01 14:11:22 +0000')
xlsx_value = convert_via_xlsx(value, "yyyy-mm-dd hh:mm:ss")
assert_equal(utc_value, xlsx_value)
end

worksheet.write_value(0, 0, value, format)
workbook.close
it "should correctly save and convert a DateTime with timezone to +0000" do
value = DateTime.parse('2017-01-01 15:11:22 CET')
utc_value = DateTime.parse('2017-01-01 14:11:22 +0000')
xlsx_value = convert_via_xlsx(value, "yyyy-mm-dd hh:mm:ss")
assert_equal(utc_value, xlsx_value)
end

data = parse_xlsx_as_matrix(workbook.filename)
it "should correctly save and convert a DateTime with DST timezone to +0000" do
value = DateTime.parse('2017-07-01 15:11:22 CEST')
utc_value = DateTime.parse('2017-07-01 13:11:22 +0000')
xlsx_value = convert_via_xlsx(value, "yyyy-mm-dd hh:mm:ss")
assert_equal(utc_value, xlsx_value)
end

assert_equal(data[0][0], value)
it "should correctly save a Time without zone offset" do
value = Time.new(2022, 1, 20, 14, 43, 10, '+00:00')
xlsx_value = convert_via_xlsx(value, "yyyy-mm-dd hh:mm:ss")
assert_equal(value.to_datetime, xlsx_value)
end

it "should correctly save a Time with zone offset" do
value = Time.new(2022, 1, 20, 14, 43, 10, '+01:00')
xlsx_value = convert_via_xlsx(value, "yyyy-mm-dd hh:mm:ss")
assert_equal(value.to_datetime, xlsx_value)
end

it "should correctly save a Date" do
value = Date.parse('2017-03-01')
xlsx_value = convert_via_xlsx(value, "yyyy-mm-dd")
assert_equal(value.to_datetime, xlsx_value)
end
end