Skip to content

Commit

Permalink
Replace deprecated datetime.utcnow() with datetime.now(datetime.UTC)
Browse files Browse the repository at this point in the history
datetime.utcnow is deprecated since Python 3.12
  • Loading branch information
phw committed Apr 30, 2024
1 parent 52656c7 commit 8891f9c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 20 deletions.
4 changes: 2 additions & 2 deletions picard/script/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Picard, the next-generation MusicBrainz tagger
#
# Copyright (C) 2021, 2023 Bob Swift
# Copyright (C) 2021-2023 Philipp Wolfer
# Copyright (C) 2021-2024 Philipp Wolfer
# Copyright (C) 2021-2024 Laurent Monin
#
# This program is free software; you can redistribute it and/or
Expand Down Expand Up @@ -152,7 +152,7 @@ def make_last_updated():
Returns:
str: Last updated string from current date and time
"""
return datetime.datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S UTC')
return datetime.datetime.now(datetime.timezone.utc).strftime('%Y-%m-%d %H:%M:%S UTC')

def update_last_updated(self):
"""Update the last updated attribute to the current UTC date and time.
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# Copyright (C) 2006-2008, 2011-2014, 2017 Lukáš Lalinský
# Copyright (C) 2007 Santiago M. Mola
# Copyright (C) 2008 Robert Kaye
# Copyright (C) 2008-2009, 2018-2022 Philipp Wolfer
# Copyright (C) 2008-2009, 2018-2024 Philipp Wolfer
# Copyright (C) 2009 Carlin Mangar
# Copyright (C) 2011-2012, 2014, 2016-2018 Wieland Hoffmann
# Copyright (C) 2011-2014 Michael Wiencek
Expand Down Expand Up @@ -716,7 +716,7 @@ def patch_version(self, filename):
regex = re.compile(r'^PICARD_BUILD_VERSION_STR\s*=.*$', re.MULTILINE)
with open(filename, 'r+b') as f:
source = (f.read()).decode()
build = self.platform + '.' + datetime.datetime.utcnow().strftime('%Y%m%d%H%M%S')
build = self.platform + '.' + datetime.datetime.now(datetime.timezone.utc).strftime('%Y%m%d%H%M%S')
patched_source = regex.sub('PICARD_BUILD_VERSION_STR = "%s"' % build, source).encode()
f.seek(0)
f.write(patched_source)
Expand Down
26 changes: 10 additions & 16 deletions test/test_script_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Picard, the next-generation MusicBrainz tagger
#
# Copyright (C) 2021 Bob Swift
# Copyright (C) 2021 Philipp Wolfer
# Copyright (C) 2021, 2024 Philipp Wolfer
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
Expand All @@ -21,6 +21,7 @@


import datetime
from unittest.mock import patch

import yaml

Expand All @@ -33,26 +34,17 @@
)


class _DateTime(datetime.datetime):
class MockDateTime(datetime.datetime):
@classmethod
def utcnow(cls):
return cls(year=2020, month=1, day=2, hour=12, minute=34, second=56, microsecond=789, tzinfo=None)
def now(cls, tz=None):
if tz == datetime.timezone.utc:
return cls(year=2020, month=1, day=2, hour=12, minute=34, second=56, microsecond=789, tzinfo=None)
else:
raise Exception("Unexpected parameter tz=%r" % tz)


class PicardScriptTest(PicardTestCase):

original_datetime = datetime.datetime

def setUp(self):
super().setUp()
# Save original datetime object and substitute one returning
# a fixed utcnow() value for testing.
datetime.datetime = _DateTime

def tearDown(self):
# Restore original datetime object
datetime.datetime = self.original_datetime

def assertYamlEquals(self, yaml_str, obj, msg=None):
self.assertEqual(obj, yaml.safe_load(yaml_str), msg)

Expand Down Expand Up @@ -88,6 +80,7 @@ def test_script_object_3(self):
self.assertEqual(test_script.last_updated, '2021-04-26')
self.assertEqual(test_script['last_updated'], '2021-04-26')

@patch('datetime.datetime', MockDateTime)
def test_script_object_4(self):
# Check updating values that modify `last_updated`.
test_script = PicardScript(title='Script 1', script='Script text', id='12345', last_updated='2021-04-26')
Expand All @@ -97,6 +90,7 @@ def test_script_object_4(self):
self.assertEqual(test_script.last_updated, '2020-01-02 12:34:56 UTC')
self.assertEqual(test_script['last_updated'], '2020-01-02 12:34:56 UTC')

@patch('datetime.datetime', MockDateTime)
def test_script_object_5(self):
# Check updating values from dict that modify `last_updated`.
test_script = PicardScript(title='Script 1', script='Script text', id='12345', last_updated='2021-04-26')
Expand Down

0 comments on commit 8891f9c

Please sign in to comment.