Skip to content

Commit b1d2121

Browse files
committed
support various number of digits after the comma in the timestamp %f format
1 parent 8ae5d30 commit b1d2121

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

parse.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ def strf_date_convert(x, _, type):
307307
"%p": "(?:AM|PM)",
308308
"%M": "[0-9]{2}",
309309
"%S": "[0-9]{2}",
310-
"%f": "[0-9]{6}",
310+
"%f": "[0-9]{1,6}",
311311
"%z": "[+|-][0-9]{2}(:?[0-9]{2})?(:?[0-9]{2})?",
312312
# "%Z": punt
313313
"%j": "[0-9]{1,3}",

tests/test_parse.py

+17
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,23 @@ def test_flexible_datetime_with_colon():
375375
assert r.named["dt"] == datetime(2023, 11, 21, 13, 23, 27)
376376

377377

378+
def test_datetime_with_various_subsecond_precision():
379+
r = parse.parse("{dt:%Y-%m-%d %H:%M:%S.%f}", "2023-11-21 13:23:27.123456")
380+
assert r.named["dt"] == datetime(2023, 11, 21, 13, 23, 27, 123456)
381+
382+
r = parse.parse("{dt:%Y-%m-%d %H:%M:%S.%f}", "2023-11-21 13:23:27.12345")
383+
assert r.named["dt"] == datetime(2023, 11, 21, 13, 23, 27, 123450)
384+
385+
r = parse.parse("{dt:%Y-%m-%d %H:%M:%S.%f}", "2023-11-21 13:23:27.1234")
386+
assert r.named["dt"] == datetime(2023, 11, 21, 13, 23, 27, 123400)
387+
388+
r = parse.parse("{dt:%Y-%m-%d %H:%M:%S.%f}", "2023-11-21 13:23:27.123")
389+
assert r.named["dt"] == datetime(2023, 11, 21, 13, 23, 27, 123000)
390+
391+
r = parse.parse("{dt:%Y-%m-%d %H:%M:%S.%f}", "2023-11-21 13:23:27.0")
392+
assert r.named["dt"] == datetime(2023, 11, 21, 13, 23, 27, 0)
393+
394+
378395
@pytest.mark.skipif(
379396
sys.version_info[0] < 3, reason="Python 3+ required for timezone support"
380397
)

0 commit comments

Comments
 (0)