Skip to content

Commit

Permalink
parse_timestamp(): add fast paths for common formats
Browse files Browse the repository at this point in the history
53 ops/s vs 2.8 ops/s
  • Loading branch information
akx committed Feb 7, 2025
1 parent c1c3c38 commit 3166a23
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions botocore/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import warnings
import weakref
from datetime import datetime as _DatetimeClass
from email.utils import parsedate_to_datetime
from ipaddress import ip_address
from pathlib import Path
from urllib.request import getproxies, proxy_bypass
Expand Down Expand Up @@ -658,14 +659,16 @@ def _evaluate_expiration(self, credentials):
if expiration is None:
return
try:
expiration = datetime.datetime.strptime(
expiration = _DatetimeClass.strptime(
expiration, "%Y-%m-%dT%H:%M:%SZ"
)
refresh_interval = self._config.get(
"ec2_credential_refresh_window", 60 * 10
)
jitter = random.randint(120, 600) # Between 2 to 10 minutes
refresh_interval_with_jitter = refresh_interval + jitter
# NB: may not be replaced with `_DatetimeClass.utcnow()` lest tests
# that mock `datetime` in this module fail
current_time = datetime.datetime.utcnow()
refresh_offset = datetime.timedelta(
seconds=refresh_interval_with_jitter
Expand Down Expand Up @@ -924,7 +927,7 @@ def _epoch_seconds_to_datetime(value):
:param value: The Unix timestamps as number.
"""
try:
return datetime.datetime.fromtimestamp(value, tz=tzutc())
return _DatetimeClass.fromtimestamp(value, tz=tzutc())
except (OverflowError, OSError):
# For numeric values attempt fallback to using fromtimestamp-free method.
# From Python's ``datetime.datetime.fromtimestamp`` documentation: "This
Expand All @@ -945,8 +948,22 @@ def parse_timestamp(value):
* epoch (value is an integer)
This will return a ``datetime.datetime`` object.
"""
if isinstance(value, str):
if value.endswith("GMT"):
# Fast path: assume RFC822-with-GMT
try:
return parsedate_to_datetime(value).replace(tzinfo=tzutc())
except Exception:
pass
if value.endswith(("Z", "+00:00", "+0000")):
# Fast path: looks like ISO8601 UTC
try:
# New in version 3.7
return _DatetimeClass.fromisoformat(value)
except Exception:
pass

if isinstance(value, (int, float)):
try:
# Possibly an epoch time.
Expand Down

0 comments on commit 3166a23

Please sign in to comment.