Skip to content

Commit

Permalink
Add typing to time.py (#1339)
Browse files Browse the repository at this point in the history
Signed-off-by: Bala.FA <bala@minio.io>
  • Loading branch information
balamurugana authored Nov 22, 2023
1 parent 5ca6653 commit 3d92349
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions minio/time.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

"""Time formatter for S3 APIs."""

from __future__ import absolute_import
from __future__ import absolute_import, annotations

import time as ctime
from datetime import datetime, timezone
Expand All @@ -32,15 +32,15 @@
"Nov", "Dec"]


def _to_utc(value):
def _to_utc(value: datetime) -> datetime:
"""Convert to UTC time if value is not naive."""
return (
value.astimezone(timezone.utc).replace(tzinfo=None)
if value.tzinfo else value
)


def from_iso8601utc(value):
def from_iso8601utc(value: str | None) -> datetime | None:
"""Parse UTC ISO-8601 formatted string to datetime."""
if value is None:
return None
Expand All @@ -52,7 +52,7 @@ def from_iso8601utc(value):
return time.replace(tzinfo=timezone.utc)


def to_iso8601utc(value):
def to_iso8601utc(value: datetime | None) -> str | None:
"""Format datetime into UTC ISO-8601 formatted string."""
if value is None:
return None
Expand All @@ -63,7 +63,7 @@ def to_iso8601utc(value):
)


def from_http_header(value):
def from_http_header(value: str) -> datetime:
"""Parse HTTP header date formatted string to datetime."""
if len(value) != 29:
raise ValueError(
Expand Down Expand Up @@ -91,7 +91,7 @@ def from_http_header(value):
return time


def to_http_header(value):
def to_http_header(value: datetime) -> str:
"""Format datatime into HTTP header date formatted string."""
value = _to_utc(value)
weekday = _WEEK_DAYS[value.weekday()]
Expand All @@ -101,23 +101,23 @@ def to_http_header(value):
return f"{weekday},{day}{month}{suffix}"


def to_amz_date(value):
def to_amz_date(value: datetime) -> str:
"""Format datetime into AMZ date formatted string."""
return _to_utc(value).strftime("%Y%m%dT%H%M%SZ")


def utcnow():
def utcnow() -> datetime:
"""Timezone-aware wrapper to datetime.utcnow()."""
if _UTC_IMPORTED:
return datetime.now(UTC).replace(tzinfo=timezone.utc)
return datetime.utcnow().replace(tzinfo=timezone.utc)


def to_signer_date(value):
def to_signer_date(value: datetime) -> str:
"""Format datetime into SignatureV4 date formatted string."""
return _to_utc(value).strftime("%Y%m%d")


def to_float(value):
def to_float(value: datetime) -> float:
"""Convert datetime into float value."""
return ctime.mktime(value.timetuple()) + value.microsecond * 1e-6

0 comments on commit 3d92349

Please sign in to comment.