Package Gnumed :: Package timelinelib :: Package calendar :: Package num :: Module time |
[frames] | no frames]

# Source Code for Module Gnumed.timelinelib.calendar.num.time

     1  # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018  Rickard Lindberg, Roger Lindberg
     2  #
     3  # This file is part of Timeline.
     4  #
     5  # Timeline is free software: you can redistribute it and/or modify
     6  # it under the terms of the GNU General Public License as published by
     7  # the Free Software Foundation, either version 3 of the License, or
     8  # (at your option) any later version.
     9  #
    10  # Timeline is distributed in the hope that it will be useful,
    11  # but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  # GNU General Public License for more details.
    14  #
    15  # You should have received a copy of the GNU General Public License
    16  # along with Timeline.  If not, see <http://www.gnu.org/licenses/>.
    17
    18
    19  from timelinelib.calendar.time import ComparableValue
    20  from timelinelib.calendar.time import GenericDeltaMixin
    21  from timelinelib.calendar.time import GenericTimeMixin
    22
    23
    24 -class NumTime(ComparableValue, GenericTimeMixin):
    25
    26      @property
    27 -    def DeltaClass(self):
    28          return NumDelta
    29
    30 -    def __repr__(self):
    31          return "{0}<{1!r}>".format(self.__class__.__name__, self.value)
    32
    33 -    def __add__(self, other):
    34          if isinstance(other, self.DeltaClass):
    35              # Time + Delta
    36              return self.__class__(self.value + other.value)
    37          else:
    38              return NotImplemented
    39
    40 -    def __sub__(self, other):
    41          if isinstance(other, self.DeltaClass):
    42              # Time - Delta
    43              return self.__class__(self.value - other.value)
    44          elif isinstance(other, self.__class__):
    45              # Time - Time
    46              return self.DeltaClass(self.value - other.value)
    47          else:
    48              return NotImplemented
    49
    50
    51 -class NumDelta(ComparableValue, GenericDeltaMixin):
    52
    53 -    def __repr__(self):
    54          return "{0}<{1!r}>".format(self.__class__.__name__, self.value)
    55
    56 -    def __sub__(self, other):
    57          if isinstance(other, self.__class__):
    58              # Delta - Delta
    59              return self.__class__(self.value - other.value)
    60          else:
    61              return NotImplemented
    62
    63 -    def __mul__(self, other):
    64          # Delta * number
    65          return self.__class__(self.value * other)
    66
    67 -    def __div__(self, other):
    68          if isinstance(other, self.__class__):
    69              # Delta / Delta
    70              return float(self.value) / float(other.value)
    71          else:
    72              # Delta / number
    73              return self.__class__(self.value / other)
    74