Skip to content

Commit 6f2a2bf

Browse files
committed
Added on() and at() methods which replace with_date() and with_time().
1 parent 4a8fe84 commit 6f2a2bf

File tree

4 files changed

+132
-38
lines changed

4 files changed

+132
-38
lines changed

CHANGELOG.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22

33
## [Unreleased]
44

5-
### Fixed
5+
### Added
6+
7+
- Added `on()` and `at()` methods which replace `with_date()` and `with_time()`.
8+
9+
### Deprecated
610

7-
- Fixed parsing of some ISO 8601 strings.
11+
- `with_date()` and `with_time()` are deprecated. Use `on()` and `at()` instead.
812

913

1014
## [0.7.0] - 2016-12-07

docs/_docs/fluent_helpers.rst

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,17 @@ setting the timestamp will not set the corresponding timezone to UTC.
1010
.. code-block:: python
1111
1212
import pendulum
13+
from datetime import time, date
1314
1415
dt = pendulum.now()
1516
16-
dt.year_(1975).month_(5).day_(21).hour_(22).minute_(32).second_(5).to_datetime_string()
17-
'1975-05-21 22:32:05'
17+
dt = dt.year_(1975).month_(5).day_(21).to_datetime_string()
18+
'1975-05-21 13:45:18'
19+
20+
dt.hour_(22).minute_(32).second_(5).to_datetime_string()
21+
'2016-11-16 22:32:05'
1822
19-
dt.with_date(1975, 5, 21).with_time(22, 32, 5).to_datetime_string()
23+
dt.on(1975, 5, 21).at(22, 32, 5).to_datetime_string()
2024
'1975-05-21 22:32:05'
2125
2226
dt.timestamp_(169957925).timezone_('Europe/London')

pendulum/pendulum.py

Lines changed: 75 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import calendar
66
import datetime
7+
import warnings
78

89
from .date import Date
910
from .time import Time
@@ -612,7 +613,7 @@ def date(self):
612613
def time(self):
613614
return Time(self.hour, self.minute, self.second, self.microsecond)
614615

