Gnumed.timelinelib.calendar.bosparanian.bosparanian

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

# Source Code for Module Gnumed.timelinelib.calendar.bosparanian.bosparanian

      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.bosparanian.time import BosparanianDelta
     20  from timelinelib.calendar.bosparanian.time import BosparanianTime
     21
     22
     23 -class BosparanianDateTime(object):
     24
     25 -    def __init__(self, year, month, day, hour, minute, second):
     26          if not is_valid(year, month, day):
     27              raise ValueError("Invalid bosparanian date %s-%s-%s" % (year, month, day))
     28          self.year = year
     29          self.month = month
     30          self.day = day
     31          self.hour = hour
     32          self.minute = minute
     33          self.second = second
     34
     35 -    def __eq__(self, other):
     36          return (isinstance(other, self.__class__) and
     37                  self.to_tuple() == other.to_tuple())
     38
     39 -    def __ne__(self, other):
     40          return not (self == other)
     41
     42      @classmethod
     43 -    def from_time(cls, time):
     44          (year, month, day) = bosparanian_day_to_ymd(time.julian_day)
     45          (hour, minute, second) = time.get_time_of_day()
     46          return BosparanianDateTime(year, month, day, hour, minute, second)
     47
     48      @classmethod
     49 -    def from_ymd(cls, year, month, day):
     50          return cls(year, month, day, 0, 0, 0)
     51
     52      @property
     53 -    def week_number(self):
     54          """
     55          returns number of week in year
     56          """
     57          def windsday_week_1(year):
     58              from timelinelib.calendar.bosparanian.timetype import BosparanianTimeType
     59              pra_4 = BosparanianDateTime.from_ymd(year, 1, 4).to_time()
     60              pra_4_day_of_week = BosparanianTimeType().get_day_of_week(pra_4)
     61              return pra_4 - BosparanianDelta.from_days(pra_4_day_of_week)
     62          def days_between(end, start):
     63              return end.julian_day - start.julian_day
     64          def days_since_windsday_week_1(time):
     65              year = BosparanianDateTime.from_time(time).year
     66              diff = days_between(end=time, start=windsday_week_1(year + 1))
     67              if diff >= 0:
     68                  return diff
     69              diff = days_between(end=time, start=windsday_week_1(year))
     70              if diff >= 0:
     71                  return diff
     72              diff = days_between(end=time, start=windsday_week_1(year - 1))
     73              if diff >= 0:
     74                  return diff
     75              raise ValueError("should not end up here")
     76          return days_since_windsday_week_1(self.to_time()) // 7 + 1
     77
     78 -    def days_in_month(self):
     79          return days_in_month(self.month)
     80
     81 -    def to_time(self):
     82          days = ymd_to_bosparanian_day(self.year, self.month, self.day)
     83          seconds = self.hour * 60 * 60 + self.minute * 60 + self.second
     84          return BosparanianTime(days, seconds)
     85
     86 -    def replace(self, year=None, month=None):
     87          if year is None:
     88              year = self.year
     89          if month is None:
     90              month = self.month
     91          return self.__class__(
     92              year,
     93              month,
     94              self.day,
     95              self.hour,
     96              self.minute,
     97              self.second
     98          )
     99
    100 -    def to_tuple(self):
    101          return (self.year, self.month, self.day, self.hour, self.minute,
    102                  self.second)
    103
    104 -    def to_date_tuple(self):
    105          return (self.year, self.month, self.day)
    106
    107 -    def to_time_tuple(self):
    108          return (self.hour, self.minute, self.second)
    109
    110 -    def is_first_day_in_year(self):
    111          return (self.month == 1 and
    112                  self.day == 1 and
    113                  self.hour == 0 and
    114                  self.minute == 0 and
    115                  self.second == 0)
    116
    117 -    def is_first_of_month(self):
    118          return (self.day == 1 and
    119                  self.hour == 0 and
    120                  self.minute == 0 and
    121                  self.second == 0)
    122
    123 -    def __repr__(self):
    124          return "BosparanianDateTime<%d-%02d-%02d %02d:%02d:%02d>" % self.to_tuple()
    125
    126
    127 -def ymd_to_bosparanian_day(year, month, day):
    128      """
    129      Converts a bosparanian date given as year, month, and day, to a day number counted from 1st PRA 0 BF
    130      """
    131      bosp_day = year * 365
    132      bosp_day += ((month - 1) // 13) * 365
    133      m = (month - 1) % 13
    134      bosp_day += m * 30
    135      bosp_day += day - 1
    136      bosparanian_day = bosp_day+(365*100*73)-3  # shift by 73 centuries and align week
    137      return bosparanian_day
    138
    139
    140 -def bosparanian_day_to_ymd(bosparanian_day):
    141      """
    142      Converts a day number, counted from 1st PRA, 0 BF to standard bosparanian calendar date
    143      """
    144      bosp_day = bosparanian_day-(365*100*73)+3  # shift by 73 centuries and align week
    145      year = bosp_day // 365
    146      d = bosp_day - (year * 365)
    147      if d >= 360:
    148          month = 13
    149          day = d-359
    150          return (year, month, day)
    151      month = d // 30 + 1
    152      day = d % 30 + 1
    153      return (year, month, day)
    154
    155
    156 -def is_valid(year, month, day):
    157      return (
    158          month >= 1 and month <= 13 and
    159          day >= 1 and day <= days_in_month(month)
    160      )
    161
    162
    163 -def is_valid_time(hour, minute, second):
    164      return (
    165          hour >= 0 and hour < 24 and
    166          minute >= 0 and minute < 60 and
    167          second >= 0 and second < 60
    168      )
    169
    170
    171 -def days_in_month(month):
    172      if month in [13]:
    173          return 5
    174      return 30
    175