Skip to content
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

Fix ods datetime and time export and import #595

Merged
merged 2 commits into from
Jul 13, 2024
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
6 changes: 6 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# History

## Unreleased

### Bugfixes

- Fix time and datetime export in ODS format (#595).

## 3.6.1 (2024-04-04)

### Bugfixes
Expand Down
10 changes: 7 additions & 3 deletions src/tablib/formats/_ods.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,11 @@ def convert_date(val):
return convert_date(date_value)
if value_type == 'time':
time_value = cell.getAttribute('timevalue')
return dt.datetime.strptime(time_value, "%H:%M:%S").time()
try:
return dt.datetime.strptime(time_value, "PT%HH%MM%SS").time()
except ValueError:
# backwards compatibility for times exported with older tablib versions
return dt.datetime.strptime(time_value, "%H:%M:%S").time()
if value_type == 'boolean':
bool_value = cell.getAttribute('booleanvalue')
return bool_value == 'true'
Expand Down Expand Up @@ -158,12 +162,12 @@ def dset_sheet(cls, dataset, ws):
cell = table.TableCell(valuetype="float", value=col)
elif isinstance(col, dt.datetime):
cell = table.TableCell(
valuetype="date", value=col.strftime('%Y-%m-%dT%H:%M:%S')
valuetype="date", datevalue=col.strftime('%Y-%m-%dT%H:%M:%S')
)
elif isinstance(col, dt.date):
cell = table.TableCell(valuetype="date", datevalue=col.strftime('%Y-%m-%d'))
elif isinstance(col, dt.time):
cell = table.TableCell(valuetype="time", timevalue=col.strftime('%H:%M:%S'))
cell = table.TableCell(valuetype="time", timevalue=col.strftime('PT%HH%MM%SS'))
elif col is None:
cell = table.TableCell(valuetype="void")
else:
Expand Down
Binary file modified tests/files/book.ods
Binary file not shown.
22 changes: 22 additions & 0 deletions tests/test_tablib.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import doctest
import json
import pickle
import re
import tempfile
import unittest
from decimal import Decimal
Expand All @@ -13,6 +14,7 @@
from uuid import uuid4

import xlrd
from odf import opendocument, table
from openpyxl.reader.excel import load_workbook

import tablib
Expand Down Expand Up @@ -1214,6 +1216,26 @@ def test_ods_unknown_value_type(self):
dataset = tablib.Dataset().load(fh, 'ods')
self.assertEqual(dataset.pop(), ('abcd',))

def test_ods_export_dates(self):
"""test against odf specification"""
date = dt.date(2019, 10, 4)
date_time = dt.datetime(2019, 10, 4, 12, 30, 8)
time = dt.time(14, 30)
data.append((date, time, date_time))
data.headers = ('date', 'time', 'date/time')
_ods = data.ods
ods_book = opendocument.load(BytesIO(_ods))
cells = ods_book.spreadsheet.getElementsByType(table.TableRow)[1].childNodes
# date value
self.assertEqual(cells[0].getAttribute('datevalue'), '2019-10-04')
# time value
duration_exp = re.compile(r"^P(?:(\d+)Y)?(?:(\d+)M)?(?:(\d+)D)?"
r"(?:T(?:(\d+)H)?(?:(\d+)M)?(?:([\d.]+)S)?)?$")
duration = duration_exp.match(cells[1].getAttribute('timevalue')).groups()
self.assertListEqual([0, 0, 0, 14, 30, 0], [int(v or 0) for v in duration])
# datetime value
self.assertEqual(cells[2].getAttribute('datevalue'), '2019-10-04T12:30:08')


class XLSTests(BaseTestCase):
def test_xls_format_detect(self):
Expand Down
Loading