Skip to content

Commit

Permalink
datetime: Make "fold" attribute handling optional
Browse files Browse the repository at this point in the history
Python's datetime objects have a "fold" attribute since Python 3.6,
which was properly forwarded when converting between Python's datetime
and Hamster's datetime. However, this prevented to code from working
with Python versions below 3.6.

This commit makes handling of the fold attribute optional (e.g. it is
only used when present), which makes this code work on older Python
versions again.
  • Loading branch information
matthijskooijman committed Apr 30, 2020
1 parent 5a930a9 commit 0ba8407
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions src/hamster/lib/datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,12 @@ class time(pdt.time):
def __new__(cls,
hour=0, minute=0,
second=0, microsecond=0,
tzinfo=None, fold=0):
tzinfo=None, **kwargs):
# round down to zero seconds and microseconds
return pdt.time.__new__(cls,
hour=hour, minute=minute,
second=0, microsecond=0,
tzinfo=None, fold=fold)
tzinfo=None, **kwargs)

@classmethod
def _extract_time(cls, match, h="hour", m="minute"):
Expand Down Expand Up @@ -220,12 +220,12 @@ class datetime(pdt.datetime):
def __new__(cls, year, month, day,
hour=0, minute=0,
second=0, microsecond=0,
tzinfo=None, *, fold=0):
tzinfo=None, **kwargs):
# round down to zero seconds and microseconds
return pdt.datetime.__new__(cls, year, month, day,
hour=hour, minute=minute,
second=0, microsecond=0,
tzinfo=None, fold=fold)
tzinfo=None, **kwargs)

def __add__(self, other):
# python datetime.__add__ was not type stable prior to 3.8
Expand All @@ -234,11 +234,13 @@ def __add__(self, other):
# similar to https://stackoverflow.com/q/51966126/3565696
# __getnewargs_ex__ did not work, brute force required
def __deepcopy__(self, memo):
kwargs = {}
if (hasattr(self, 'fold')):
kwargs['fold'] = self.fold
return datetime(self.year, self.month, self.day,
self.hour, self.minute,
self.second, self.microsecond,
self.tzinfo, fold=self.fold)

self.tzinfo, **kwargs)
__radd__ = __add__

def __sub__(self, other):
Expand Down Expand Up @@ -327,10 +329,13 @@ def from_day_time(cls, d: hday, t: time):
@classmethod
def from_pdt(cls, t):
"""Convert python datetime to hamster datetime."""
kwargs = {}
if (hasattr(t, 'fold')):
kwargs['fold'] = t.fold
return cls(t.year, t.month, t.day,
t.hour, t.minute,
t.second, t.microsecond,
t.tzinfo, fold=t.fold)
t.tzinfo, **kwargs)

@classmethod
def now(cls):
Expand Down Expand Up @@ -392,10 +397,13 @@ def pattern(cls, n=None):

def to_pdt(self):
"""Convert to python datetime."""
kwargs = {}
if (hasattr(self, 'fold')):
kwargs['fold'] = self.fold
return pdt.datetime(self.year, self.month, self.day,
self.hour, self.minute,
self.second, self.microsecond,
self.tzinfo, fold=self.fold)
self.tzinfo, **kwargs)


# outside class; need the class to be defined first
Expand Down

0 comments on commit 0ba8407

Please sign in to comment.