Skip to content

Commit

Permalink
Ensure datetime backcompat
Browse files Browse the repository at this point in the history
  • Loading branch information
ghiggi committed Jul 2, 2024
1 parent a2b0570 commit d7dc174
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 11 deletions.
5 changes: 3 additions & 2 deletions gpm/dataset/attrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@
# -----------------------------------------------------------------------------.
"""This module contains functions to parse GPM granule attributes."""
import ast
import datetime

import numpy as np

from gpm.io.checks import get_current_utc_time

STATIC_GLOBAL_ATTRS = (
## FileHeader
"DOI",
Expand Down Expand Up @@ -195,7 +196,7 @@ def get_granule_attrs(dt):

def add_history(ds):
"""Add the history attribute to the xarray.Dataset."""
current_time = datetime.datetime.now(datetime.UTC).strftime("%Y-%m-%d %H:%M:%S")
current_time = get_current_utc_time().strftime("%Y-%m-%d %H:%M:%S")
history = f"Created by ghiggi/gpm_api software on {current_time}"
ds.attrs["history"] = history
return ds
11 changes: 9 additions & 2 deletions gpm/io/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,17 @@
import datetime
import os
import subprocess
import sys

import numpy as np


def get_current_utc_time():
if sys.version_info >= (3, 11):
return datetime.datetime.now(datetime.UTC).replace(tzinfo=None)
return datetime.datetime.utcnow()


def check_base_dir(base_dir):
"""Check base directory path.
Expand Down Expand Up @@ -262,9 +269,9 @@ def check_start_end_time(start_time, end_time):
if start_time > end_time:
raise ValueError("Provide 'start_time' occurring before of 'end_time'.")
# Check start_time and end_time are in the past
if start_time > datetime.datetime.now(datetime.UTC).replace(tzinfo=None):
if start_time > get_current_utc_time():
raise ValueError("Provide a 'start_time' occurring in the past.")
if end_time > datetime.datetime.now(datetime.UTC).replace(tzinfo=None):
if end_time > get_current_utc_time():
raise ValueError("Provide a 'end_time' occurring in the past.")
return (start_time, end_time)

Expand Down
3 changes: 2 additions & 1 deletion gpm/io/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
check_product,
check_start_end_time,
check_version,
get_current_utc_time,
)
from gpm.io.info import (
get_info_from_filepath,
Expand Down Expand Up @@ -275,7 +276,7 @@ def filter_by_time(filepaths, start_time=None, end_time=None):
if start_time is None:
start_time = datetime.datetime(1998, 1, 1, 0, 0, 0) # GPM start mission
if end_time is None:
end_time = datetime.datetime.now(datetime.UTC).replace(tzinfo=None) # Current time
end_time = get_current_utc_time.replace(tzinfo=None) # Current time
start_time, end_time = check_start_end_time(start_time, end_time)

# -------------------------------------------------------------------------.
Expand Down
7 changes: 4 additions & 3 deletions gpm/io/products.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
check_sensors,
check_time,
check_versions,
get_current_utc_time,
)
from gpm.utils.list import flatten_list
from gpm.utils.yaml import read_yaml
Expand Down Expand Up @@ -175,7 +176,7 @@ def filter_info_dict_by_time(info_dict, start_time, end_time):
if start_time is None:
start_time = datetime.datetime(1987, 7, 9, 0, 0, 0)
if end_time is None:
end_time = datetime.datetime.now(datetime.UTC).replace(tzinfo=None)
end_time = get_current_utc_time()

start_time = check_time(start_time)
end_time = check_time(end_time)
Expand All @@ -185,7 +186,7 @@ def filter_info_dict_by_time(info_dict, start_time, end_time):
sensor_start_time = product_info["start_time"]
sensor_end_time = product_info["end_time"]
if sensor_end_time is None:
sensor_end_time = datetime.datetime.now(datetime.UTC).replace(tzinfo=None)
sensor_end_time = get_current_utc_time()
if is_granule_within_time(
start_time=start_time,
end_time=end_time,
Expand Down Expand Up @@ -229,7 +230,7 @@ def get_product_end_time(product):
"""Provide the product ``end_time``."""
end_time = get_info_dict()[product]["end_time"]
if end_time is None:
end_time = datetime.datetime.now(datetime.UTC).replace(tzinfo=None)
end_time = get_current_utc_time()
return end_time


Expand Down
5 changes: 3 additions & 2 deletions gpm/tests/test_io/test_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import gpm.configs
from gpm.io import download as dl
from gpm.io import find
from gpm.io.checks import get_current_utc_time
from gpm.io.products import available_products, get_product_start_time
from gpm.utils.warnings import GPMDownloadWarning

Expand All @@ -51,7 +52,7 @@ def test_construct_curl_pps_cmd(
`local_filepath` relates to a file on the disk
"""
# Use datetime as path as to be unique to every test
current_time = datetime.datetime.now(datetime.UTC).replace(tzinfo=None)
current_time = get_current_utc_time()
local_filepath = os.path.join(
tmpdir,
current_time.isoformat().replace(":", "-"),
Expand Down Expand Up @@ -108,7 +109,7 @@ def test_construct_wget_pps_cmd(
`local_filepath` relates to a file on the disk
"""
# Use datetime as path as to be unique to every test
current_time = datetime.datetime.now(datetime.UTC).replace(tzinfo=None)
current_time = get_current_utc_time()
local_filepath = os.path.join(
tmpdir,
current_time.isoformat().replace(":", "-"),
Expand Down
2 changes: 1 addition & 1 deletion gpm/tests/test_io/test_io_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@ def test_check_start_end_time() -> None:
# behind the current time tested in the function
checks.check_start_end_time(
datetime.datetime(2014, 12, 31, 12, 30, 30, 300),
datetime.datetime.now(datetime.UTC),
checks.get_current_utc_time(),
)


Expand Down

0 comments on commit d7dc174

Please sign in to comment.