Skip to content

Commit

Permalink
Merge pull request #178 from bbertincourt/support-for-milliseconds-in…
Browse files Browse the repository at this point in the history
…-datetime-format

Support various number of digits after the comma in the timestamp %f …
  • Loading branch information
wimglenn authored Jan 28, 2024
2 parents 8ae5d30 + b1d2121 commit 79f516d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ def strf_date_convert(x, _, type):
"%p": "(?:AM|PM)",
"%M": "[0-9]{2}",
"%S": "[0-9]{2}",
"%f": "[0-9]{6}",
"%f": "[0-9]{1,6}",
"%z": "[+|-][0-9]{2}(:?[0-9]{2})?(:?[0-9]{2})?",
# "%Z": punt
"%j": "[0-9]{1,3}",
Expand Down
17 changes: 17 additions & 0 deletions tests/test_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,23 @@ def test_flexible_datetime_with_colon():
assert r.named["dt"] == datetime(2023, 11, 21, 13, 23, 27)


def test_datetime_with_various_subsecond_precision():
r = parse.parse("{dt:%Y-%m-%d %H:%M:%S.%f}", "2023-11-21 13:23:27.123456")
assert r.named["dt"] == datetime(2023, 11, 21, 13, 23, 27, 123456)

r = parse.parse("{dt:%Y-%m-%d %H:%M:%S.%f}", "2023-11-21 13:23:27.12345")
assert r.named["dt"] == datetime(2023, 11, 21, 13, 23, 27, 123450)

r = parse.parse("{dt:%Y-%m-%d %H:%M:%S.%f}", "2023-11-21 13:23:27.1234")
assert r.named["dt"] == datetime(2023, 11, 21, 13, 23, 27, 123400)

r = parse.parse("{dt:%Y-%m-%d %H:%M:%S.%f}", "2023-11-21 13:23:27.123")
assert r.named["dt"] == datetime(2023, 11, 21, 13, 23, 27, 123000)

r = parse.parse("{dt:%Y-%m-%d %H:%M:%S.%f}", "2023-11-21 13:23:27.0")
assert r.named["dt"] == datetime(2023, 11, 21, 13, 23, 27, 0)


@pytest.mark.skipif(
sys.version_info[0] < 3, reason="Python 3+ required for timezone support"
)
Expand Down

0 comments on commit 79f516d

Please sign in to comment.