Skip to content

Commit

Permalink
handle date format
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanwang520 committed Aug 9, 2019
1 parent 241ffcb commit 52501a4
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 7 deletions.
2 changes: 1 addition & 1 deletion poi/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "0.1.4"
__version__ = "0.1.5"


from .sheet import Sheet # noqa
Expand Down
4 changes: 4 additions & 0 deletions poi/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,9 @@ def __init__(
cell_width=None,
cell_height=None,
cell_style=None,
datetime_format=None,
date_format=None,
time_format=None,
*args,
**kwargs,
):
Expand All @@ -285,6 +287,8 @@ def __init__(

self.cell_style = cell_style or {}
self.date_format = date_format
self.datetime_format = datetime_format
self.time_format = time_format
self.columns = []
for col in columns:
assert isinstance(col, (tuple, dict))
Expand Down
10 changes: 6 additions & 4 deletions poi/visitors/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,13 @@ def get_obj_attr(obj, field):
val = column.render(item)
else:
val = column.render(item, column)
if isinstance(val, datetime.datetime):
fmt["num_format"] = self.datetime_format or "yyyy-mm-dd hh:mm:ss"
if isinstance(val, datetime.date):
fmt["num_format"] = self.date_format or "yyyy-mm-dd"
if isinstance(val, datetime.time):
fmt["num_format"] = self.time_format or "hh:mm:ss"

if self.date_format and isinstance(
val, (datetime.date, datetime.datetime)
):
fmt["num_format"] = self.date_format
writer.write(row + i + 1, col + j, val, {**self.cell_format, **fmt})

@visitor.register
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "poi"
version = "0.1.4"
version = "0.1.5"
description = ""
authors = ["Ryan Wang <[email protected]>"]

Expand Down
2 changes: 1 addition & 1 deletion tests/test_poi.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@


def test_version():
assert __version__ == "0.1.4"
assert __version__ == "0.1.5"


def assert_match_snapshot(sheet: Sheet, snapshot):
Expand Down

0 comments on commit 52501a4

Please sign in to comment.