Skip to content

Commit ed85a9d

Browse files
committed
Adds a datetime() helper method to the Timezone class
1 parent 863b3a9 commit ed85a9d

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

docs/_docs/timezones.rst

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,6 @@ when adding and subtracting time around transition times.
215215
'2013-03-31T03:00:00+02:00'
216216
217217
218-
219218
.. note::
220219

221220
You can control the normalization behavior:
@@ -233,3 +232,15 @@ when adding and subtracting time around transition times.
233232
'2013-03-31T02:30:00+01:00'
234233
tz.convert(dt, dst_rule=tz.TRANSITION_ERROR)
235234
# NonExistingTime: The datetime 2013-03-31 02:30:00 does not exist.
235+
236+
237+
You can also get a normalized ``datetime`` object from a ``Timezone`` by using the ``datetime()`` method:
238+
239+
.. code-block:: python
240+
241+
import pendulum
242+
243+
tz = pendulum.timezone('Europe/Paris')
244+
dt = tz.datetime(2013, 3, 31, 2, 30)
245+
dt.isoformat()
246+
'2013-03-31T03:30:00+02:00'

pendulum/tz/timezone.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,38 @@ def convert(self, dt, dst_rule=None):
118118

119119
return dt.__class__(*converted[0], **converted[1])
120120

121+
def datetime(self, year, month, day,
122+
hour=0, minute=0, second=0, microsecond=0):
123+
"""
124+
Creates a new datetime object for the current timezone.
125+
126+
:param year: The year
127+
:type year: int
128+
129+
:param month: The month
130+
:type month: int
131+
132+
:param day: The day
133+
:type day: int
134+
135+
:param hour: The hour
136+
:type hour: int
137+
138+
:param minute: The minute
139+
:type minute: int
140+
141+
:param second: The second
142+
:type second: int
143+
144+
:param microsecond: The microsecond
145+
:type microsecond: int
146+
147+
:rtype: datetime
148+
"""
149+
dt = datetime(year, month, day, hour, minute, second, microsecond)
150+
151+
return self.convert(dt, dst_rule=self.POST_TRANSITION)
152+
121153
def _normalize(self, dt, dst_rule=None):
122154
# if tzinfo is set, something wrong happened
123155
if dt.tzinfo is not None:

tests/tz_tests/test_timezone.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,3 +283,24 @@ def test_timezone_with_no_transitions(self):
283283
self.assertEqual(dt.hour, 0)
284284
self.assertEqual(dt.minute, 0)
285285
self.assertEqual(dt.second, 0)
286+
287+
def test_datetime(self):
288+
tz = timezone('Europe/Paris')
289+
290+
dt = tz.datetime(2013, 3, 24, 1, 30)
291+
self.assertEqual(2013, dt.year)
292+
self.assertEqual(3, dt.month)
293+
self.assertEqual(24, dt.day)
294+
self.assertEqual(1, dt.hour)
295+
self.assertEqual(30, dt.minute)
296+
self.assertEqual(0, dt.second)
297+
self.assertEqual(0, dt.microsecond)
298+
299+
dt = tz.datetime(2013, 3, 31, 2, 30)
300+
self.assertEqual(2013, dt.year)
301+
self.assertEqual(3, dt.month)
302+
self.assertEqual(31, dt.day)
303+
self.assertEqual(3, dt.hour)
304+
self.assertEqual(30, dt.minute)
305+
self.assertEqual(0, dt.second)
306+
self.assertEqual(0, dt.microsecond)

0 commit comments

Comments
 (0)