Skip to content

Commit

Permalink
Render leading zeros for years 0001-0999
Browse files Browse the repository at this point in the history
  • Loading branch information
kotofos committed Aug 11, 2021
1 parent b21c507 commit fd86c72
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
9 changes: 8 additions & 1 deletion pyhive/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,14 @@ def escape_sequence(self, item):
return '(' + ','.join(l) + ')'

def escape_datetime(self, item, format, cutoff=0):
dt_str = item.strftime(format)
if format.startswith('%Y'):
# patch for #404
dt_str = '{:0>4}{}'.format(
item.strftime('%Y'),
item.strftime(format.lstrip('%Y'))
)
else:
dt_str = item.strftime(format)
formatted = dt_str[:-cutoff] if cutoff and format.endswith(".%f") else dt_str
return "'{}'".format(formatted)

Expand Down
4 changes: 4 additions & 0 deletions pyhive/tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,7 @@ def test_escape_args(self):
("'2020-04-17'",))
self.assertEqual(escaper.escape_args((datetime.datetime(2020, 4, 17, 12, 0, 0, 123456),)),
("'2020-04-17 12:00:00.123456'",))
self.assertEqual(escaper.escape_args((datetime.date(2020, 4, 17),)),
("'2020-04-17'",))
self.assertEqual(escaper.escape_args((datetime.datetime(20, 4, 17, 12, 0, 0, 123456),)),
("'0020-04-17 12:00:00.123456'",))

0 comments on commit fd86c72

Please sign in to comment.