Skip to content

Commit 8af1c06

Browse files
committed
Changes behavior of create*() methods (time now defaults to 00:00:00)
1 parent 0b1017f commit 8af1c06

File tree

5 files changed

+64
-61
lines changed

5 files changed

+64
-61
lines changed

docs/index.rst

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,19 +142,18 @@ besides behaving as expected, all accept a timezone parameter and each has their
142142
The next group of static helpers are the ``from_xxx()`` and ``create()`` helpers.
143143
Most of the static ``create`` functions allow you to provide
144144
as many or as few arguments as you want and will provide default values for all others.
145-
Generally default values are the current date, time or timezone.
145+
Generally default values are the current date, time set to ``00:00:00`` and ``UTC`` timezone.
146146

147147
.. code-block:: python
148148
149149
pendulum.from_date(year, month, day, tz)
150150
pendulum.from_time(hour, minute, second, microsecond, tz)
151151
pendulum.create(year, month, day, hour, minute, second, microsecond, tz)
152152
153-
``from_date()`` will default the time to now. ``from_time()`` will default the date to today.
154-
``create()`` will default any null parameter to the current respective value.
155-
As before, the ``tz`` defaults to the ``UTC`` timezone and otherwise can be a ``tzinfo`` instance
156-
or simply a string timezone value. The only special case for default values occurs when an hour value
157-
is specified but no minutes or seconds, they will get defaulted to ``0``.
153+
``from_date()`` will default the time to ``00:00:00``. ``from_time()`` will default the date to today.
154+
``create()`` will default any null parameter to the current date for the date part and to ``00:00:00`` for time.
155+
As before, the ``tz`` defaults to the ``UTC`` timezone and otherwise can be a ``TimezoneInfo`` instance
156+
or simply a string timezone value.
158157

159158
.. code-block:: python
160159

pendulum/pendulum.py

Lines changed: 26 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -268,9 +268,9 @@ def now(cls, tz=None):
268268
elif tz is UTC or tz == 'UTC':
269269
dt = datetime.datetime.utcnow().replace(tzinfo=UTC)
270270
else:
271+
dt = datetime.datetime.utcnow().replace(tzinfo=UTC)
271272
tz = cls._safe_create_datetime_zone(tz)
272-
273-
return cls(*cls._create_datetime(tz))
273+
dt = tz.convert(dt)
274274

275275
return cls.instance(dt, tz)
276276

