Package Gnumed :: Package timelinelib :: Package calendar :: Package coptic :: Module coptic
[frames] | no frames]

Source Code for Module Gnumed.timelinelib.calendar.coptic.coptic

  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.coptic.time import CopticDelta 
 20  from timelinelib.calendar.coptic.time import CopticTime 
 21   
 22   
 23  FIRST_DAY = 1825030        
24 25 26 -class CopticDateTime(object):
27
28 - def __init__(self, year, month, day, hour, minute, second):
29 if not is_valid(year, month, day): 30 raise ValueError("Invalid coptic date %s-%s-%s" % (year, month, day)) 31 self.year = year 32 self.month = month 33 self.day = day 34 self.hour = hour 35 self.minute = minute 36 self.second = second
37
38 - def __eq__(self, other):
39 return (isinstance(other, self.__class__) and 40 self.to_tuple() == other.to_tuple())
41
42 - def __ne__(self, other):
43 return not (self == other)
44 45 @classmethod
46 - def from_ymd(cls, year, month, day):
47 return cls(year, month, day, 0, 0, 0)
48 49 @classmethod
50 - def from_time(cls, time):
51 (year, month, day) = julian_day_to_coptic_ymd(time.julian_day) 52 (hour, minute, second) = time.get_time_of_day() 53 return cls(year, month, day, hour, minute, second)
54 55 @property
56 - def week_number(self):
57 def tkyriaka_week_1(year): 58 from timelinelib.calendar.coptic.timetype import CopticTimeType 59 thoth_4 = CopticDateTime.from_ymd(year, 1, 4).to_time() 60 thoth_4_day_of_week = CopticTimeType().get_day_of_week(thoth_4) 61 return thoth_4 - CopticDelta.from_days(thoth_4_day_of_week)
62 63 def days_between(end, start): 64 return end.julian_day - start.julian_day
65 66 def days_since_tkyriaka_week_1(time): 67 year = CopticDateTime.from_time(time).year 68 diff = days_between(end=time, start=tkyriaka_week_1(year + 1)) 69 if diff >= 0: 70 return diff 71 diff = days_between(end=time, start=tkyriaka_week_1(year)) 72 if diff >= 0: 73 return diff 74 diff = days_between(end=time, start=tkyriaka_week_1(year - 1)) 75 if diff >= 0: 76 return diff 77 raise ValueError("should not end up here") 78 return days_since_monday_week_1(self.to_time()) // 7 + 1 79 """ 80 if self.month == 13: 81 return 0 82 83 length_of_week = 10 84 days_into_year = ((self.month - 1)*30) + self.day 85 86 if self.day % length_of_week > 0: 87 return (days_into_year / length_of_week) + 1 88 89 return days_into_year / length_of_week 90 """ 91
92 - def is_bc(self):
93 return self.year <= 0
94
95 - def replace(self, year=None, month=None):
96 if year is None: 97 year = self.year 98 if month is None: 99 month = self.month 100 return self.__class__( 101 year, 102 month, 103 self.day, 104 self.hour, 105 self.minute, 106 self.second 107 )
108
109 - def days_in_month(self):
110 return days_in_month(self.year, self.month)
111
112 - def to_tuple(self):
113 return (self.year, self.month, self.day, self.hour, self.minute, 114 self.second)
115
116 - def to_date_tuple(self):
117 return (self.year, self.month, self.day)
118
119 - def to_time_tuple(self):
120 return (self.hour, self.minute, self.second)
121
122 - def to_time(self):
123 days = coptic_ymd_to_julian_day(self.year, self.month, self.day) 124 seconds = self.hour * 60 * 60 + self.minute * 60 + self.second 125 return CopticTime(days, seconds)
126
127 - def is_first_day_in_year(self):
128 return (self.month == 1 and 129 self.day == 1 and 130 self.hour == 0 and 131 self.minute == 0 and 132 self.second == 0)
133
134 - def is_first_of_month(self):
135 return (self.day == 1 and 136 self.hour == 0 and 137 self.minute == 0 and 138 self.second == 0)
139
140 - def __repr__(self):
141 return "CopticDateTime<%d-%02d-%02d %02d:%02d:%02d>" % self.to_tuple()
142
143 -def days_in_month(year, month):
144 if month <= 12: 145 return 30 146 elif is_leap_year(year): 147 return 6 148 else: 149 return 5
150
151 -def is_leap_year(year):
152 if year > 0 and (year+1) % 4 == 0: 153 return True 154 elif year < 0 and year % 4 == 0: 155 return True 156 else: 157 return False
158
159 -def num_leap_years(year):
160 return int(abs(year/4))
161
162 -def is_valid_time(hour, minute, second):
163 return ( 164 hour >= 0 and hour < 24 and 165 minute >= 0 and minute < 60 and 166 second >= 0 and second < 60 167 )
168
169 170 -def is_valid(year, month, day):
171 return month >= 1 and month <= 13 and day >= 1 and day <= days_in_month(year, month)
172
173 174 -def julian_day_to_coptic_ymd(julian_day):
175 """ 176 This calendar calculation was originally published in Explanatory Supplement to the Astronomical Almanac, S.E. Urban and P.K. Seidelman, Eds. (2012). You can purchase the book at uscibooks.com/urban.htm. "15.11 Calendar Conversion Algorithms" from the following pdf is used in the below code. https://aa.usno.navy.mil/publications/docs/c15_usb_online.pdf 177 """ 178 if julian_day < CopticTime.MIN_JULIAN_DAY: 179 raise ValueError("coptic_day_to_gregorian_ymd only works for julian days >= %d, but was %d" % (CopticTime.MIN_JULIAN_DAY, julian_day)) 180 #year_inx = int((julian_day - FIRST_DAY) / 365) 181 #days_inx = (julian_day - FIRST_DAY) - year_inx * 365 182 #month_inx = days_inx / 30 183 #day_inx = days_inx - month_inx * 30 184 185 y = 4996 186 j = 124 187 m = 0 188 n = 13 189 r = 4 190 p = 1461 191 q = 0 192 v = 3 193 u = 1 194 s = 30 195 t = 0 196 w = 0 197 198 f = julian_day + j 199 e = r * f + v 200 g = (e % p)//r 201 h = u * g + w 202 day = ((h % s)/u) + 1 203 month = (((h // s) + m) % n) + 1 204 year = (e // p) - y + (n + m - month)//n 205 return (year, month, day)
206 #return (year_inx + 1, month_inx + 1, day_inx + 1)
207 208 209 -def coptic_ymd_to_julian_day(year, month, day):
210 """ 211 Coptic year 1 = Julian day 1825030 212 """ 213 214 215 #julian_day = FIRST_DAY + (year - 1) * 365 + (month - 1) * 30 + (day - 1) 216 #num_leap_days = num_leap_years(year) 217 #julian_day = FIRST_DAY + (year - 1 - num_leap_days)*365 + num_leap_days*366 + (month - 1)*30 + (day - 1) 218 219 y = 4996 220 j = 124 221 m = 0 222 n = 13 223 r = 4 224 p = 1461 225 q = 0 226 v = 3 227 u = 1 228 s = 30 229 t = 0 230 w = 0 231 232 h = month - m 233 g = year + y - (n-h)//n 234 f = (h - 1 + n) % n 235 e = (p * g + q)//r + day - 1 -j 236 julian_day = e + (s * f + t)//u 237 238 if julian_day < CopticTime.MIN_JULIAN_DAY: 239 raise ValueError("coptic_ymd_to_julian_day only works for julian days >= %d, but was %d" % (CopticTime.MIN_JULIAN_DAY, julian_day)) 240 return julian_day
241