Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(timedelta): add scientific notation #3346

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/3315-JeanArhancet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Allow scientific notation for numbers in timedelta fields
11 changes: 10 additions & 1 deletion pydantic/datetime_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@
r'$'
)

# Support scientific notation
scientific_notation = re.compile(r'^(?P<sign>[-+]?)(?P<scientific_notation>\d+(.\d+)?[eE][+\-]?\d+)?$')

EPOCH = datetime(1970, 1, 1)
# if greater than this, the number is in ms, if less than or equal it's in seconds
# (in seconds this is 11th October 2603, in ms it's 20th August 1970)
Expand Down Expand Up @@ -228,7 +231,9 @@ def parse_duration(value: StrBytesIntFloat) -> timedelta:
value = value.decode()

try:
match = standard_duration_re.match(value) or iso8601_duration_re.match(value)
match = (
standard_duration_re.match(value) or iso8601_duration_re.match(value) or scientific_notation.match(value)
)
except TypeError:
raise TypeError('invalid type; expected timedelta, string, bytes, int or float')

Expand All @@ -243,6 +248,10 @@ def parse_duration(value: StrBytesIntFloat) -> timedelta:
if kw.get('seconds') and kw.get('microseconds') and kw['seconds'].startswith('-'):
kw['microseconds'] = '-' + kw['microseconds']

if kw.get('scientific_notation'):
# convert scientific notation to decimals
kw['seconds'] = '{:.8s}'.format(kw.pop('scientific_notation'))

kw_ = {k: float(v) for k, v in kw.items() if v is not None}

return sign * timedelta(**kw_) # type: ignore
10 changes: 10 additions & 0 deletions tests/test_datetime_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ def test_datetime_parsing(value, result):
timedelta(days=-4, minutes=15, seconds=30), # negative durations
timedelta(minutes=15, seconds=30), # minute & seconds
timedelta(seconds=30), # seconds
timedelta(microseconds=99), # microseconds
],
)
def test_parse_python_format(delta):
Expand Down Expand Up @@ -210,6 +211,15 @@ def test_parse_python_format(delta):
('PT5S', timedelta(seconds=5)),
('PT0.000005S', timedelta(microseconds=5)),
(b'PT0.000005S', timedelta(microseconds=5)),
# Scientific Notation
('+0003', errors.DurationError),
(9.9e-05, timedelta(microseconds=99)),
('9.9e-05', timedelta(microseconds=99)),
(9.9e05, timedelta(days=11, seconds=39600)),
(+3, timedelta(seconds=3)),
(-4.70e9, timedelta(days=-54399, seconds=73600)),
(-0.2e-4, timedelta(days=-1, seconds=86399, microseconds=999980)),
(-7.6603, timedelta(days=-1, seconds=86392, microseconds=339700)),
],
)
def test_parse_durations(value, result):
Expand Down