Skip to content

Update xlsw.py: fix timedelta support #49

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

Merged
merged 4 commits into from
Oct 2, 2021
Merged
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
9 changes: 3 additions & 6 deletions pyexcel_xls/xlsw.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

DEFAULT_DATE_FORMAT = "DD/MM/YY"
DEFAULT_TIME_FORMAT = "HH:MM:SS"
DEFAULT_LONGTIME_FORMAT = "[HH]:MM:SS"
DEFAULT_DATETIME_FORMAT = "%s %s" % (DEFAULT_DATE_FORMAT, DEFAULT_TIME_FORMAT)
EMPTY_SHEET_NOT_ALLOWED = "xlwt does not support a book without any sheets"

Expand Down Expand Up @@ -53,13 +54,9 @@ def write_row(self, array):
style = XFStyle()
style.num_format_str = DEFAULT_DATETIME_FORMAT
elif isinstance(value, datetime.timedelta):
hours = value.days * 24 + value.seconds // 3600
minutes = (value.seconds // 60) % 60
seconds = value.seconds % 60
tmp_array = [hours, minutes, seconds]
value = xlrd.xldate.xldate_from_time_tuple(tmp_array)
value = value.days + value.seconds / 86_400
style = XFStyle()
style.num_format_str = DEFAULT_TIME_FORMAT
style.num_format_str = DEFAULT_LONGTIME_FORMAT
elif isinstance(value, datetime.date):
tmp_array = [value.year, value.month, value.day]
value = xlrd.xldate.xldate_from_date_tuple(tmp_array, 0)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ def test_writing_date_format(self):
assert r[0, 1].strftime("%H:%M:%S") == "11:11:11"
assert isinstance(r[0, 2], datetime.date) is True
assert r[0, 2].strftime("%d/%m/%y %H:%M:%S") == "25/12/14 11:11:11"
assert isinstance(r[0, 2], datetime.timedelta)
assert r[0, 1].strftime("%H:%M:%S") == "3:30:20"
assert isinstance(r[0, 3], datetime.datetime)
assert r[0, 3].strftime("%D-%H:%M:%S") == "03/04/00-08:05:56"
os.unlink(excel_filename)


Expand Down