Skip to content

Commit fac1f09

Browse files
committed
Adds support for pytz timezones in constructor
1 parent 1fabeba commit fac1f09

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

pendulum/pendulum.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,15 @@ def _safe_create_datetime_zone(cls, obj):
102102
timezone_offset = obj * 60 * 60
103103

104104
return FixedTimezone(timezone_offset)
105+
elif isinstance(obj, datetime.tzinfo) and not isinstance(obj, Timezone):
106+
# pytz
107+
if hasattr(obj, 'localize'):
108+
obj = obj.zone
109+
else:
110+
# We have no sure way to figure out
111+
# the timezone name, we raise an error
112+
113+
raise ValueError('Unsupported timezone {}'.format(obj))
105114

106115
tz = cls._timezone(obj)
107116

tests/pendulum_tests/test_create_from_timestamp.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# -*- coding: utf-8 -*-
22

3+
import pytz
34
from pendulum import Pendulum, timezone
45

56
from .. import AbstractTestCase
@@ -21,3 +22,8 @@ def test_create_from_timestamp_with_timezone(self):
2122
d = Pendulum.create_from_timestamp(0, timezone('America/Toronto'))
2223
self.assertEqual('America/Toronto', d.timezone_name)
2324
self.assertPendulum(d, 1969, 12, 31, 19, 0, 0)
25+
26+
def test_create_from_timestamp_with_pytz_timezone(self):
27+
d = Pendulum.create_from_timestamp(0, pytz.timezone('America/Toronto'))
28+
self.assertEqual('America/Toronto', d.timezone_name)
29+
self.assertPendulum(d, 1969, 12, 31, 19, 0, 0)

0 commit comments

Comments
 (0)