Skip to content

Commit b522d40

Browse files
committed
Fixes Timezone._convert() for fixed timezones.
1 parent c598341 commit b522d40

File tree

4 files changed

+38
-8
lines changed

4 files changed

+38
-8
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Change Log
22

3+
## [Unreleased]
4+
5+
### Fixed
6+
7+
- Fixed `Timezone._convert()` method for fixed timezones.
8+
9+
310
## [0.6.0] - 2016-09-12
411

512
### Added
@@ -170,6 +177,7 @@ This version causes major breaking API changes to simplify it and making it more
170177
Initial release
171178

172179

180+
[Unreleased]: https://github.com/sdispater/pendulum/compare/0.6.0...master
173181
[0.6.0]: https://github.com/sdispater/pendulum/releases/tag/0.6.0
174182
[0.5.5]: https://github.com/sdispater/pendulum/releases/tag/0.5.5
175183
[0.5.4]: https://github.com/sdispater/pendulum/releases/tag/0.5.4

pendulum/pendulum.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ def now(cls, tz=None):
261261
Get a Pendulum instance for the current date and time.
262262
263263
:param tz: The timezone
264-
:type tz: Timezone or TimezoneInfo or str or None
264+
:type tz: Timezone or TimezoneInfo or str or int or None
265265
266266
:rtype: Pendulum
267267
"""

pendulum/tz/timezone.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -323,12 +323,13 @@ def _find_utc_index(self, dt):
323323
def __repr__(self):
324324
return '<Timezone [{}]>'.format(self._name)
325325

326+
326327
class FixedTimezone(Timezone):
327328
"""
328329
A timezone that has a fixed offset to UTC.
329330
"""
330331

331-
def __init__(self, offset):
332+
def __init__(self, offset, name=None, transition_type=None):
332333
"""
333334
:param offset: offset to UTC in seconds.
334335
:type offset: int
@@ -338,21 +339,37 @@ def __init__(self, offset):
338339
minutes = offset / 60
339340
hour, minute = divmod(abs(int(minutes)), 60)
340341

341-
name = '{0}{1:02d}:{2:02d}'.format(sign, hour, minute)
342+
if not name:
343+
name = '{0}{1:02d}:{2:02d}'.format(sign, hour, minute)
342344

343-
transition_type = TransitionType(int(offset), False, '')
345+
if not transition_type:
346+
transition_type = TransitionType(int(offset), False, '')
344347

345-
super(FixedTimezone, self).__init__(name, [], [], 0, (0,))
348+
super(FixedTimezone, self).__init__(name, [], [], 0, (datetime(1970, 1, 1),))
346349

347350
self._tzinfos = (TimezoneInfo(self, transition_type),)
348351

352+
def utcoffset(self, dt):
353+
if dt is None:
354+
return None
355+
356+
return self._tzinfos[0].utcoffset(dt)
357+
358+
def dst(self, dt):
359+
if dt is None:
360+
return None
361+
362+
return self._tzinfos[0].dst(dt)
363+
364+
def fromutc(self, dt):
365+
return dt.replace(tzinfo=self._tzinfos[0])
366+
349367

350-
class _UTC(Timezone):
368+
class _UTC(FixedTimezone):
351369

352370
def __init__(self):
353-
super(_UTC, self).__init__('UTC', [], [], 0, (0,))
371+
super(_UTC, self).__init__(0, 'UTC', TransitionType(0, False, 'GMT'))
354372

355-
self._tzinfos = (TimezoneInfo(self, TransitionType(0, False, 'GMT')),)
356373
UTC._tz = self
357374

358375
def fromutc(self, dt):

tests/pendulum_tests/test_construct.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@ def test_now(self):
134134

135135
self.assertNotEqual(now.hour, in_paris.hour)
136136

137+
def test_now_with_fixed_offset(self):
138+
now = Pendulum.now(6)
139+
140+
self.assertEqual(now.timezone_name, '+06:00')
141+
137142
def test_create(self):
138143
with self.wrap_with_test_now(Pendulum(2016, 8, 7, 12, 34, 56)):
139144
now = Pendulum.now()

0 commit comments

Comments
 (0)