@@ -322,43 +322,9 @@ def yesterday(cls, tz=None):
322322
"""
323323
return cls.today(tz).subtract(days=1)
324324

325-
@classmethod
326-
def _create_datetime(cls, tz, year=None, month=None, day=None,
327-
hour=None, minute=None, second=None, microsecond=None):
328-
tzinfo = tz
329-
330-
if any([year is None, month is None, day is None,
331-
hour is None, minute is None, second is None, microsecond is None]):
332-
now = datetime.datetime.utcnow().replace(tzinfo=UTC)
333-
now = tz.convert(now)
334-
335-
if year is None:
336-
year = now.year
337-
338-
if month is None:
339-
month = now.month
340-
341-
if day is None:
342-
day = now.day
343-
344-
if hour is None:
345-
hour = now.hour
346-
minute = now.minute if minute is None else minute
347-
second = now.second if second is None else second
348-
microsecond = now.microsecond if microsecond is None else microsecond
349-
else:
350-
minute = 0 if minute is None else minute
351-
second = 0 if second is None else second
352-
microsecond = 0 if microsecond is None else microsecond
353-
354-
tzinfo = now.tzinfo
355-
356-
return (year, month, day,
357-
hour, minute, second, microsecond, tzinfo)
358-
359325
@classmethod
360326
def create(cls, year=None, month=None, day=None,
361-
hour=None, minute=None, second=None, microsecond=None,
327+
hour=0, minute=0, second=0, microsecond=0,
362328
tz=UTC):
363329
"""
364330
Create a new Carbon instance from a specific date and time.
@@ -379,18 +345,30 @@ def create(cls, year=None, month=None, day=None,
379345
"""
380346
tz = cls._safe_create_datetime_zone(tz)
381347

382-
dt = datetime.datetime(*cls._create_datetime(
383-
tz, year, month, day, hour, minute, second, microsecond
384-
)[:-1])
385-
dt = tz.convert(dt, dst_rule=cls._TRANSITION_RULE)
348+
if any([year is None, month is None, day is None]):
349+
now = datetime.datetime.utcnow().replace(tzinfo=UTC)
350+
now = tz.convert(now, dst_rule=cls._TRANSITION_RULE)
351+
352+
if year is None:
353+
year = now.year
354+
355+
if month is None:
356+
month = now.month
357+
358+
if day is None:
359+
day = now.day
360+
361+
dt = datetime.datetime(
362+
year, month, day, hour, minute, second, microsecond
363+
)
386364

387-
return cls.instance(dt)
365+
return cls.instance(dt, tz)
388366

389367
@classmethod
390368
def create_from_date(cls, year=None, month=None, day=None, tz='UTC'):
391369
"""
392370
Create a Pendulum instance from just a date.
393-
The time portion is set to now.
371+
The time portion is set to 00:00:00.
394372
395373
:type year: int
396374
:type month: int
@@ -402,8 +380,8 @@ def create_from_date(cls, year=None, month=None, day=None, tz='UTC'):
402380
return cls.create(year, month, day, tz=tz)
403381

404382
@classmethod
405-
def create_from_time(cls, hour=None, minute=None, second=None,
406-
microsecond=None, tz='UTC'):
383+
def create_from_time(cls, hour=0, minute=0, second=0,
384+
microsecond=0, tz='UTC'):
407385
"""
408386
Create a Pendulum instance from just a time.
409387
The date portion is set to today.
@@ -2275,10 +2253,10 @@ def _getstate(self):
22752253
)
22762254

22772255
def __setstate__(self, year, month, day, hour, minute, second, microsecond, tz):
2278-
self._datetime = datetime.datetime(self._create_datetime(
2279-
tz, year, month, day,
2256+
self._datetime = tz.convert(datetime.datetime(
2257+
year, month, day,
22802258
hour, minute, second, microsecond
2281-
)[:-1])
2259+
))
22822260
self._tz = tz
22832261

22842262
def __reduce__(self):

tests/pendulum_tests/test_construct.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,30 @@ def test_now(self):
127127
in_paris = Pendulum.now('Europe/Paris')
128128

129129
self.assertNotEqual(now.hour, in_paris.hour)
130+
131+
def test_create(self):
132+
with self.wrap_with_test_now():
133+
now = Pendulum.now()
134+
d = Pendulum.create()
135+
self.assertPendulum(d, now.year, now.month, now.day, 0, 0, 0, 0)
136+
137+
d = Pendulum.create(year=1975)
138+
self.assertPendulum(d, 1975, now.month, now.day, 0, 0, 0, 0)
139+
140+
d = Pendulum.create(month=11)
141+
self.assertPendulum(d, now.year, 11, now.day, 0, 0, 0, 0)
142+
143+
d = Pendulum.create(day=27)
144+
self.assertPendulum(d, now.year, now.month, 27, 0, 0, 0, 0)
145+
146+
d = Pendulum.create(hour=12)
147+
self.assertPendulum(d, now.year, now.month, now.day, 12, 0, 0, 0)
148+
149+
d = Pendulum.create(minute=12)
150+
self.assertPendulum(d, now.year, now.month, now.day, 0, 12, 0, 0)
151+
152+
d = Pendulum.create(second=12)
153+
self.assertPendulum(d, now.year, now.month, now.day, 0, 0, 12, 0)
154+
155+
d = Pendulum.create(microsecond=123456)
156+
self.assertPendulum(d, now.year, now.month, now.day, 0, 0, 0, 123456)

tests/pendulum_tests/test_create_from_date.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@ class CreateFromDateTest(AbstractTestCase):
99

1010
def test_create_from_date_with_defaults(self):
1111
d = Pendulum.create_from_date()
12-
self.assertEqual(d.timestamp, Pendulum.utcnow().timestamp)
12+
self.assertEqual(d.timestamp, Pendulum.utcnow().with_time(0, 0, 0, 0).timestamp)
1313

1414
def test_create_from_date(self):
1515
d = Pendulum.create_from_date(1975, 12, 25)
16-
now = Pendulum.utcnow()
17-
self.assertPendulum(d, 1975, 12, 25, now.hour, now.minute)
16+
self.assertPendulum(d, 1975, 12, 25, 0, 0, 0)
1817

1918
def test_create_from_date_with_year(self):
2019
d = Pendulum.create_from_date(1975)

tests/pendulum_tests/test_create_from_time.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class CreateFromTimeTest(AbstractTestCase):
99

1010
def test_create_from_time_with_defaults(self):
1111
d = Pendulum.create_from_time()
12-
self.assertEqual(d.timestamp, Pendulum.utcnow().timestamp)
12+
self.assertEqual(d.timestamp, Pendulum.utcnow().with_time(0, 0, 0, 0).timestamp)
1313
self.assertEqual('UTC', d.timezone_name)
1414

1515
def test_create_from_time(self):
@@ -22,9 +22,9 @@ def test_create_from_time_with_hour(self):
2222
with Pendulum.test(Pendulum(2016, 8, 11, 12, 34, 56, 123456)):
2323
d = Pendulum.create_from_time(23)
2424
self.assertEqual(23, d.hour)
25-
self.assertEqual(34, d.minute)
26-
self.assertEqual(56, d.second)
27-
self.assertEqual(123456, d.microsecond)
25+
self.assertEqual(0, d.minute)
26+
self.assertEqual(0, d.second)
27+
self.assertEqual(0, d.microsecond)
2828

2929
def test_create_from_time_with_minute(self):
3030
d = Pendulum.create_from_time(minute=5)

0 commit comments

Comments
 (0)