def _get_period(self, start, end): if start is None and end is None: time = self.time_type.now() return TimePeriod(time, time) elif end is None: return TimePeriod(start, start) else: return TimePeriod(start, end)
def move_period_num_months(period, num): try: delta = num years = abs(delta) / 13 bosparanian_start = BosparanianDateTime.from_time(period.start_time) bosparanian_end = BosparanianDateTime.from_time(period.end_time) if num < 0: years = -years delta = delta - 13 * years if delta < 0: start_month = bosparanian_start.month + 13 + delta end_month = bosparanian_end.month + 13 + delta if start_month > 13: start_month -= 13 end_month -= 13 if start_month > bosparanian_start.month: years -= 1 else: start_month = bosparanian_start.month + delta end_month = bosparanian_start.month + delta if start_month > 13: start_month -= 13 end_month -= 13 years += 1 start_year = bosparanian_start.year + years end_year = bosparanian_start.year + years start_time = bosparanian_start.replace(year=start_year, month=start_month) end_time = bosparanian_end.replace(year=end_year, month=end_month) return TimePeriod(start_time.to_time(), end_time.to_time()) except ValueError: return None
def coptic_period(human_start_time, human_end_time): """ Create a coptic TimePeriod object. The start and end times are strings in a human readable format. """ return TimePeriod(human_time_to_coptic(human_start_time), human_time_to_coptic(human_end_time))
def gregorian_period(human_start_time, human_end_time): """ Create a gregorian TimePeriod object. The start and end times are strings in a human readable format. """ return TimePeriod(human_time_to_gregorian(human_start_time), human_time_to_gregorian(human_end_time))
def choose_strip(self, metrics, appearance): """ Return a tuple (major_strip, minor_strip) for current time period and window size. """ day_period = TimePeriod(BosparanianTime(0, 0), BosparanianTime(1, 0)) one_day_width = metrics.calc_exact_width(day_period) self.major_strip_is_decade = False if one_day_width > 20000: return (StripHour(), StripMinute()) elif one_day_width > 600: return (StripDay(), StripHour()) elif one_day_width > 60: return (StripMonth(), StripWeekday()) elif one_day_width > 25: return (StripMonth(), StripDay()) elif one_day_width > 10: return (StripMonth(), StripWeek()) elif one_day_width > 1.75: return (StripYear(), StripMonth()) elif one_day_width > 0.5: return (StripYear(), StripQuarter()) elif one_day_width > 0.12: self.major_strip_is_decade = True return (StripDecade(), StripYear()) elif one_day_width > 0.012: return (StripCentury(), StripDecade()) else: return (StripCentury(), StripCentury())
def _set_milestone_time_period(self): try: t = self.view.GetTime() if t is None: raise DateTimeError() self._milestone.set_time_period(TimePeriod(t, t)) except: display_error_message(_("You can't save a milestone with an invalid date/time")) raise(DateTimeError())
def move_period_num_years(period, num): try: delta = num start_year = PharaonicDateTime.from_time(period.start_time).year end_year = PharaonicDateTime.from_time(period.end_time).year start_time = PharaonicDateTime.from_time(period.start_time).replace(year=start_year + delta) end_time = PharaonicDateTime.from_time(period.end_time).replace(year=end_year + delta) return TimePeriod(start_time.to_time(), end_time.to_time()) except ValueError: return None
def a_time_period(): """Create a random :doc:`TimePeriod <timelinelib_canvas_data_timeperiod>` object.""" year = random.randint(1, 4000) month = random.randint(1, 12) day = random.randint(1, 28) end_year = year + random.randint(1, 5) end_month = random.randint(1, 12) end_day = random.randint(1, 28) return TimePeriod(GregorianDateTime(year, month, day, 0, 0, 0).to_time(), GregorianDateTime(end_year, end_month, end_day, 0, 0, 0).to_time())
def _get_period(self, t1, t2): if t1 > t2: start = t2 end = t1 else: start = t1 end = t2 return TimePeriod( self.timeline_canvas.Snap(start), self.timeline_canvas.Snap(end))
def fill(strip_list, strip): """Fill the given list with the given strip.""" try: current_start = strip.start(self._view_properties.displayed_period.start_time) while current_start < self._view_properties.displayed_period.end_time: next_start = strip.increment(current_start) strip_list.append(TimePeriod(current_start, next_start)) current_start = next_start except: # Exception occurs when major=century and when we are at the end of the calendar pass
def _get_period_from_view(self): try: period = self.view.GetPeriod() except ValueError as ex: self.view.DisplayInvalidPeriod("%s" % ex_msg(ex)) raise ValueError() (start, end) = (period.get_start_time(), period.get_end_time()) if self.event is not None and self.view.GetLocked(): self._verify_that_time_has_not_been_changed(start, end) end = self._adjust_end_if_ends_today(start, end) TimePeriod(start, end) return (start, end)
def _period_for_all_visible_events(self): try: visible_events = self._all_visible_events() if len(visible_events) > 0: start = self._first_time(visible_events) end = self._last_time(visible_events) return TimePeriod(start, end).zoom(-1) else: return None except ValueError as ex: display_error_message(str(ex)) return None
def _create_wrapper(self, wrapper_class, **kwargs): wrapper = wrapper_class(db=self) if hasattr(wrapper, "time_period") and "time_period" not in kwargs: now = self.time_type.now() kwargs["time_period"] = TimePeriod(now, now) if "container" in kwargs: container = kwargs.pop("container") else: container = None for name, value in kwargs.items(): setattr(wrapper, name, value) if container is not None: wrapper.container = container return wrapper
def get_export_periods(self, first_time, last_time): periods = [] current_period = None if self.main_frame.timeline: time_type = self.main_frame.timeline.get_time_type() current_period = self.get_view_properties().displayed_period period_delta = current_period.end_time - current_period.start_time periods.append(current_period) start_time = current_period.start_time period = current_period while first_time < start_time: start_time = period.start_time - period_delta end_time = period.start_time period = TimePeriod(start_time, end_time) periods.insert(0, period) end_time = current_period.end_time period = current_period while last_time > end_time: start_time = period.end_time end_time = period.end_time + period_delta period = TimePeriod(start_time, end_time) periods.append(period) return periods, current_period
def move_period_num_months(period, num): def move_time(time): coptic_time = CopticDateTime.from_time(time) new_month = coptic_time.month + num new_year = coptic_time.year while new_month < 1: new_month += 13 new_year -= 1 while new_month > 13: new_month -= 13 new_year += 1 return coptic_time.replace(year=new_year, month=new_month).to_time() try: return TimePeriod(move_time(period.start_time), move_time(period.end_time)) except ValueError: return None
def choose_strip(self, metrics, appearance): # Choose an exponent that will make the minor strip just larger than # the displayed period: # # 10**x > period_delta => # x > log(period_delta) exponent = int(math.log(metrics.time_period.delta().value, 10)) + 1 # Keep decreasing the exponent until the minor strip is small enough. while True: if exponent == 0: break next_minor_strip_with_px = metrics.calc_exact_width( TimePeriod(NumTime(0), NumTime(10**(exponent - 1)))) if next_minor_strip_with_px > 30: exponent -= 1 else: break return (NumStrip(10**(exponent + 1)), NumStrip(10**exponent))
def move_period_num_months(period, num): def move_time(time): gregorian_time = GregorianDateTime.from_time(time) new_month = gregorian_time.month + num new_year = gregorian_time.year while new_month < 1: new_month += 12 new_year -= 1 while new_month > 12: new_month -= 12 new_year += 1 return gregorian_time.replace(year=new_year, month=new_month).to_time() try: return TimePeriod(move_time(period.start_time), move_time(period.end_time)) except ValueError: return None
def add_subevent(self, container, text, description, start_add, end_add=None, hyperlink=None): start, end = self._calc_start_end(start_add, end_add) event = self.db.new_subevent(container=container, time_period=TimePeriod(start, end)).update( start, end, text, self.last_cat) if description: event.set_data("description", description) if hyperlink: event.set_hyperlink(hyperlink) event.save()
def choose_strip(self, metrics, appearance): """ Return a tuple (major_strip, minor_strip) for current time period and window size. """ day_period = TimePeriod(GregorianTime(0, 0), GregorianTime(1, 0)) one_day_width = metrics.calc_exact_width(day_period) if one_day_width > 20000: return (StripHour(), StripMinute()) elif one_day_width > 600: return (StripDay(), StripHour()) elif one_day_width > 45: return (StripWeek(appearance), StripWeekday()) elif one_day_width > 25: return (StripMonth(), StripDay()) elif one_day_width > 1.5: return (StripYear(), StripMonth()) elif one_day_width > 0.12: return (StripDecade(), StripYear()) elif one_day_width > 0.012: return (StripCentury(), StripDecade()) else: return (StripCentury(), StripCentury())
def move_period_num_weeks(period, num): delta = GregorianDelta.from_days(7) * num start_time = period.start_time + delta end_time = period.end_time + delta return TimePeriod(start_time, end_time)
def DragPeriodSelect(self, evt): if self._period_select: self._end_time = self.GetTimeAt(evt.GetX()) self.SetPeriodSelection(TimePeriod(self._start_time, self._end_time))
def DragZoom(self, evt): if self._zooming: self._end_time = self.GetTimeAt(evt.GetX()) self.SetPeriodSelection(TimePeriod(self._start_time, self._end_time))
def format_current_pos_time_string(x): tm = self.GetTimeAt(x) return self.GetTimeType().format_period(TimePeriod(tm, tm))
def get_value(self): return TimePeriod(self._get_start(), self._get_end())
def numeric_period(start, end): """ Create a numeric TimePeriod object. The start and end are numeric values. """ return TimePeriod(start, end)
def move_period_num_days(period, num): delta = PharaonicDelta.from_days(1) * num start_time = period.start_time + delta end_time = period.end_time + delta return TimePeriod(start_time, end_time)
def move_period(period, num): delta = NumDelta(num) start_time = period.start_time + delta end_time = period.end_time + delta return TimePeriod(start_time, end_time)
def _parse_displayed_period(self, text, tmp_dict): start = self._parse_time(tmp_dict.pop("tmp_start")) end = self._parse_time(tmp_dict.pop("tmp_end")) self.db.set_displayed_period(TimePeriod(start, end))
def __init__(self): self.db = MemoryDB() self.db.time_type = self.get_time_type() self.start, self.end = self.get_start_end() self.db.set_displayed_period(TimePeriod(self.start, self.end)) self.last_cat = None