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 seconds_in_day = int(self.julian_day + seconds // SECONDS_IN_DAY) 66 return self.__class__(seconds_in_day, seconds % SECONDS_IN_DAY) 67 raise TypeError( 68 "%s + %s not supported" % (self.__class__.__name__, type(delta)) 69 )
70
71 - def __sub__(self, other):
72 if isinstance(other, self.DeltaClass): 73 seconds = self.seconds - other.seconds 74 if seconds < 0: 75 if seconds % SECONDS_IN_DAY == 0: 76 days = abs(seconds) // SECONDS_IN_DAY 77 seconds = 0 78 else: 79 days = abs(seconds) // SECONDS_IN_DAY + 1 80 seconds = SECONDS_IN_DAY - abs(seconds) % SECONDS_IN_DAY 81 return self.__class__(self.julian_day - days, seconds) 82 else: 83 return self.__class__(self.julian_day, seconds) 84 else: 85 days_diff = self.julian_day - other.julian_day 86 seconds_diff = self.seconds - other.seconds 87 return self.DeltaClass(days_diff * SECONDS_IN_DAY + seconds_diff)
88
89 - def __gt__(self, dt):
90 return (self.julian_day, self.seconds) > (dt.julian_day, dt.seconds)
91
92 - def __ge__(self, dt):
93 return self == dt or self > dt
94
95 - def __lt__(self, dt):
96 return (self.julian_day, self.seconds) < (dt.julian_day, dt.seconds)
97
98 - def __repr__(self):
99 return "{0}({1!r}, {2!r})".format( 100 self.__class__.__name__, 101 self.julian_day, 102 self.seconds 103 )
104
105 - def to_str(self):
108
109 - def get_time_of_day(self):
110 hours = self.seconds // 3600 111 minutes = (self.seconds // 60) % 60 112 seconds = self.seconds % 60 113 return (hours, minutes, seconds)
114
115 116 -class GregorianDelta(ComparableValue, GenericDeltaMixin):
117 118 @classmethod
119 - def from_seconds(cls, seconds):
120 return cls(seconds)
121 122 @classmethod
123 - def from_days(cls, days):
124 return cls(SECONDS_IN_DAY * days)
125 126 @property
127 - def seconds(self):
128 return self.value
129
130 - def __truediv__(self, value):
131 if isinstance(value, self.__class__): 132 return self.seconds / value.seconds 133 else: 134 return self.__class__(int(self.seconds // value))
135
136 - def __sub__(self, delta):
137 return self.__class__(self.seconds - delta.seconds)
138
139 - def __mul__(self, value):
140 return self.__class__(int(self.seconds * value))
141
142 - def get_days(self):
143 return self.seconds // SECONDS_IN_DAY
144
145 - def get_hours(self):
146 return (self.seconds // (60 * 60)) % 24
147
148 - def get_minutes(self):
149 return (self.seconds // 60) % 60
150
151 - def __repr__(self):
152 return "{0}({1!r})".format( 153 self.__class__.__name__, 154 self.seconds 155 )
156