From 1b78bb4fb75d814b9f9cc62a1d2a7b1fcb09f898 Mon Sep 17 00:00:00 2001 From: Lisias T Date: Sun, 18 Aug 2024 02:21:53 -0300 Subject: [PATCH] Preventing the Python 3.12's "Deprecated since version 3.12: Use datetime.now() with UTC instead." For https://github.com/webrecorder/pywb/issues/913 --- pywb/recorder/multifilewarcwriter.py | 4 ++-- pywb/rewrite/header_rewriter.py | 4 ++-- pywb/rewrite/test/test_header_rewriter.py | 4 ++-- pywb/warcserver/access_checker.py | 6 +++--- pywb/warcserver/index/zipnum.py | 2 +- pywb/warcserver/resource/responseloader.py | 2 +- 6 files changed, 11 insertions(+), 11 deletions(-) diff --git a/pywb/recorder/multifilewarcwriter.py b/pywb/recorder/multifilewarcwriter.py index 5d0ec1eb3..8ce8ef758 100644 --- a/pywb/recorder/multifilewarcwriter.py +++ b/pywb/recorder/multifilewarcwriter.py @@ -245,7 +245,7 @@ def close_idle_files(self): if not self.max_idle_time: return - now = datetime.datetime.now() + now = datetime.datetime.now(datetime.timezone.utc) for dir_key, out, filename in self.iter_open_files(): try: @@ -254,7 +254,7 @@ def close_idle_files(self): self.close_key(dir_key) return - mtime = datetime.datetime.fromtimestamp(mtime) + mtime = datetime.datetime.fromtimestamp(mtime,tz=datetime.timezone.utc) if (now - mtime) > self.max_idle_time: print('Closing idle ' + filename) diff --git a/pywb/rewrite/header_rewriter.py b/pywb/rewrite/header_rewriter.py index bc6ac85e5..3c37ccc4d 100644 --- a/pywb/rewrite/header_rewriter.py +++ b/pywb/rewrite/header_rewriter.py @@ -1,6 +1,6 @@ from warcio.statusandheaders import StatusAndHeaders from warcio.timeutils import datetime_to_http_date -from datetime import datetime, timedelta +from datetime import datetime, timedelta, timezone from six.moves.urllib.parse import urlsplit @@ -166,7 +166,7 @@ def _add_cache_headers(self, new_headers, http_cache): if age <= 0: new_headers.append(('Cache-Control', 'no-cache; no-store')) else: - dt = datetime.utcnow() + dt = datetime.now(timezone.utc) dt = dt + timedelta(seconds=age) new_headers.append(('Cache-Control', 'max-age=' + str(age))) new_headers.append(('Expires', datetime_to_http_date(dt))) diff --git a/pywb/rewrite/test/test_header_rewriter.py b/pywb/rewrite/test/test_header_rewriter.py index 63975b361..3d257a020 100644 --- a/pywb/rewrite/test/test_header_rewriter.py +++ b/pywb/rewrite/test/test_header_rewriter.py @@ -7,7 +7,7 @@ from pywb.rewrite.header_rewriter import DefaultHeaderRewriter from pywb.rewrite.url_rewriter import UrlRewriter -from datetime import datetime +from datetime import datetime, timezone from io import BytesIO @@ -163,7 +163,7 @@ def _test_cookie_headers(): def _make_cache_headers(): cache_headers = [('Content-Length', '123'), ('Cache-Control', 'max-age=10'), - ('Expires', datetime_to_http_date(datetime.now())), + ('Expires', datetime_to_http_date(datetime.now(timezone.utc))), ('ETag', '123456')] return cache_headers diff --git a/pywb/warcserver/access_checker.py b/pywb/warcserver/access_checker.py index a5fc83356..14febfc47 100644 --- a/pywb/warcserver/access_checker.py +++ b/pywb/warcserver/access_checker.py @@ -7,7 +7,7 @@ from pywb.utils.merge import merge from warcio.timeutils import timestamp_to_datetime -from datetime import datetime, timedelta +from datetime import datetime, timezone from dateutil.relativedelta import relativedelta import os @@ -164,13 +164,13 @@ def check_embargo(self, url, ts): # embargo if newser than newer = self.embargo.get('newer') if newer: - actual = datetime.utcnow() - newer + actual = datetime.now(timezone.utc) - newer return access if actual < dt else None # embargo if older than older = self.embargo.get('older') if older: - actual = datetime.utcnow() - older + actual = datetime.now(timezone.utc) - older return access if actual > dt else None def create_access_aggregator(self, source_files): diff --git a/pywb/warcserver/index/zipnum.py b/pywb/warcserver/index/zipnum.py index 78fd590e3..e738e2d4e 100644 --- a/pywb/warcserver/index/zipnum.py +++ b/pywb/warcserver/index/zipnum.py @@ -136,7 +136,7 @@ def __init__(self, summary, config=None): self.summary = summary # reload interval - self.loc_update_time = datetime.datetime.now() + self.loc_update_time = datetime.datetime.now(datetime.timezone.utc) self.reload_interval = datetime.timedelta(minutes=reload_ival) self.blk_loader = BlockLoader(cookie_maker=cookie_maker) diff --git a/pywb/warcserver/resource/responseloader.py b/pywb/warcserver/resource/responseloader.py index f38dafe37..00d09e720 100644 --- a/pywb/warcserver/resource/responseloader.py +++ b/pywb/warcserver/resource/responseloader.py @@ -430,7 +430,7 @@ def load_resource(self, cdx, params): warc_headers['WARC-Date'] = datetime_to_iso_date(dt) if not cdx.get('is_live'): - now = datetime.datetime.utcnow() + now = datetime.datetime.now(datetime.timezone.utc) warc_headers['WARC-Source-URI'] = cdx.get('load_url') warc_headers['WARC-Creation-Date'] = datetime_to_iso_date(now)