Skip to content

Instantly share code, notes, and snippets.

@hsauers5
Last active March 17, 2020 23:06
Show Gist options
  • Save hsauers5/60a102303525cb30c81e5da5e6a39773 to your computer and use it in GitHub Desktop.
Save hsauers5/60a102303525cb30c81e5da5e6a39773 to your computer and use it in GitHub Desktop.
import datetime
import copy
class IncrementableDate:
def __init__(self, date_time=None, datestring=None, dateformat='%Y%m%d'):
if date_time is not None:
self.value = date_time
elif datestring is not None:
self.value = datetime.datetime.strptime(datestring, dateformat)
def to_string(self, dateformat='%Y%m%d'):
return datetime.datetime.strftime(self.value, dateformat)
def increment(self, offset=1):
self.value += datetime.timedelta(days=offset)
def copy(self):
return copy.copy(self)
def __eq__(self, other):
if isinstance(other, IncrementableDate):
return self.value == other.value
return NotImplemented
def __ne__(self, other):
x = self.__eq__(other)
if x is not NotImplemented:
return not x
return NotImplemented
def __gt__(self, other):
if isinstance(other, IncrementableDate):
return self.value > other.value
return NotImplemented
def __lt__(self, other):
if isinstance(other, IncrementableDate):
return self.value < other.value
return NotImplemented
def __ge__(self, other):
return self.__gt__(other) or self.__eq__(other)
def __le__(self, other):
return self.__lt__(other) or self.__eq__(other)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment