Gnumed.timelinelib.calendar.gregorian.time

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

# Source Code for Module Gnumed.timelinelib.calendar.gregorian.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  SECONDS_IN_DAY = 24 * 60 * 60
     25
     26
     27 -class GregorianTime(GenericTimeMixin):
     28
     29      MIN_JULIAN_DAY = 0
     30
     31      @property
     32 -    def DeltaClass(self):
     33          return GregorianDelta
     34
     35      @classmethod
     36 -    def min(cls):
     37          return cls(cls.MIN_JULIAN_DAY, 0)
     38
     39      @classmethod
     40 -    def set_min_julian_day(cls, allow_negative_julian_yeras):
     41          if allow_negative_julian_yeras:
     42              cls.MIN_JULIAN_DAY = -1000000000000000000000000000000000000000000000000
     43          else:
     44              cls.MIN_JULIAN_DAY = 0
     45
     46 -    def __init__(self, julian_day, seconds):
     47          if julian_day < self.MIN_JULIAN_DAY:
     48              raise ValueError("julian_day must be >= %d" % self.MIN_JULIAN_DAY)
     49          if seconds < 0 or seconds >= SECONDS_IN_DAY:
     50              raise ValueError("seconds must be >= 0 and <= 24*60*60")
     51          self.julian_day = julian_day
     52          self.seconds = seconds
     53
     54 -    def __eq__(self, time):
     55          return (isinstance(time, self.__class__) and
     56                  self.julian_day == time.julian_day and
     57                  self.seconds == time.seconds)
     58
     59 -    def __ne__(self, time):
     60          return not (self == time)
     61
     62 -    def __add__(self, delta):
     63          if isinstance(delta, self.DeltaClass):
     64              seconds = self.seconds + delta.seconds
     65              return self.__class__(self.julian_day + seconds / SECONDS_IN_DAY, seconds % SECONDS_IN_DAY)
     66          raise TypeError(
     67              "%s + %s not supported" % (self.__class__.__name__, type(delta))
     68          )
     69
     70 -    def __sub__(self, other):
     71          if isinstance(other, self.DeltaClass):
     72              seconds = self.seconds - other.seconds
     73              if seconds < 0:
     74                  if seconds % SECONDS_IN_DAY == 0:
     75                      days = abs(seconds) / SECONDS_IN_DAY
     76                      seconds = 0
     77                  else:
     78                      days = abs(seconds) / SECONDS_IN_DAY + 1
     79                      seconds = SECONDS_IN_DAY - abs(seconds) % SECONDS_IN_DAY
     80                  return self.__class__(self.julian_day - days, seconds)
     81              else:
     82                  return self.__class__(self.julian_day, seconds)
     83          else:
     84              days_diff = self.julian_day - other.julian_day
     85              seconds_diff = self.seconds - other.seconds
     86              return self.DeltaClass(days_diff * SECONDS_IN_DAY + seconds_diff)
     87
     88 -    def __gt__(self, dt):
     89          return (self.julian_day, self.seconds) > (dt.julian_day, dt.seconds)
     90
     91 -    def __ge__(self, dt):
     92          return self == dt or self > dt
     93
     94 -    def __lt__(self, dt):
     95          return (self.julian_day, self.seconds) < (dt.julian_day, dt.seconds)
     96
     97 -    def __repr__(self):
     98          return "{0}({1!r}, {2!r})".format(
     99              self.__class__.__name__,
    100              self.julian_day,
    101              self.seconds
    102          )
    103
    104 -    def get_time_of_day(self):
    105          hours = self.seconds / 3600
    106          minutes = (self.seconds / 60) % 60
    107          seconds = self.seconds % 60
    108          return (hours, minutes, seconds)
    109
    110
    111 -class GregorianDelta(ComparableValue, GenericDeltaMixin):
    112
    113      @classmethod
    114 -    def from_seconds(cls, seconds):
    115          return cls(seconds)
    116
    117      @classmethod
    118 -    def from_days(cls, days):
    119          return cls(SECONDS_IN_DAY * days)
    120
    121      @property
    122 -    def seconds(self):
    123          return self.value
    124
    125 -    def __div__(self, value):
    126          if isinstance(value, self.__class__):
    127              return float(self.seconds) / float(value.seconds)
    128          else:
    129              return self.__class__(self.seconds / value)
    130
    131 -    def __sub__(self, delta):
    132          return self.__class__(self.seconds - delta.seconds)
    133
    134 -    def __mul__(self, value):
    135          return self.__class__(int(self.seconds * value))
    136
    137 -    def get_days(self):
    138          return self.seconds / SECONDS_IN_DAY
    139
    140 -    def get_hours(self):
    141          return (self.seconds / (60 * 60)) % 24
    142
    143 -    def get_minutes(self):
    144          return (self.seconds / 60) % 60
    145
    146 -    def __repr__(self):
    147          return "{0}({1!r})".format(
    148              self.__class__.__name__,
    149              self.seconds
    150          )
    151