diff --git a/bin/pps_runner.py b/bin/pps_runner.py index bf75324..cd60a29 100644 --- a/bin/pps_runner.py +++ b/bin/pps_runner.py @@ -29,7 +29,7 @@ import os import sys import threading -from datetime import datetime +from datetime import datetime, timezone from subprocess import PIPE, Popen from queue import Empty, Queue @@ -97,7 +97,7 @@ def pps_worker(scene, publish_q, input_msg, options): try: LOG.info("Starting pps runner for scene %s", str(scene)) - job_start_time = datetime.utcnow() + job_start_time = datetime.now(tz=timezone.utc) LOG.debug("Level-1c file: %s", scene['file4pps']) LOG.debug("Platform name: %s", scene['platform_name']) @@ -178,7 +178,7 @@ def pps_worker(scene, publish_q, input_msg, options): servername=options['servername'], station=options['station']) - dt_ = datetime.utcnow() - job_start_time + dt_ = datetime.now(tz=timezone.utc) - job_start_time LOG.info("PPS on scene " + str(scene) + " finished. It took: " + str(dt_)) t__.cancel() diff --git a/bin/run_nwp_preparation.py b/bin/run_nwp_preparation.py index 31c32df..52393f4 100644 --- a/bin/run_nwp_preparation.py +++ b/bin/run_nwp_preparation.py @@ -26,7 +26,7 @@ import logging import time -from datetime import datetime, timedelta +from datetime import datetime, timedelta, timezone from nwcsafpps_runner.logger import setup_logging from nwcsafpps_runner.prepare_nwp import update_nwp from nwcsafpps_runner.utils import NwpPrepareError @@ -37,12 +37,12 @@ LOG = logging.getLogger('nwp-preparation') -# datetime.datetime.utcnow => datetime.datetime.now(datetime.UTC) ~python 3.12 +# Eventually timezone.utz => UTC. UTC in python 3.12 not in python 3.9 def prepare_and_publish(pub, options, flens): config_file_name = options.config_file - starttime = datetime.utcnow() - timedelta(days=1) + starttime = datetime.now(tz=timezone.utc) - timedelta(days=1) ok_files, publish_topic = update_nwp(starttime, flens, config_file_name) if publish_topic is not None: for filename in ok_files: @@ -60,7 +60,7 @@ def _run_subscribe_publisher(pub, options, flens): if every_hour_minute > 60: return while True: - minute = datetime.utcnow().minute + minute = datetime.now(tz=timezone.utc).minute if minute < every_hour_minute or minute > every_hour_minute + 7: LOG.info("Not time for nwp preparation for pps yet, waiting 5 minutes") time.sleep(60 * 5) diff --git a/nwcsafpps_runner/prepare_nwp.py b/nwcsafpps_runner/prepare_nwp.py index 4dbca28..2854e54 100644 --- a/nwcsafpps_runner/prepare_nwp.py +++ b/nwcsafpps_runner/prepare_nwp.py @@ -25,7 +25,7 @@ from glob import glob import os -from datetime import datetime +from datetime import datetime, timezone import time import tempfile from trollsift import Parser @@ -71,8 +71,8 @@ def set_time_info(self, filename, cfg): res = parser.parse("{}".format(os.path.basename(self.nhsf_file))) if 'analysis_time' in res: if res['analysis_time'].year == 1900: - res['analysis_time'] = res['analysis_time'].replace(year=datetime.utcnow().year) - self.analysis_time = res['analysis_time'] + res['analysis_time'] = res['analysis_time'].replace(year=datetime.now(timezone.utc).year) + self.analysis_time = res['analysis_time'].replace(tzinfo=timezone.utc) self.timestamp = self.analysis_time.strftime("%Y%m%d%H%M") else: raise NwpPrepareError("Can not parse analysis_time in file name. Check config and filename timestamp") @@ -338,5 +338,5 @@ def check_and_reduce_nwp_content(gribfile, result_file, nwp_req_filename): LOG = logging.getLogger('test_update_nwp') from datetime import timedelta - now = datetime.utcnow() + now = datetime.now(tz=timezone.utc) update_nwp(now - timedelta(days=2), [9]) diff --git a/nwcsafpps_runner/tests/test_nwp_prepare.py b/nwcsafpps_runner/tests/test_nwp_prepare.py index 91d3781..e570c90 100644 --- a/nwcsafpps_runner/tests/test_nwp_prepare.py +++ b/nwcsafpps_runner/tests/test_nwp_prepare.py @@ -24,7 +24,7 @@ """Test the nwp_prepare runner code.""" import pytest import unittest -from datetime import datetime +from datetime import datetime, timezone import os import logging from datetime import timedelta @@ -110,7 +110,7 @@ def test_update_nwp(self, fake_file_dir): my_temp_dir = fake_file_dir outfile = os.path.join(str(my_temp_dir), "PPS_ECMWF_202205100000+009H00M") cfg_file = my_temp_dir + '/pps_config.yaml' - date = datetime(year=2022, month=5, day=10, hour=0) + date = datetime(year=2022, month=5, day=10, hour=0, tzinfo=timezone.utc) nwc_prep.update_nwp(date - timedelta(days=2), [9], cfg_file) # Run again when file is already created nwc_prep.update_nwp(date - timedelta(days=2), [9], cfg_file) @@ -123,7 +123,7 @@ def test_update_nwp_no_requirement_file(self, fake_file_dir): requirement_name = str(my_temp_dir) + '/pps_nwp_req.txt' outfile = os.path.join(str(my_temp_dir), "PPS_ECMWF_202205100000+009H00M") os.remove(requirement_name) - date = datetime(year=2022, month=5, day=10, hour=0) + date = datetime(year=2022, month=5, day=10, hour=0, tzinfo=timezone.utc) nwc_prep.update_nwp(date - timedelta(days=2), [9], cfg_file) assert os.path.exists(outfile) @@ -132,9 +132,7 @@ def test_update_nwp_missing_fields(self, fake_file_dir): my_temp_dir = fake_file_dir outfile = os.path.join(str(my_temp_dir), "PPS_ECMWF_MANDATORY_202205100000+009H00M") cfg_file = my_temp_dir + '/pps_config_missing_fields.yaml' - date = datetime(year=2022, month=5, day=10, hour=0) - nwc_prep.update_nwp(date - timedelta(days=2), [9], cfg_file) - date = datetime(year=2022, month=5, day=10, hour=0) + date = datetime(year=2022, month=5, day=10, hour=0, tzinfo=timezone.utc) nwc_prep.update_nwp(date - timedelta(days=2), [9], cfg_file) assert not (os.path.exists(outfile))