Skip to content

Commit

Permalink
Replace datetime.utcnow (#1179)
Browse files Browse the repository at this point in the history
  • Loading branch information
cdce8p authored Aug 21, 2023
1 parent 0056c88 commit 725ccc0
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 26 deletions.
4 changes: 2 additions & 2 deletions dateparser/freshness_date_parser.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime, time
from datetime import datetime, time, timezone

import regex as re
from dateutil.relativedelta import relativedelta
Expand Down Expand Up @@ -86,7 +86,7 @@ def apply_time(dateobj, timeobj):

else:
if "local" not in _settings_tz:
utc_dt = datetime.utcnow()
utc_dt = datetime.now(tz=timezone.utc)
now = apply_timezone(utc_dt, settings.TIMEZONE)
else:
now = datetime.now(self.get_local_tz())
Expand Down
4 changes: 2 additions & 2 deletions dateparser/parser.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import calendar
from collections import OrderedDict
from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone
from io import StringIO

import pytz
Expand Down Expand Up @@ -437,7 +437,7 @@ def _get_correct_leap_year(self, prefer_dates_from, current_year):
def _set_relative_base(self):
self.now = self.settings.RELATIVE_BASE
if not self.now:
self.now = datetime.utcnow()
self.now = datetime.now(tz=timezone.utc).replace(tzinfo=None)

def _get_datetime_obj_params(self):
if not self.now:
Expand Down
4 changes: 2 additions & 2 deletions dateparser/timezone_parser.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime, timedelta, tzinfo
from datetime import datetime, timedelta, timezone, tzinfo

import regex as re

Expand Down Expand Up @@ -80,7 +80,7 @@ def get_offset(tz_obj, regex, repl="", replw=""):


def get_local_tz_offset():
offset = datetime.now() - datetime.utcnow()
offset = datetime.now() - datetime.now(tz=timezone.utc).replace(tzinfo=None)
offset = timedelta(days=offset.days, seconds=round(offset.seconds, -1))
return offset

Expand Down
7 changes: 2 additions & 5 deletions tests/test_freshness_date_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,9 @@ def setUp(self):

settings.TIMEZONE = "utc"

def now_with_timezone(self, tzinfo):
def now_with_timezone(self, tz):
now = self.now
return datetime(
now.year, now.month, now.day, now.hour, now.minute, tzinfo=tzinfo
)
return datetime(now.year, now.month, now.day, now.hour, now.minute, tzinfo=tz)

@parameterized.expand(
[
Expand Down Expand Up @@ -2621,7 +2619,6 @@ def wrapped(*args, **kwargs):
self.freshness_parser = Mock(wraps=freshness_date_parser)

dt_mock = Mock(wraps=dateparser.freshness_date_parser.datetime)
dt_mock.utcnow = Mock(return_value=self.now)
dt_mock.now = Mock(side_effect=self.now_with_timezone)
self.add_patch(patch("dateparser.freshness_date_parser.datetime", new=dt_mock))
self.add_patch(
Expand Down
20 changes: 16 additions & 4 deletions tests/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,27 +597,39 @@ def test_search_and_parse(self, shortname, string, expected, settings=None):
(
"May 2020",
datetime.datetime(
2020, 5, datetime.datetime.utcnow().day, 0, 0
2020,
5,
datetime.datetime.now(tz=datetime.timezone.utc).day,
0,
0,
),
),
(
"July 2020",
datetime.datetime(
2020, 7, datetime.datetime.utcnow().day, 0, 0
2020,
7,
datetime.datetime.now(tz=datetime.timezone.utc).day,
0,
0,
),
),
(
"2023",
datetime.datetime(
2023, 7, datetime.datetime.utcnow().day, 0, 0
2023,
7,
datetime.datetime.now(tz=datetime.timezone.utc).day,
0,
0,
),
),
(
"January UTC",
datetime.datetime(
2023,
1,
datetime.datetime.utcnow().day,
datetime.datetime.now(tz=datetime.timezone.utc).day,
0,
0,
tzinfo=pytz.utc,
Expand Down
25 changes: 14 additions & 11 deletions tests/test_timezone_parser.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import datetime as dt
from datetime import datetime, timedelta
from unittest.mock import Mock, patch

Expand Down Expand Up @@ -133,17 +134,10 @@ def setUp(self):
]
)
def test_timezone_offset_calculation(self, utc, local, offset):
self.given_utc_time(utc)
self.given_local_time(local)
self.given_time(utc, local)
self.when_offset_popped_from_string()
self.then_offset_is(offset)

def given_utc_time(self, datetime_string):
self._given_time(datetime_string, "utcnow")

def given_local_time(self, datetime_string):
self._given_time(datetime_string, "now")

def when_offset_popped_from_string(self):
self.timezone_offset = get_local_tz_offset()

Expand All @@ -155,12 +149,21 @@ def then_offset_is(self, expected_offset):
)
self.assertEqual(delta, self.timezone_offset)

def _given_time(self, datetime_string, getter_name):
def given_time(self, utc_dt_string, local_dt_string):
datetime_cls = dateparser.timezone_parser.datetime
if not isinstance(datetime_cls, Mock):
datetime_cls = Mock(wraps=datetime)
datetime_obj = datetime.strptime(datetime_string, "%Y-%m-%d %H:%M")
setattr(datetime_cls, getter_name, Mock(return_value=datetime_obj))
utc_dt_obj = datetime.strptime(utc_dt_string, "%Y-%m-%d %H:%M").astimezone(
dt.timezone.utc
)
local_dt_obj = datetime.strptime(local_dt_string, "%Y-%m-%d %H:%M")

def _dt_now(tz=None):
if tz == dt.timezone.utc:
return utc_dt_obj
return local_dt_obj

datetime_cls.now = Mock(side_effect=_dt_now)
self.add_patch(patch("dateparser.timezone_parser.datetime", new=datetime_cls))


Expand Down

0 comments on commit 725ccc0

Please sign in to comment.