Skip to content

Commit

Permalink
format datetime.date objs appropriately for TSDB
Browse files Browse the repository at this point in the history
Resolves #291
  • Loading branch information
goodmami committed Jul 1, 2020
1 parent d6dcb43 commit 155989b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

* `delphin.tfs.FeatureStructure` raises a `TFSError` when attempting
to assign a value on a non-subscriptable feature value ([#293])
* `delphin.tsdb.format()` formats `datetime.date` objects in the TSDB
date format ([#291])


## [v1.2.4]
Expand Down Expand Up @@ -1384,5 +1386,6 @@ information about changes, except for
[#285]: https://github.com/delph-in/pydelphin/issues/285
[#288]: https://github.com/delph-in/pydelphin/issues/288
[#289]: https://github.com/delph-in/pydelphin/issues/289
[#291]: https://github.com/delph-in/pydelphin/issues/291
[#293]: https://github.com/delph-in/pydelphin/issues/293
[#296]: https://github.com/delph-in/pydelphin/issues/296
9 changes: 5 additions & 4 deletions delphin/tsdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from gzip import open as gzopen
import tempfile
import shutil
from datetime import datetime
from datetime import datetime, date
import warnings

from delphin.exceptions import PyDelphinException, PyDelphinWarning
Expand Down Expand Up @@ -64,7 +64,7 @@

RawValue = Union[str, None]
RawRecord = Sequence[RawValue]
Value = Union[str, int, float, datetime, None]
Value = Union[str, int, float, datetime, date, None]
Record = Sequence[Value]
Records = Iterable[Record]
ColumnMap = Dict[str, Value] # e.g., a partial Record
Expand Down Expand Up @@ -680,10 +680,11 @@ def format(datatype: str,
else:
default = str(default) # ensure it is a string
raw_value = default
elif datatype == ':date' and isinstance(value, datetime):
elif datatype == ':date' and isinstance(value, (date, datetime)):
month = _MONTHS[value.month]
pattern = f'{value.day!s}-{month}-%Y'
if (value.hour, value.minute, value.second) != (0, 0, 0):
if (isinstance(value, datetime)
and (value.hour, value.minute, value.second) != (0, 0, 0)):
pattern += ' %H:%M:%S'
raw_value = value.strftime(pattern)
else:
Expand Down
3 changes: 2 additions & 1 deletion tests/tsdb_test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import pathlib
from collections import OrderedDict
from datetime import datetime
from datetime import datetime, date

import pytest

Expand Down Expand Up @@ -217,6 +217,7 @@ def test_format():
assert tsdb.format(':integer', None) == '-1'
assert tsdb.format(':integer', None, default='1') == '1'
assert tsdb.format(':date', datetime(1999, 9, 8)) == '8-sep-1999'
assert tsdb.format(':date', date(1999, 9, 8)) == '8-sep-1999' # Issue #291


def test_open(single_item_skeleton, gzipped_single_item_skeleton):
Expand Down

0 comments on commit 155989b

Please sign in to comment.