Skip to content

Commit 93306ef

Browse files
committed
Improves performances
1 parent d0e88b6 commit 93306ef

File tree

2 files changed

+47
-26
lines changed

2 files changed

+47
-26
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,7 @@ nosetests.xml
2121

2222
/test.py
2323
/test_*.py
24+
/benchmark/*
25+
benchmark.py
26+
results.json
27+
profile.html

pendulum/pendulum.py

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,12 @@ def _safe_create_datetime_zone(cls, obj):
109109
110110
:rtype: BaseTzInfo
111111
"""
112-
if obj is None or obj == 'local':
113-
return cls._local_timezone()
114-
115112
if isinstance(obj, tzinfo):
116113
return obj
117114

115+
if obj is None or obj == 'local':
116+
return cls._local_timezone()
117+
118118
if isinstance(obj, (int, float)):
119119
timezone_offset = obj * 60
120120

@@ -188,7 +188,7 @@ def parse(cls, time=None, tz=pytz.UTC):
188188
189189
:param tz: The timezone
190190
:type tz: BaseTzInfo or str or None
191-
q
191+
192192
:rtype: Pendulum
193193
"""
194194
if time is None:
@@ -235,7 +235,15 @@ def now(cls, tz=None):
235235

236236
return test_instance.copy()
237237

238-
return cls.create(tz=tz)
238+
dt = datetime.datetime.utcnow().replace(tzinfo=pytz.UTC)
239+
if not tz:
240+
tz = cls._local_timezone()
241+
else:
242+
tz = cls._safe_create_datetime_zone(tz)
243+
244+
dt = tz.normalize(dt)
245+
246+
return cls.instance(dt)
239247

240248
@classmethod
241249
def utcnow(cls):
@@ -288,33 +296,33 @@ def yesterday(cls, tz=None):
288296
@classmethod
289297
def _create_datetime(cls, tz, year=None, month=None, day=None,
290298
hour=None, minute=None, second=None, microsecond=None):
291-
now = (datetime.datetime.utcnow()
292-
.replace(tzinfo=pytz.UTC)
293-
.astimezone(tz))
299+
if any([year is None, month is None, day is None,
300+
hour is None, minute is None, second is None, microsecond is None]):
301+
now = pytz.UTC.localize(datetime.datetime.utcnow())
302+
now = tz.normalize(now)
294303

295-
if year is None:
296-
year = now.year
304+
if year is None:
305+
year = now.year
297306

298-
if month is None:
299-
month = now.month
307+
if month is None:
308+
month = now.month
300309

301-
if day is None:
302-
day = now.day
310+
if day is None:
311+
day = now.day
303312

304-
if hour is None:
305-
hour = now.hour
306-
minute = now.minute if minute is None else minute
307-
second = now.second if second is None else second
308-
microsecond = now.microsecond if microsecond is None else microsecond
309-
else:
310-
minute = 0 if minute is None else minute
311-
second = 0 if second is None else second
312-
microsecond = 0 if microsecond is None else microsecond
313+
if hour is None:
314+
hour = now.hour
315+
minute = now.minute if minute is None else minute
316+
second = now.second if second is None else second
317+
microsecond = now.microsecond if microsecond is None else microsecond
318+
else:
319+
minute = 0 if minute is None else minute
320+
second = 0 if second is None else second
321+
microsecond = 0 if microsecond is None else microsecond
313322

314323
return tz.localize(datetime.datetime(
315324
year, month, day,
316-
hour, minute, second, microsecond,
317-
tzinfo=None
325+
hour, minute, second, microsecond
318326
))
319327

320328
@classmethod
@@ -414,7 +422,16 @@ def create_from_timestamp(cls, timestamp, tz=pytz.UTC):
414422
415423
:rtype: Pendulum
416424
"""
417-
return cls.now(tz).with_timestamp(timestamp)
425+
dt = datetime.datetime.utcfromtimestamp(timestamp).replace(tzinfo=pytz.UTC)
426+
427+
if not tz:
428+
tz = cls._local_timezone()
429+
else:
430+
tz = cls._safe_create_datetime_zone(tz)
431+
432+
dt = tz.normalize(dt)
433+
434+
return cls.instance(dt)
418435

419436
@classmethod
420437
def strptime(cls, time, fmt):

0 commit comments

Comments
 (0)