From d7dc174c1399038202090500003ed44aa9eb5b9c Mon Sep 17 00:00:00 2001 From: ghiggi Date: Tue, 2 Jul 2024 09:52:44 +0200 Subject: [PATCH] Ensure datetime backcompat --- gpm/dataset/attrs.py | 5 +++-- gpm/io/checks.py | 11 +++++++++-- gpm/io/filter.py | 3 ++- gpm/io/products.py | 7 ++++--- gpm/tests/test_io/test_download.py | 5 +++-- gpm/tests/test_io/test_io_checks.py | 2 +- 6 files changed, 22 insertions(+), 11 deletions(-) diff --git a/gpm/dataset/attrs.py b/gpm/dataset/attrs.py index 1aa753c..7efca44 100644 --- a/gpm/dataset/attrs.py +++ b/gpm/dataset/attrs.py @@ -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", @@ -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 diff --git a/gpm/io/checks.py b/gpm/io/checks.py index 76801aa..72f94ff 100644 --- a/gpm/io/checks.py +++ b/gpm/io/checks.py @@ -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. @@ -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) diff --git a/gpm/io/filter.py b/gpm/io/filter.py index aaa19c1..b260c2e 100644 --- a/gpm/io/filter.py +++ b/gpm/io/filter.py @@ -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, @@ -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) # -------------------------------------------------------------------------. diff --git a/gpm/io/products.py b/gpm/io/products.py index fc3fa13..2a45903 100644 --- a/gpm/io/products.py +++ b/gpm/io/products.py @@ -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 @@ -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) @@ -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, @@ -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 diff --git a/gpm/tests/test_io/test_download.py b/gpm/tests/test_io/test_download.py index a54fc68..9a83bf3 100644 --- a/gpm/tests/test_io/test_download.py +++ b/gpm/tests/test_io/test_download.py @@ -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 @@ -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(":", "-"), @@ -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(":", "-"), diff --git a/gpm/tests/test_io/test_io_checks.py b/gpm/tests/test_io/test_io_checks.py index 8c0302a..68c4ca0 100644 --- a/gpm/tests/test_io/test_io_checks.py +++ b/gpm/tests/test_io/test_io_checks.py @@ -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(), )