Skip to content

Commit

Permalink
Force all datetime objects to be timezone aware.
Browse files Browse the repository at this point in the history
This works around some strangeness in the modified datetime class due to
it being derived from a native C object. The timezone was being set to
None in the base class's __new__ function, and the corrected timezone
was ignored when set by the derived class's __init__.
  • Loading branch information
wagnerrp committed Sep 28, 2012
1 parent 358af8a commit 9c4e49c
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions mythtv/bindings/python/MythTV/utility/dt.py
Expand Up @@ -411,12 +411,22 @@ def duck(cls, t):
pass
raise TypeError("time data '%s' does not match supported formats"%t)

def __init__(self, year, month, day, hour=None, minute=None, second=None,
microsecond=None, tzinfo=None):
def __new__(cls, year, month, day, hour=None, minute=None, second=None,
microsecond=None, tzinfo=None):

if tzinfo is None:
tzinfo = self.localTZ()
super(datetime, self).__init__(year, month, day, hour, minute, second,\
microsecond, tzinfo)
kwargs = {'tzinfo':cls.localTZ()}
else:
kwargs = {'tzinfo':tzinfo}
if hour is not None:
kwargs['hour'] = hour
if minute is not None:
kwargs['minute'] = minute
if second is not None:
kwargs['second'] = second
if microsecond is not None:
kwargs['microsecond'] = microsecond
return _pydatetime.__new__(cls, year, month, day, **kwargs)

def mythformat(self):
return self.astimezone(self.UTCTZ()).strftime('%Y%m%d%H%M%S')
Expand Down

0 comments on commit 9c4e49c

Please sign in to comment.