Skip to content

Commit

Permalink
fix #3: accept long date time string 2015-08-17T19:09:59.999999
Browse files Browse the repository at this point in the history
  • Loading branch information
chfw committed Sep 23, 2015
1 parent 68736d1 commit 80c9657
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
22 changes: 13 additions & 9 deletions pyexcel_ods/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
load_data as read_data,
store_data as write_data
)
import datetime
import odf.opendocument
from odf.table import TableRow, TableCell, Table
from odf.text import P
Expand All @@ -50,14 +49,19 @@ def float_value(value):


def date_value(value):
if len(value) == 10:
ret = datetime.datetime.strptime(value, "%Y-%m-%d")
ret = ret.date()
elif len(value) == 19:
ret = datetime.datetime.strptime(value, "%Y-%m-%dT%H:%M:%S")
else:
raise Exception("Bad date format")
return ret
try:
if len(value) == 10:
ret = datetime.datetime.strptime(value, "%Y-%m-%d")
ret = ret.date()
elif len(value) == 19:
ret = datetime.datetime.strptime(value, "%Y-%m-%dT%H:%M:%S")
elif len(value) > 19:
ret = datetime.datetime.strptime(value[0:26], "%Y-%m-%dT%H:%M:%S.%f")
else:
raise Exception("Bad date value %s" % value)
return ret
except:
raise Exception("Bad date value %s" % value)


def ods_date_value(value):
Expand Down
29 changes: 27 additions & 2 deletions tests/test_bug_fixes.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,34 @@ def test_date_util_parse():
value = "2015-08-17"
d = date_value(value)
assert d.strftime("%Y-%m-%d") == "2015-08-17"

value = "2015-08-17T19:20:59.999999"
d = date_value(value)
assert d.strftime("%Y-%m-%dT%H:%M:%S") == "2015-08-17T19:20:59"
value = "2015-08-17T19:20:59.99999"
d = date_value(value)
assert d.strftime("%Y-%m-%dT%H:%M:%S") == "2015-08-17T19:20:59"
value = "2015-08-17T19:20:59.999999999999999"
d = date_value(value)
assert d.strftime("%Y-%m-%dT%H:%M:%S") == "2015-08-17T19:20:59"

@raises(Exception)
def test_invalid_date():
from pyexcel_ods import date_value
value = "2015-08-"
date_value(value)
date_value(value)

@raises(Exception)
def test_fake_date_time_10():
from pyexcel_ods import date_value
date_value("1234567890")

@raises(Exception)
def test_fake_date_time_19():
from pyexcel_ods import date_value
date_value("1234567890123456789")

@raises(Exception)
def test_fake_date_time_20():
from pyexcel_ods import date_value
date_value("12345678901234567890")

0 comments on commit 80c9657

Please sign in to comment.