Skip to content

Commit 843dd99

Browse files
authored
Merge pull request #373 from OpenDataServices/342-last-modified-does-not-convert
ODSReader: Fix parsing date and number formatting from .ods files
2 parents bb5c539 + 44fa738 commit 843dd99

File tree

14 files changed

+60
-25
lines changed

14 files changed

+60
-25
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,15 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
66

77
## [Unreleased]
88

9+
## [0.15.4] - 2021-03-08
10+
911
### Fixed
1012

11-
## [0.15.3] - 2020-02-23
13+
- Fix parsing date and number formatting from .ods files https://github.com/OpenDataServices/flatten-tool/pull/373
14+
15+
## [0.15.3] - 2021-02-23
16+
17+
### Fixed
1218

1319
- use-titles: Use $ref'erring title if available https://github.com/OpenDataServices/flatten-tool/pull/368
1420
- create-template --no-deprecated-fields: Did not work if deprecated element at same level as a $ref https://github.com/OpenDataServices/flatten-tool/issues/185#issuecomment-719587348

examples/iati.ods

11.4 KB
Binary file not shown.

examples/iati.xlsx

6.5 KB
Binary file not shown.

examples/iati/expected.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version='1.0' encoding='utf-8'?>
22
<iati-activities>
33
<!--XML generated by flatten-tool-->
4-
<iati-activity>
4+
<iati-activity last-updated-datetime="2011-10-01T00:00:00+00:00">
55
<iati-identifier>AA-AAA-123456789-ABC123</iati-identifier>
66
<reporting-org ref="AA-AAA-123456789" type="40">
77
<narrative>Organisation name</narrative>
@@ -28,7 +28,7 @@
2828
<value value-date="2012-03-03">20</value>
2929
</transaction>
3030
</iati-activity>
31-
<iati-activity>
31+
<iati-activity last-updated-datetime="2016-01-01T00:00:00+00:00">
3232
<iati-identifier>AA-AAA-123456789-ABC124</iati-identifier>
3333
<reporting-org ref="AA-AAA-123456789" type="40">
3434
<narrative>Organisation name</narrative>

examples/iati/main.csv

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
iati-identifier,reporting-org/@ref,reporting-org/@type,reporting-org/narrative,participating-org/@role,participating-org/@ref,activity-status/@code,activity-date/@type,activity-date/@iso-date,recipient-country/0/@code,recipient-country/0/@percentage,recipient-country/1/@code,recipient-country/1/@percentage,title/narrative,description/narrative
2-
AA-AAA-123456789-ABC123,AA-AAA-123456789,40,Organisation name,1,AA-AAA-123456789,2,1,2011-10-01,AF,40,XK,60,A title,A description
3-
AA-AAA-123456789-ABC124,AA-AAA-123456789,40,Organisation name,1,AA-AAA-123456789,3,2,2016-01-01,AG,30,XK,70,Another title,Another description
1+
iati-identifier,reporting-org/@ref,reporting-org/@type,reporting-org/narrative,participating-org/@role,participating-org/@ref,activity-status/@code,activity-date/@type,activity-date/@iso-date,recipient-country/0/@code,recipient-country/0/@percentage,recipient-country/1/@code,recipient-country/1/@percentage,title/narrative,description/narrative,@last-updated-datetime
2+
AA-AAA-123456789-ABC123,AA-AAA-123456789,40,Organisation name,1,AA-AAA-123456789,2,1,2011-10-01,AF,40,XK,60,A title,A description,2011-10-01T00:00:00+00:00
3+
AA-AAA-123456789-ABC124,AA-AAA-123456789,40,Organisation name,1,AA-AAA-123456789,3,2,2016-01-01,AG,30,XK,70,Another title,Another description,2016-01-01T00:00:00+00:00

flattentool/ODSReader.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,16 @@
1515
# Thanks to grt for the fixes
1616
# https://github.com/marcoconti83/read-ods-with-odfpy
1717

18-
import re
1918
from collections import OrderedDict
19+
from datetime import datetime
2020

21+
import backports.datetime_fromisoformat
2122
import odf.opendocument
2223
from odf.table import Table, TableCell, TableRow
2324

25+
# Backport for datetime.fromisoformat, which is new in Python 3.7
26+
backports.datetime_fromisoformat.MonkeyPatch.patch_fromisoformat()
27+
2428

2529
# http://stackoverflow.com/a/4544699/1846474
2630
class GrowingList(list):
@@ -74,23 +78,29 @@ def readSheet(self, sheet):
7478
)
7579
)
7680
if value_type == "float":
81+
value = cell.attributes.get(
82+
(
83+
"urn:oasis:names:tc:opendocument:xmlns:office:1.0",
84+
"value",
85+
)
86+
)
7787
if "." in str(cell):
78-
arrCells[count] = float(str(cell))
88+
arrCells[count] = float(value)
7989
else:
80-
arrCells[count] = int(str(cell))
90+
arrCells[count] = int(value)
8191
elif value_type == "date":
8292
date_value = cell.attributes.get(
8393
(
8494
"urn:oasis:names:tc:opendocument:xmlns:office:1.0",
8595
"date-value",
8696
)
8797
)
88-
# Add UTC timezone to naive datetime strings
89-
if re.match(
90-
r"^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d$", date_value
91-
):
92-
date_value += "Z"
93-
arrCells[count] = date_value
98+
# fromisoformat assumes microseconds appear as 3 or
99+
# 6 digits, whereas ods drops trailing 0s, so can
100+
# have 1-6 digits, so pad some extra 0s
101+
if "." in date_value:
102+
date_value = date_value.ljust(26, "0")
103+
arrCells[count] = datetime.fromisoformat(date_value)
94104
else:
95105
arrCells[count] = str(cell)
96106
count += 1
10.8 KB
Binary file not shown.
112 Bytes
Binary file not shown.
5.4 KB
Binary file not shown.
-1.44 KB
Binary file not shown.

0 commit comments

Comments
 (0)