Skip to content

Commit e6b6589

Browse files
committed
[nixio] Use older methods for datetime <-> string conversion
isoformat() and timestamp() don't exist in older versions of the standard library, so we do the same conversions using strftime() and strptime().
1 parent 1709301 commit e6b6589

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

neo/io/nixio.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@
6363
TIMEANNOTATION = "TIME"
6464
MIN_NIX_VER = Version("1.5.0")
6565

66+
datefmt = "%Y-%m-%d"
67+
timefmt = "%H:%M:%S.%f"
68+
datetimefmt = datefmt + "T" + timefmt
69+
6670

6771
def stringify(value):
6872
if value is None:
@@ -95,11 +99,11 @@ def dt_to_nix(dt):
9599
type.
96100
"""
97101
if isinstance(dt, datetime):
98-
return dt.isoformat(), DATETIMEANNOTATION
102+
return dt.strftime(datetimefmt), DATETIMEANNOTATION
99103
if isinstance(dt, date):
100-
return dt.isoformat(), DATEANNOTATION
104+
return dt.strftime(datefmt), DATEANNOTATION
101105
if isinstance(dt, time):
102-
return dt.isoformat(), TIMEANNOTATION
106+
return dt.strftime(timefmt), TIMEANNOTATION
103107
# Unknown: returning as is
104108
return dt
105109

@@ -110,11 +114,14 @@ def dt_from_nix(nixdt, annotype):
110114
distinguish between the three source types (date, time, and datetime).
111115
"""
112116
if annotype == DATEANNOTATION:
113-
return date.fromisoformat(nixdt)
117+
dt = datetime.strptime(nixdt, datefmt)
118+
return dt.date()
114119
if annotype == TIMEANNOTATION:
115-
return time.fromisoformat(nixdt)
120+
dt = datetime.strptime(nixdt, timefmt)
121+
return dt.time()
116122
if annotype == DATETIMEANNOTATION:
117-
return datetime.fromisoformat(nixdt)
123+
dt = datetime.strptime(nixdt, datetimefmt)
124+
return dt
118125
# Unknown type: older (or newer) IO version?
119126
# Returning as is to avoid data loss.
120127
return nixdt
@@ -612,7 +619,7 @@ def write_block(self, block, use_obj_names=False):
612619
metadata["neo_name"] = neoname
613620
nixblock.definition = block.description
614621
if block.rec_datetime:
615-
nix_rec_dt = int(block.rec_datetime.timestamp())
622+
nix_rec_dt = int(block.rec_datetime.strftime("%s"))
616623
nixblock.force_created_at(nix_rec_dt)
617624
if block.file_datetime:
618625
fdt, annotype = dt_to_nix(block.file_datetime)
@@ -713,7 +720,7 @@ def _write_segment(self, segment, nixblock):
713720
metadata["neo_name"] = neoname
714721
nixgroup.definition = segment.description
715722
if segment.rec_datetime:
716-
nix_rec_dt = int(segment.rec_datetime.timestamp())
723+
nix_rec_dt = int(segment.rec_datetime.strftime("%s"))
717724
nixgroup.force_created_at(nix_rec_dt)
718725
if segment.file_datetime:
719726
fdt, annotype = dt_to_nix(segment.file_datetime)

0 commit comments

Comments
 (0)