home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_2370 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  11.8 KB  |  391 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __author__ = 'Gustavo Niemeyer <gustavo@niemeyer.net>'
  5. __license__ = 'PSF License'
  6. import datetime
  7. import calendar
  8. __all__ = [
  9.     'relativedelta',
  10.     'MO',
  11.     'TU',
  12.     'WE',
  13.     'TH',
  14.     'FR',
  15.     'SA',
  16.     'SU']
  17.  
  18. class weekday(object):
  19.     __slots__ = [
  20.         'weekday',
  21.         'n']
  22.     
  23.     def __init__(self, weekday, n = None):
  24.         self.weekday = weekday
  25.         self.n = n
  26.  
  27.     
  28.     def __call__(self, n):
  29.         if n == self.n:
  30.             return self
  31.         return self.__class__(self.weekday, n)
  32.  
  33.     
  34.     def __eq__(self, other):
  35.         
  36.         try:
  37.             if self.weekday != other.weekday or self.n != other.n:
  38.                 return False
  39.         except AttributeError:
  40.             return False
  41.  
  42.         return True
  43.  
  44.     
  45.     def __repr__(self):
  46.         s = ('MO', 'TU', 'WE', 'TH', 'FR', 'SA', 'SU')[self.weekday]
  47.         if not self.n:
  48.             return s
  49.         return '%s(%+d)' % (s, self.n)
  50.  
  51.  
  52.  
  53. class relativedelta:
  54.     
  55.     def __init__(self, dt1 = None, dt2 = None, years = 0, months = 0, days = 0, leapdays = 0, weeks = 0, hours = 0, minutes = 0, seconds = 0, microseconds = 0, year = None, month = None, day = None, weekday = None, yearday = None, nlyearday = None, hour = None, minute = None, second = None, microsecond = None):
  56.         if dt1 and dt2:
  57.             if not isinstance(dt1, datetime.date) or not isinstance(dt2, datetime.date):
  58.                 raise TypeError, 'relativedelta only diffs datetime/date'
  59.             not isinstance(dt2, datetime.date)
  60.             if type(dt1) is not type(dt2):
  61.                 if not isinstance(dt1, datetime.datetime):
  62.                     dt1 = datetime.datetime.fromordinal(dt1.toordinal())
  63.                 elif not isinstance(dt2, datetime.datetime):
  64.                     dt2 = datetime.datetime.fromordinal(dt2.toordinal())
  65.                 
  66.             
  67.             self.years = 0
  68.             self.months = 0
  69.             self.days = 0
  70.             self.leapdays = 0
  71.             self.hours = 0
  72.             self.minutes = 0
  73.             self.seconds = 0
  74.             self.microseconds = 0
  75.             self.year = None
  76.             self.month = None
  77.             self.day = None
  78.             self.weekday = None
  79.             self.hour = None
  80.             self.minute = None
  81.             self.second = None
  82.             self.microsecond = None
  83.             self._has_time = 0
  84.             months = dt1.year * 12 + dt1.month - (dt2.year * 12 + dt2.month)
  85.             self._set_months(months)
  86.             dtm = self.__radd__(dt2)
  87.             if dt1 < dt2:
  88.                 while dt1 > dtm:
  89.                     months += 1
  90.                     self._set_months(months)
  91.                     dtm = self.__radd__(dt2)
  92.             else:
  93.                 while dt1 < dtm:
  94.                     months -= 1
  95.                     self._set_months(months)
  96.                     dtm = self.__radd__(dt2)
  97.             delta = dt1 - dtm
  98.             self.seconds = delta.seconds + delta.days * 86400
  99.             self.microseconds = delta.microseconds
  100.         else:
  101.             self.years = years
  102.             self.months = months
  103.             self.days = days + weeks * 7
  104.             self.leapdays = leapdays
  105.             self.hours = hours
  106.             self.minutes = minutes
  107.             self.seconds = seconds
  108.             self.microseconds = microseconds
  109.             self.year = year
  110.             self.month = month
  111.             self.day = day
  112.             self.hour = hour
  113.             self.minute = minute
  114.             self.second = second
  115.             self.microsecond = microsecond
  116.             if type(weekday) is int:
  117.                 self.weekday = weekdays[weekday]
  118.             else:
  119.                 self.weekday = weekday
  120.             yday = 0
  121.             if nlyearday:
  122.                 yday = nlyearday
  123.             elif yearday:
  124.                 yday = yearday
  125.                 if yearday > 59:
  126.                     self.leapdays = -1
  127.                 
  128.             
  129.             if yday:
  130.                 ydayidx = [
  131.                     31,
  132.                     59,
  133.                     90,
  134.                     120,
  135.                     151,
  136.                     181,
  137.                     212,
  138.                     243,
  139.                     273,
  140.                     304,
  141.                     334,
  142.                     366]
  143.                 for idx, ydays in enumerate(ydayidx):
  144.                     if yday <= ydays:
  145.                         self.month = idx + 1
  146.                         if idx == 0:
  147.                             self.day = ydays
  148.                         else:
  149.                             self.day = yday - ydayidx[idx - 1]
  150.                         break
  151.                         continue
  152.                 else:
  153.                     raise ValueError, 'invalid year day (%d)' % yday
  154.         self._fix()
  155.  
  156.     
  157.     def _fix(self):
  158.         if abs(self.microseconds) > 999999:
  159.             s = self.microseconds // abs(self.microseconds)
  160.             (div, mod) = divmod(self.microseconds * s, 1000000)
  161.             self.microseconds = mod * s
  162.             self.seconds += div * s
  163.         
  164.         if abs(self.seconds) > 59:
  165.             s = self.seconds // abs(self.seconds)
  166.             (div, mod) = divmod(self.seconds * s, 60)
  167.             self.seconds = mod * s
  168.             self.minutes += div * s
  169.         
  170.         if abs(self.minutes) > 59:
  171.             s = self.minutes // abs(self.minutes)
  172.             (div, mod) = divmod(self.minutes * s, 60)
  173.             self.minutes = mod * s
  174.             self.hours += div * s
  175.         
  176.         if abs(self.hours) > 23:
  177.             s = self.hours // abs(self.hours)
  178.             (div, mod) = divmod(self.hours * s, 24)
  179.             self.hours = mod * s
  180.             self.days += div * s
  181.         
  182.         if abs(self.months) > 11:
  183.             s = self.months // abs(self.months)
  184.             (div, mod) = divmod(self.months * s, 12)
  185.             self.months = mod * s
  186.             self.years += div * s
  187.         
  188.         if self.hours and self.minutes and self.seconds and self.microseconds and self.hour is not None and self.minute is not None and self.second is not None or self.microsecond is not None:
  189.             self._has_time = 1
  190.         else:
  191.             self._has_time = 0
  192.  
  193.     
  194.     def _set_months(self, months):
  195.         self.months = months
  196.         if abs(self.months) > 11:
  197.             s = self.months // abs(self.months)
  198.             (div, mod) = divmod(self.months * s, 12)
  199.             self.months = mod * s
  200.             self.years = div * s
  201.         else:
  202.             self.years = 0
  203.  
  204.     
  205.     def __radd__(self, other):
  206.         if not isinstance(other, datetime.date):
  207.             raise TypeError, 'unsupported type for add operation'
  208.         isinstance(other, datetime.date)
  209.         if self._has_time and not isinstance(other, datetime.datetime):
  210.             other = datetime.datetime.fromordinal(other.toordinal())
  211.         
  212.         if not self.year:
  213.             pass
  214.         year = other.year + self.years
  215.         if not self.month:
  216.             pass
  217.         month = other.month
  218.         if self.months:
  219.             month += self.months
  220.             if month > 12:
  221.                 year += 1
  222.                 month -= 12
  223.             elif month < 1:
  224.                 year -= 1
  225.                 month += 12
  226.             
  227.         
  228.         if not self.day:
  229.             pass
  230.         day = min(calendar.monthrange(year, month)[1], other.day)
  231.         repl = {
  232.             'year': year,
  233.             'month': month,
  234.             'day': day }
  235.         for attr in [
  236.             'hour',
  237.             'minute',
  238.             'second',
  239.             'microsecond']:
  240.             value = getattr(self, attr)
  241.             if value is not None:
  242.                 repl[attr] = value
  243.                 continue
  244.         
  245.         days = self.days
  246.         if self.leapdays and month > 2 and calendar.isleap(year):
  247.             days += self.leapdays
  248.         
  249.         ret = other.replace(**repl) + datetime.timedelta(days = days, hours = self.hours, minutes = self.minutes, seconds = self.seconds, microseconds = self.microseconds)
  250.         if self.weekday:
  251.             if not self.weekday.n:
  252.                 pass
  253.             weekday = self.weekday.weekday
  254.             nth = 1
  255.             jumpdays = (abs(nth) - 1) * 7
  256.             if nth > 0:
  257.                 jumpdays += ((7 - ret.weekday()) + weekday) % 7
  258.             else:
  259.                 jumpdays += (ret.weekday() - weekday) % 7
  260.                 jumpdays *= -1
  261.             ret += datetime.timedelta(days = jumpdays)
  262.         
  263.         return ret
  264.  
  265.     
  266.     def __rsub__(self, other):
  267.         return self.__neg__().__radd__(other)
  268.  
  269.     
  270.     def __add__(self, other):
  271.         if not isinstance(other, relativedelta):
  272.             raise TypeError, 'unsupported type for add operation'
  273.         isinstance(other, relativedelta)
  274.         if not other.leapdays:
  275.             pass
  276.         if not other.year:
  277.             pass
  278.         if not other.month:
  279.             pass
  280.         if not other.day:
  281.             pass
  282.         if not other.weekday:
  283.             pass
  284.         if not other.hour:
  285.             pass
  286.         if not other.minute:
  287.             pass
  288.         if not other.second:
  289.             pass
  290.         if not other.second:
  291.             pass
  292.         return relativedelta(years = other.years + self.years, months = other.months + self.months, days = other.days + self.days, hours = other.hours + self.hours, minutes = other.minutes + self.minutes, seconds = other.seconds + self.seconds, microseconds = other.microseconds + self.microseconds, leapdays = self.leapdays, year = self.year, month = self.month, day = self.day, weekday = self.weekday, hour = self.hour, minute = self.minute, second = self.second, microsecond = self.microsecond)
  293.  
  294.     
  295.     def __sub__(self, other):
  296.         if not isinstance(other, relativedelta):
  297.             raise TypeError, 'unsupported type for sub operation'
  298.         isinstance(other, relativedelta)
  299.         if not other.leapdays:
  300.             pass
  301.         if not other.year:
  302.             pass
  303.         if not other.month:
  304.             pass
  305.         if not other.day:
  306.             pass
  307.         if not other.weekday:
  308.             pass
  309.         if not other.hour:
  310.             pass
  311.         if not other.minute:
  312.             pass
  313.         if not other.second:
  314.             pass
  315.         if not other.second:
  316.             pass
  317.         return relativedelta(years = other.years - self.years, months = other.months - self.months, days = other.days - self.days, hours = other.hours - self.hours, minutes = other.minutes - self.minutes, seconds = other.seconds - self.seconds, microseconds = other.microseconds - self.microseconds, leapdays = self.leapdays, year = self.year, month = self.month, day = self.day, weekday = self.weekday, hour = self.hour, minute = self.minute, second = self.second, microsecond = self.microsecond)
  318.  
  319.     
  320.     def __neg__(self):
  321.         return relativedelta(years = -(self.years), months = -(self.months), days = -(self.days), hours = -(self.hours), minutes = -(self.minutes), seconds = -(self.seconds), microseconds = -(self.microseconds), leapdays = self.leapdays, year = self.year, month = self.month, day = self.day, weekday = self.weekday, hour = self.hour, minute = self.minute, second = self.second, microsecond = self.microsecond)
  322.  
  323.     
  324.     def __nonzero__(self):
  325.         if not (self.years) and not (self.months) and not (self.days) and not (self.hours) and not (self.minutes) and not (self.seconds) and not (self.microseconds) and not (self.leapdays) and self.year is None and self.month is None and self.day is None and self.weekday is None and self.hour is None and self.minute is None and self.second is None:
  326.             pass
  327.         return not (self.microsecond is None)
  328.  
  329.     
  330.     def __mul__(self, other):
  331.         f = float(other)
  332.         return relativedelta(years = self.years * f, months = self.months * f, days = self.days * f, hours = self.hours * f, minutes = self.minutes * f, seconds = self.seconds * f, microseconds = self.microseconds * f, leapdays = self.leapdays, year = self.year, month = self.month, day = self.day, weekday = self.weekday, hour = self.hour, minute = self.minute, second = self.second, microsecond = self.microsecond)
  333.  
  334.     
  335.     def __eq__(self, other):
  336.         if not isinstance(other, relativedelta):
  337.             return False
  338.         if self.weekday or other.weekday:
  339.             if not (self.weekday) or not (other.weekday):
  340.                 return False
  341.             if self.weekday.weekday != other.weekday.weekday:
  342.                 return False
  343.             n1 = self.weekday.n
  344.             n2 = other.weekday.n
  345.         if self.years == other.years and self.months == other.months and self.days == other.days and self.hours == other.hours and self.minutes == other.minutes and self.seconds == other.seconds and self.leapdays == other.leapdays and self.year == other.year and self.month == other.month and self.day == other.day and self.hour == other.hour and self.minute == other.minute and self.second == other.second:
  346.             pass
  347.         return self.microsecond == other.microsecond
  348.  
  349.     
  350.     def __ne__(self, other):
  351.         return not self.__eq__(other)
  352.  
  353.     
  354.     def __div__(self, other):
  355.         return self.__mul__(1 / float(other))
  356.  
  357.     
  358.     def __repr__(self):
  359.         l = []
  360.         for attr in [
  361.             'years',
  362.             'months',
  363.             'days',
  364.             'leapdays',
  365.             'hours',
  366.             'minutes',
  367.             'seconds',
  368.             'microseconds']:
  369.             value = getattr(self, attr)
  370.             if value:
  371.                 l.append('%s=%+d' % (attr, value))
  372.                 continue
  373.         
  374.         for attr in [
  375.             'year',
  376.             'month',
  377.             'day',
  378.             'weekday',
  379.             'hour',
  380.             'minute',
  381.             'second',
  382.             'microsecond']:
  383.             value = getattr(self, attr)
  384.             if value is not None:
  385.                 l.append('%s=%s' % (attr, `value`))
  386.                 continue
  387.         
  388.         return '%s(%s)' % (self.__class__.__name__, ', '.join(l))
  389.  
  390.  
  391.