615-
def with_date(self, year, month, day):
616+
def on(self, year, month, day):
616617
"""
617618
Returns a new instance with the current date set to a different date.
618619
@@ -627,13 +628,34 @@ def with_date(self, year, month, day):
627628
628629
:rtype: Pendulum
629630
"""
630-
dt = self.replace(
631+
return self.replace(
631632
year=int(year), month=int(month), day=int(day)
632633
)
633634

634-
return self._tz.convert(dt)
635+
def with_date(self, year, month, day):
636+
"""
637+
Returns a new instance with the current date set to a different date.
635638
636-
def with_time(self, hour, minute, second, microsecond=0):
639+
:param year: The year
640+
:type year: int
641+
642+
:param month: The month
643+
:type month: int
644+
645+
:param day: The day
646+
:type day: int
647+
648+
:rtype: Pendulum
649+
"""
650+
warnings.warn(
651+
'with_date() is deprecated. Use on() instead.',
652+
category=DeprecationWarning,
653+
stacklevel=2
654+
)
655+
656+
return self.on(year, month, day)
657+
658+
def at(self, hour, minute, second, microsecond=0):
637659
"""
638660
Returns a new instance with the current time to a different time.
639661
@@ -651,13 +673,36 @@ def with_time(self, hour, minute, second, microsecond=0):
651673
652674
:rtype: Pendulum
653675
"""
654-
dt = self._datetime.replace(
655-
hour=int(hour), minute=int(minute), second=int(second),
656-
microsecond=microsecond,
657-
tzinfo=None
676+
return self.replace(
677+
hour=hour, minute=minute, second=second,
678+
microsecond=microsecond
679+
)
680+
681+
def with_time(self, hour, minute, second, microsecond=0):
682+
"""
683+
Returns a new instance with the current time set to a different time.
684+
685+
:param hour: The hour
686+
:type hour: int
687+
688+
:param minute: The minute
689+
:type minute: int
690+
691+
:param second: The second
692+
:type second: int
693+
694+
:param microsecond: The microsecond
695+
:type microsecond: int
696+
697+
:rtype: Pendulum
698+
"""
699+
warnings.warn(
700+
'with_time() is deprecated. Use at() instead.',
701+
category=DeprecationWarning,
702+
stacklevel=2
658703
)
659704

660-
return self.instance(dt, self._tz)
705+
return self.at(hour, minute, second, microsecond)
661706

662707
def with_date_time(self, year, month, day, hour, minute, second, microsecond=0):
663708
"""
@@ -672,15 +717,12 @@ def with_date_time(self, year, month, day, hour, minute, second, microsecond=0):
672717
673718
:rtype: Pendulum
674719
"""
675-
dt = self._datetime.replace(
720+
return self.replace(
676721
year=year, month=month, day=day,
677-
hour=int(hour), minute=int(minute), second=int(second),
678-
microsecond=microsecond,
679-
tzinfo=None
722+
hour=hour, minute=minute, second=second,
723+
microsecond=microsecond
680724
)
681725

682-
return self.instance(dt, self._tz)
683-
684726
def with_time_from_string(self, time):
685727
"""
686728
Returns a new instance with the time set by time string.
@@ -692,11 +734,11 @@ def with_time_from_string(self, time):
692734
"""
693735
time = time.split(':')
694736

695-
hour = time[0]
696-
minute = time[1] if len(time) > 1 else 0
697-
second = time[2] if len(time) > 2 else 0
737+
hour = int(time[0])
738+
minute = int(time[1]) if len(time) > 1 else 0
739+
second = int(time[2]) if len(time) > 2 else 0
698740

699-
return self.with_time(hour, minute, second)
741+
return self.at(hour, minute, second)
700742

701743
def in_timezone(self, tz):
702744
"""
@@ -1425,15 +1467,15 @@ def _start_of_day(self):
14251467
14261468
:rtype: Pendulum
14271469
"""
1428-
return self.with_time(0, 0, 0)
1470+
return self.at(0, 0, 0)
14291471

14301472
def _end_of_day(self):
14311473
"""
14321474
Reset the time to 23:59:59.999999
14331475
14341476
:rtype: Pendulum
14351477
"""
1436-
return self.with_time(23, 59, 59, 999999)
1478+
return self.at(23, 59, 59, 999999)
14371479

14381480
def _start_of_month(self):
14391481
"""
@@ -1753,7 +1795,7 @@ def _first_of_quarter(self, day_of_week=None):
17531795
17541796
:rtype: Pendulum
17551797
"""
1756-
return self.with_date(self.year, self.quarter * 3 - 2, 1).first_of('month', day_of_week)
1798+
return self.on(self.year, self.quarter * 3 - 2, 1).first_of('month', day_of_week)
17571799

17581800
def _last_of_quarter(self, day_of_week=None):
17591801
"""
@@ -1766,7 +1808,7 @@ def _last_of_quarter(self, day_of_week=None):
17661808
17671809
:rtype: Pendulum
17681810
"""
1769-
return self.with_date(self.year, self.quarter * 3, 1).last_of('month', day_of_week)
1811+
return self.on(self.year, self.quarter * 3, 1).last_of('month', day_of_week)
17701812

17711813
def _nth_of_quarter(self, nth, day_of_week):
17721814
"""
@@ -1795,7 +1837,7 @@ def _nth_of_quarter(self, nth, day_of_week):
17951837
if last_month < dt.month or year != dt.year:
17961838
return False
17971839

1798-
return self.with_date(self.year, dt.month, dt.day).start_of('day')
1840+
return self.on(self.year, dt.month, dt.day).start_of('day')
17991841

18001842
def _first_of_year(self, day_of_week=None):
18011843
"""
@@ -1848,7 +1890,7 @@ def _nth_of_year(self, nth, day_of_week):
18481890
if year != dt.year:
18491891
return False
18501892

1851-
return self.with_date(self.year, dt.month, dt.day).start_of('day')
1893+
return self.on(self.year, dt.month, dt.day).start_of('day')
18521894

18531895
def average(self, dt=None):
18541896
"""
@@ -1939,7 +1981,8 @@ def utctimetuple(self):
19391981
return self._datetime.utctimetuple()
19401982

19411983
def replace(self, year=None, month=None, day=None, hour=None,
1942-
minute=None, second=None, microsecond=None, tzinfo=True):
1984+
minute=None, second=None, microsecond=None, tzinfo=True,
1985+
fold=None):
19431986
year = year if year is not None else self._year
19441987
month = month if month is not None else self._month
19451988
day = day if day is not None else self._day
@@ -1952,14 +1995,14 @@ def replace(self, year=None, month=None, day=None, hour=None,
19521995
if tzinfo is not None and tzinfo is not True:
19531996
tzinfo = self._safe_create_datetime_zone(tzinfo)
19541997
elif tzinfo is None:
1955-
tzinfo = tzinfo
1998+
tzinfo = UTC
19561999
else:
1957-
tzinfo = self._tzinfo
2000+
tzinfo = self._tzinfo.tz
19582001

1959-
return self.instance(
1960-
self._datetime.replace(year=year, month=month, day=day,
1961-
hour=hour, minute=minute, second=second,
1962-
microsecond=microsecond, tzinfo=tzinfo)
2002+
return self.__class__(
2003+
year, month, day,
2004+
hour, minute, second, microsecond,
2005+
tzinfo=tzinfo, fold=fold
19632006
)
19642007

19652008
def astimezone(self, tz=None):

tests/pendulum_tests/test_fluent_setters.py

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,17 @@ def test_fluid_with_date(self):
8787
self.assertEqual(7, d.month)
8888
self.assertEqual(2, d.day)
8989

90+
def test_fluid_on(self):
91+
d = Pendulum.create(2016, 7, 2, 0, 41, 20)
92+
new = d.on(1995, 11, 9)
93+
self.assertIsInstanceOfPendulum(new)
94+
self.assertEqual(1995, new.year)
95+
self.assertEqual(11, new.month)
96+
self.assertEqual(9, new.day)
97+
self.assertEqual(2016, d.year)
98+
self.assertEqual(7, d.month)
99+
self.assertEqual(2, d.day)
100+
90101
def test_fluid_with_date_with_transition(self):
91102
d = Pendulum.create(2013, 3, 31, 0, 0, 0, 0, 'Europe/Paris')
92103
new = d.with_date(2013, 4, 1)
@@ -100,7 +111,20 @@ def test_fluid_with_date_with_transition(self):
100111
self.assertEqual(31, d.day)
101112
self.assertEqual(3600, d.offset)
102113

103-
def test_fluid_set_time(self):
114+
def test_fluid_on_with_transition(self):
115+
d = Pendulum.create(2013, 3, 31, 0, 0, 0, 0, 'Europe/Paris')
116+
new = d.on(2013, 4, 1)
117+
self.assertIsInstanceOfPendulum(new)
118+
self.assertEqual(2013, new.year)
119+
self.assertEqual(4, new.month)
120+
self.assertEqual(1, new.day)
121+
self.assertEqual(7200, new.offset)
122+
self.assertEqual(2013, d.year)
123+
self.assertEqual(3, d.month)
124+
self.assertEqual(31, d.day)
125+
self.assertEqual(3600, d.offset)
126+
127+
def test_fluid_with_time(self):
104128
d = Pendulum.create(2016, 7, 2, 0, 41, 20)
105129
new = d.with_time(5, 32, 49)
106130
self.assertIsInstanceOfPendulum(new)
@@ -111,6 +135,17 @@ def test_fluid_set_time(self):
111135
self.assertEqual(41, d.minute)
112136
self.assertEqual(20, d.second)
113137

138+
def test_fluid_at(self):
139+
d = Pendulum.create(2016, 7, 2, 0, 41, 20)
140+
new = d.at(5, 32, 49)
141+
self.assertIsInstanceOfPendulum(new)
142+
self.assertEqual(5, new.hour)
143+
self.assertEqual(32, new.minute)
144+
self.assertEqual(49, new.second)
145+
self.assertEqual(0, d.hour)
146+
self.assertEqual(41, d.minute)
147+
self.assertEqual(20, d.second)
148+
114149
def test_fluid_set_time_with_transition(self):
115150
d = Pendulum.create(2013, 3, 31, 0, 0, 0, 0, 'Europe/Paris')
116151
new = d.with_time(2, 30, 0)
@@ -119,6 +154,14 @@ def test_fluid_set_time_with_transition(self):
119154
self.assertEqual(30, new.minute)
120155
self.assertEqual(0, new.second)
121156

157+
def test_fluid_at_with_transition(self):
158+
d = Pendulum.create(2013, 3, 31, 0, 0, 0, 0, 'Europe/Paris')
159+
new = d.at(2, 30, 0)
160+
self.assertIsInstanceOfPendulum(new)
161+
self.assertEqual(3, new.hour)
162+
self.assertEqual(30, new.minute)
163+
self.assertEqual(0, new.second)
164+
122165
def test_fluid_set_timestamp(self):
123166
d = Pendulum.create(2016, 7, 2, 0, 41, 20)
124167
new = d.timestamp_(0)

0 commit comments

Comments
 (0)