Skip to content

Commit

Permalink
Merge pull request #328 from ocefpaf/fix_datetime_deprecation
Browse files Browse the repository at this point in the history
Fix datetime deprecation in Python >=3.11
  • Loading branch information
ocefpaf authored Feb 27, 2024
2 parents 219d8fb + b225cb9 commit 9bbb705
Showing 1 changed file with 15 additions and 6 deletions.
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),
"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

0 comments on commit 9bbb705

Please sign in to comment.