Skip to content

Commit 552c34d

Browse files
committed
Adds support for external tzinfo in instance() method.
1 parent 0711750 commit 552c34d

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

pendulum/pendulum.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,13 @@ def instance(cls, dt, tz=UTC):
214214
tz = tz.zone
215215
else:
216216
# We have no sure way to figure out
217-
# the timezone name, we raise an error
218-
raise ValueError('Unsupported tzinfo [{}]'.format(tz))
217+
# the timezone name, we fallback
218+
# on a fixed offset
219+
offset = dt.utcoffset()
220+
if not offset:
221+
raise ValueError('Unsupported tzinfo [{}]'.format(tz))
222+
223+
tz = dt.utcoffset().total_seconds() / 3600
219224

220225
return cls(
221226
dt.year, dt.month, dt.day,

tests/pendulum_tests/test_construct.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import os
44
import pytz
55
from datetime import datetime
6+
from dateutil import tz
67
from pendulum import Pendulum
78
from pendulum.tz import timezone
89
from pendulum.tz.timezone_info import TimezoneInfo
@@ -122,6 +123,11 @@ def test_instance_timezone_aware_datetime_pytz(self):
122123
)
123124
self.assertEqual('Europe/Paris', now.timezone_name)
124125

126+
def test_instance_timezone_aware_datetime_any_tzinfo(self):
127+
dt = datetime(2016, 8, 7, 12, 34, 56, tzinfo=tz.gettz('Europe/Paris'))
128+
now = Pendulum.instance(dt)
129+
self.assertEqual('+02:00', now.timezone_name)
130+
125131
def test_now(self):
126132
now = Pendulum.now()
127133
in_paris = Pendulum.now('Europe/Paris')

0 commit comments

Comments
 (0)