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

Fix datetime deprecation in Python >=3.11 #328

Merged
merged 4 commits into from
Feb 27, 2024
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
21 changes: 15 additions & 6 deletions tests/test_erddapy.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
"""Test ERDDAP functionality."""

from datetime import datetime
import datetime
import sys

import httpx
import packaging.version
import pytest
import pytz

Expand All @@ -17,22 +19,29 @@
)
from erddapy.erddapy import ERDDAP

if packaging.version.parse(
f"{sys.version_info.major}.{sys.version_info.minor}",
) < packaging.version.parse(
"3.11",
):
datetime.UTC = datetime.timezone.utc


def test_parse_dates_naive_datetime():
"""Naive timestamp at 1970-1-1 must be 0."""
d = datetime(1970, 1, 1, 0, 0)
d = datetime.datetime(1970, 1, 1, 0, 0)
assert parse_dates(d) == 0


def test_parse_dates_utc_datetime():
"""UTC timestamp at 1970-1-1 must be 0."""
d = datetime(1970, 1, 1, 0, 0, tzinfo=pytz.utc)
d = datetime.datetime(1970, 1, 1, 0, 0, tzinfo=pytz.utc)
assert parse_dates(d) == 0


def test_parse_dates_nonutc_datetime():
"""Non-UTC timestamp at 1970-1-1 must have the zone offset."""
d = datetime(1970, 1, 1, tzinfo=pytz.timezone("US/Eastern"))
d = datetime.datetime(1970, 1, 1, tzinfo=pytz.timezone("US/Eastern"))
assert parse_dates(d) == abs(d.utcoffset().total_seconds())


Expand All @@ -52,15 +61,15 @@ def test__quote_string_constraints():
{
"latitude": 42,
"longitude": 42.0,
"max_time": datetime.utcnow(),
"max_time": datetime.datetime.now(datetime.UTC),
ocefpaf marked this conversation as resolved.
Show resolved Hide resolved
"min_time": "1970-01-01T00:00:00Z",
"cdm_data_type": "trajectoryprofile",
},
)

assert isinstance(kw["latitude"], int)
assert isinstance(kw["longitude"], float)
assert isinstance(kw["max_time"], datetime)
assert isinstance(kw["max_time"], datetime.datetime)
assert isinstance(kw["min_time"], str)
assert isinstance(kw["cdm_data_type"], str)

Expand Down
Loading