Skip to content

Commit 097623b

Browse files
committed
Fixes Time class
1 parent 0116c5e commit 097623b

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

pendulum/time.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,21 +66,24 @@ def instance(cls, t, copy=True):
6666
return cls(t.hour, t.minute, t.second, t.microsecond, t.tzinfo)
6767

6868
@classmethod
69-
def now(cls, with_microseconds=False):
69+
def now(cls, with_microseconds=True):
7070
"""
7171
Return a Time instance corresponding to the current time.
7272
7373
It will return your local time.
7474
75-
By default, it will not include microseconds.
76-
Just set ``with_microseconds`` to ``True`` to include them.
75+
By default, it will include microseconds.
76+
Just set ``with_microseconds`` to ``False`` to exclude them.
7777
7878
:param with_microseconds: Whether to include microseconds or not.
7979
:type with_microseconds: bool
8080
8181
:rtype: Time
8282
"""
8383
if cls.has_test_now():
84+
if not with_microseconds:
85+
return cls.get_test_now().replace(microsecond=0)
86+
8487
return cls.get_test_now()
8588

8689
now = datetime.now()
@@ -490,6 +493,11 @@ def replace(self, hour=None, minute=None, second=None, microsecond=None,
490493
if tzinfo is True:
491494
tzinfo = self._tzinfo
492495

496+
hour = hour if hour is not None else self._hour
497+
minute = minute if minute is not None else self._minute
498+
second = second if second is not None else self._second
499+
microsecond = microsecond if microsecond is not None else self._microsecond
500+
493501
return self.instance(
494502
self._time.replace(
495503
hour, minute, second, microsecond,

tests/time_tests/test_construct.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,11 @@ def test_now(self):
4444
t = Time.now()
4545

4646
self.assertIsInstanceOfTime(t)
47+
48+
def test_now_microseconds(self):
49+
with Time.test(Time(1, 2, 3, 123456)):
50+
t = Time.now()
51+
self.assertTime(t, 1, 2, 3, 123456)
52+
53+
t = Time.now(False)
54+
self.assertTime(t, 1, 2, 3, 0)

0 commit comments

Comments
 (0)