Skip to content

Commit 6aa68cf

Browse files
authored
Merge pull request #2442 from phw/datetime-utcnow-deprecation
Replace deprecated datetime.utcnow() with datetime.now(datetime.UTC)
2 parents 5f76d92 + 8891f9c commit 6aa68cf

File tree

3 files changed

+14
-20
lines changed

3 files changed

+14
-20
lines changed

picard/script/serializer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Picard, the next-generation MusicBrainz tagger
44
#
55
# Copyright (C) 2021, 2023 Bob Swift
6-
# Copyright (C) 2021-2023 Philipp Wolfer
6+
# Copyright (C) 2021-2024 Philipp Wolfer
77
# Copyright (C) 2021-2024 Laurent Monin
88
#
99
# This program is free software; you can redistribute it and/or
@@ -152,7 +152,7 @@ def make_last_updated():
152152
Returns:
153153
str: Last updated string from current date and time
154154
"""
155-
return datetime.datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S UTC')
155+
return datetime.datetime.now(datetime.timezone.utc).strftime('%Y-%m-%d %H:%M:%S UTC')
156156

157157
def update_last_updated(self):
158158
"""Update the last updated attribute to the current UTC date and time.

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# Copyright (C) 2006-2008, 2011-2014, 2017 Lukáš Lalinský
77
# Copyright (C) 2007 Santiago M. Mola
88
# Copyright (C) 2008 Robert Kaye
9-
# Copyright (C) 2008-2009, 2018-2022 Philipp Wolfer
9+
# Copyright (C) 2008-2009, 2018-2024 Philipp Wolfer
1010
# Copyright (C) 2009 Carlin Mangar
1111
# Copyright (C) 2011-2012, 2014, 2016-2018 Wieland Hoffmann
1212
# Copyright (C) 2011-2014 Michael Wiencek
@@ -716,7 +716,7 @@ def patch_version(self, filename):
716716
regex = re.compile(r'^PICARD_BUILD_VERSION_STR\s*=.*$', re.MULTILINE)
717717
with open(filename, 'r+b') as f:
718718
source = (f.read()).decode()
719-
build = self.platform + '.' + datetime.datetime.utcnow().strftime('%Y%m%d%H%M%S')
719+
build = self.platform + '.' + datetime.datetime.now(datetime.timezone.utc).strftime('%Y%m%d%H%M%S')
720720
patched_source = regex.sub('PICARD_BUILD_VERSION_STR = "%s"' % build, source).encode()
721721
f.seek(0)
722722
f.write(patched_source)

test/test_script_serializer.py

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Picard, the next-generation MusicBrainz tagger
44
#
55
# Copyright (C) 2021 Bob Swift
6-
# Copyright (C) 2021 Philipp Wolfer
6+
# Copyright (C) 2021, 2024 Philipp Wolfer
77
#
88
# This program is free software; you can redistribute it and/or
99
# modify it under the terms of the GNU General Public License
@@ -21,6 +21,7 @@
2121

2222

2323
import datetime
24+
from unittest.mock import patch
2425

2526
import yaml
2627

@@ -33,26 +34,17 @@
3334
)
3435

3536

36-
class _DateTime(datetime.datetime):
37+
class MockDateTime(datetime.datetime):
3738
@classmethod
38-
def utcnow(cls):
39-
return cls(year=2020, month=1, day=2, hour=12, minute=34, second=56, microsecond=789, tzinfo=None)
39+
def now(cls, tz=None):
40+
if tz == datetime.timezone.utc:
41+
return cls(year=2020, month=1, day=2, hour=12, minute=34, second=56, microsecond=789, tzinfo=None)
42+
else:
43+
raise Exception("Unexpected parameter tz=%r" % tz)
4044

4145

4246
class PicardScriptTest(PicardTestCase):
4347

44-
original_datetime = datetime.datetime
45-
46-
def setUp(self):
47-
super().setUp()
48-
# Save original datetime object and substitute one returning
49-
# a fixed utcnow() value for testing.
50-
datetime.datetime = _DateTime
51-
52-
def tearDown(self):
53-
# Restore original datetime object
54-
datetime.datetime = self.original_datetime
55-
5648
def assertYamlEquals(self, yaml_str, obj, msg=None):
5749
self.assertEqual(obj, yaml.safe_load(yaml_str), msg)
5850

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

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

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

0 commit comments

Comments
 (0)