Skip to content

Commit

Permalink
Merge pull request #5 from bwaldvogel/master
Browse files Browse the repository at this point in the history
credits go to @bwaldvogel, support times with more than 24 hours
  • Loading branch information
chfw committed Jan 11, 2016
2 parents aafd1a4 + 5625dfe commit a75c5ba
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
12 changes: 11 additions & 1 deletion pyexcel_ods3/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ def time_value(value):
hour = int(value[2:4])
minute = int(value[5:7])
second = int(value[8:10])
ret = datetime.time(hour, minute, second)
if hour < 24:
ret = datetime.time(hour, minute, second)
else:
ret = datetime.timedelta(hours=hour, minutes=minute, seconds=second)
return ret


Expand Down Expand Up @@ -85,6 +88,7 @@ def boolean_value(value):
str: "string",
datetime.date: "date",
datetime.time: "time",
datetime.timedelta: "timedelta",
bool: "boolean"
}

Expand Down Expand Up @@ -178,6 +182,12 @@ def write_row(self, array):
value_type = ODS_WRITE_FORMAT_COVERSION[type(cell)]
if value_type == "time":
cell = cell.strftime("PT%HH%MM%SS")
elif value_type == "timedelta":
hours = cell.days * 24 + cell.seconds // 3600
minutes = (cell.seconds // 60) % 60
seconds = cell.seconds % 60
cell = "PT%02dH%02dM%02dS" % (hours, minutes, seconds)
value_type = "time"
self.native_sheet[self.current_row, count].set_value(
cell,
value_type=value_type)
Expand Down
3 changes: 3 additions & 0 deletions tests/base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pyexcel
import datetime
import os
from nose.tools import raises

Expand Down Expand Up @@ -123,6 +124,8 @@ def test_formats(self):
assert self.data["Sheet1"][1][1].strftime(time_format) == "12:12:11"
assert self.data["Sheet1"][2][1].strftime(time_format) == "12:00:00"
assert self.data["Sheet1"][3][1] == 0
assert self.data["Sheet1"][4][1] == datetime.timedelta(hours=27, minutes=17, seconds=54)
assert self.data["Sheet1"][5][1] == "Other"
# boolean
assert self.data["Sheet1"][0][2] == "Boolean"
assert self.data["Sheet1"][1][2] is True
Expand Down
Binary file modified tests/fixtures/ods_formats.ods
Binary file not shown.

0 comments on commit a75c5ba

Please sign in to comment.