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

Add typing to time.py #1339

Merged
merged 1 commit into from
Nov 22, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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