1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
'''
Task Coach - Your friendly task manager
Copyright (C) 2004-2014 Task Coach developers <developers@taskcoach.org>
Task Coach is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Task Coach is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
import time, string, datetime, re, timedelta # pylint: disable=W0402
from taskcoachlib import patterns
from .fix import StrftimeFix
infinite = datetime.date.max
class RealDate(StrftimeFix, datetime.date):
def __add__(self, delta):
newdate = super(RealDate, self).__add__(delta)
return RealDate(newdate.year, newdate.month, newdate.day)
def __sub__(self, other):
newdate = super(RealDate, self).__sub__(other)
if isinstance(newdate, datetime.timedelta):
return timedelta.TimeDelta(newdate.days, newdate.seconds, newdate.microseconds)
else:
return RealDate(newdate.year, newdate.month, newdate.day)
class InfiniteDate(datetime.date):
__metaclass__ = patterns.Singleton
def __new__(self):
return super(InfiniteDate, self).__new__(InfiniteDate, infinite.year,
infinite.month, infinite.day)
def _getyear(self):
return None
year = property(_getyear)
def _getmonth(self):
return None
month = property(_getmonth)
def _getday(self):
return None
day = property(_getday)
def __str__(self):
return ''
def __sub__(self, other):
if isinstance(other, InfiniteDate):
return timedelta.TimeDelta()
else:
return timedelta.TimeDelta.max
def __add__(self, delta):
# Whatever is added to InfiniteDate, we're still infinite:
return self
# factories:
def parseDate(dateString, default=None):
try:
return Date(*[string.atoi(part) for part in dateString.split('-')])
except ValueError:
if default:
return default
else:
return Date()
def Date(year=infinite.year, month=infinite.month, day=infinite.day):
if (year, month, day) == (infinite.year, infinite.month, infinite.day):
return InfiniteDate()
else:
return RealDate(year, month, day)
|