Skip to content

Preventing the Python 3.12's "Deprecated since version 3.12: Use datetime.now() with UTC instead." #914

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pywb/recorder/multifilewarcwriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions pywb/rewrite/header_rewriter.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -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)))
Expand Down
4 changes: 2 additions & 2 deletions pywb/rewrite/test/test_header_rewriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions pywb/warcserver/access_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
actual = datetime.now(timezone.utc) - newer
actual = datetime.now(timezone.utc).replace(tzinfo=None) - newer

I think this is one of the few places where we do need to explicitly create a naive datetime or else an exception will be thrown due to comparison of naive and aware datetimes.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

newer is a relativedelta, not a datetime.

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
actual = datetime.now(timezone.utc) - older
actual = datetime.now(timezone.utc).replace(tzinfo=None) - newer

Same as above.

return access if actual > dt else None

def create_access_aggregator(self, source_files):
Expand Down
2 changes: 1 addition & 1 deletion pywb/warcserver/index/zipnum.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pywb/warcserver/resource/responseloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down