Skip to content

Commit 293f4ed

Browse files
committed
Fixes now() not behaving properly when given a timezone.
1 parent 99191dc commit 293f4ed

File tree

2 files changed

+35
-19
lines changed

2 files changed

+35
-19
lines changed

pendulum/pendulum.py

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,9 @@ class Pendulum(datetime.datetime, TranslatableMixin):
9898
@classmethod
9999
def _safe_create_datetime_zone(cls, obj):
100100
"""
101-
Creates a timezone from a string, BaseTzInfo or integer offset.
101+
Creates a timezone from a string, Timezone, TimezoneInfo or integer offset.
102102
103-
:param obj: str or tzinfo or int or None
103+
:param obj: str or Timezone or TimezoneInfo or int or None
104104
105105
:rtype: Timezone
106106
"""
@@ -125,7 +125,7 @@ def _local_timezone(cls):
125125
Returns the local timezone using tzlocal.get_localzone
126126
or the TZ environment variable.
127127
128-
:rtype: tzinfo
128+
:rtype: Timezone
129129
"""
130130
return local_timezone()
131131

@@ -137,7 +137,7 @@ def _timezone(cls, zone):
137137
:param zone: The name of the timezone.
138138
:type zone: str
139139
140-
:rtype: BaseTzInfo
140+
:rtype: Timezone
141141
"""
142142
if isinstance(zone, Timezone):
143143
return zone
@@ -214,7 +214,7 @@ def parse(cls, time=None, tz=UTC):
214214
:type time: str
215215
216216
:param tz: The timezone
217-
:type tz: BaseTzInfo or str or None
217+
:type tz: Timezone or TimezoneInfo or str or None
218218
219219
:rtype: Pendulum
220220
"""
@@ -248,7 +248,7 @@ def now(cls, tz=None):
248248
Get a Pendulum instance for the current date and time.
249249
250250
:param tz: The timezone
251-
:type tz: BaseTzInfo or str or None
251+
:type tz: Timezone or TimezoneInfo or str or None
252252
253253
:rtype: Pendulum
254254
"""
@@ -262,10 +262,14 @@ def now(cls, tz=None):
262262

263263
return test_instance.copy()
264264

265-
if tz is UTC or tz == 'UTC':
266-
dt = datetime.datetime.utcnow()
267-
else:
265+
if tz is None or tz == 'local':
268266
dt = datetime.datetime.now()
267+
elif tz is UTC or tz == 'UTC':
268+
dt = datetime.datetime.utcnow().replace(tzinfo=UTC)
269+
else:
270+
tz = cls._safe_create_datetime_zone(tz)
271+
272+
return cls(*cls._create_datetime(tz))
269273

270274
return cls.instance(dt, tz)
271275

@@ -275,7 +279,7 @@ def utcnow(cls):
275279
Get a Pendulum instance for the current date and time in UTC.
276280
277281
:param tz: The timezone
278-
:type tz: BaseTzInfo or str or None
282+
:type tz: Timezone or TimezoneInfo or str or None
279283
280284
:rtype: Pendulum
281285
"""
@@ -287,7 +291,7 @@ def today(cls, tz=None):
287291
Create a Pendulum instance for today.
288292
289293
:param tz: The timezone
290-
:type tz: BaseTzInfo or str or None
294+
:type tz: Timezone or TimezoneInfo or str or None
291295
292296
:rtype: Pendulum
293297
"""
@@ -299,7 +303,7 @@ def tomorrow(cls, tz=None):
299303
Create a Pendulum instance for tomorrow.
300304
301305
:param tz: The timezone
302-
:type tz: BaseTzInfo or str or None
306+
:type tz: Timezone or TimezoneInfo or str or None
303307
304308
:rtype: Pendulum
305309
"""
@@ -311,7 +315,7 @@ def yesterday(cls, tz=None):
311315
Create a Pendulum instance for yesterday.
312316
313317
:param tz: The timezone
314-
:type tz: BaseTzInfo or str or None
318+
:type tz: Timezone or TimezoneInfo or str or None
315319
316320
:rtype: Pendulum
317321
"""
@@ -320,6 +324,8 @@ def yesterday(cls, tz=None):
320324
@classmethod
321325
def _create_datetime(cls, tz, year=None, month=None, day=None,
322326
hour=None, minute=None, second=None, microsecond=None):
327+
tzinfo = tz
328+
323329
if any([year is None, month is None, day is None,
324330
hour is None, minute is None, second is None, microsecond is None]):
325331
now = datetime.datetime.utcnow().replace(tzinfo=UTC)
@@ -344,8 +350,10 @@ def _create_datetime(cls, tz, year=None, month=None, day=None,
344350
second = 0 if second is None else second
345351
microsecond = 0 if microsecond is None else microsecond
346352

353+
tzinfo = now.tzinfo
354+
347355
return (year, month, day,
348-
hour, minute, second, microsecond)
356+
hour, minute, second, microsecond, tzinfo)
349357

350358
@classmethod
351359
def create(cls, year=None, month=None, day=None,
@@ -372,7 +380,7 @@ def create(cls, year=None, month=None, day=None,
372380

373381
dt = datetime.datetime(*cls._create_datetime(
374382
tz, year, month, day, hour, minute, second, microsecond
375-
))
383+
)[:-1])
376384
dt = tz.convert(dt)
377385

378386
return cls.instance(dt)
@@ -712,7 +720,7 @@ def in_timezone(self, tz):
712720
Set the instance's timezone from a string or object.
713721
714722
:param value: The timezone
715-
:type value: BaseTzInfo or str or None
723+
:type value: Timezone or TimezoneInfo or str or None
716724
717725
:rtype: Pendulum
718726
"""
@@ -725,7 +733,7 @@ def in_tz(self, tz):
725733
Set the instance's timezone from a string or object.
726734
727735
:param value: The timezone
728-
:type value: BaseTzInfo or str or None
736+
:type value: Timezone or TimezoneInfo or str or None
729737
730738
:rtype: Pendulum
731739
"""
@@ -2281,10 +2289,11 @@ def _getstate(self):
22812289
)
22822290

22832291
def __setstate__(self, year, month, day, hour, minute, second, microsecond, tz):
2284-
self._datetime = self._create_datetime(
2292+
self._datetime = datetime.datetime(self._create_datetime(
22852293
tz, year, month, day,
22862294
hour, minute, second, microsecond
2287-
)
2295+
)[:-1])
2296+
self._tz = tz
22882297

22892298
def __reduce__(self):
22902299
return self.__class__, self._getstate()

tests/pendulum_tests/test_construct.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,10 @@ def test_instance_naive_datetime_defaults_to_utc(self):
114114
def test_instance_timezone_aware_datetime(self):
115115
now = Pendulum.instance(datetime.now(TimezoneInfo.create(timezone('Europe/Paris'), 7200, True, 'EST')))
116116
self.assertEqual('Europe/Paris', now.timezone_name)
117+
118+
def test_now(self):
119+
now = Pendulum.now()
120+
in_paris = Pendulum.now('Europe/Paris')
121+
print(now.tz)
122+
123+
self.assertNotEqual(now.hour, in_paris.hour)

0 commit comments

Comments
 (0)