def set_value(self, time): if time is None: time = self.now_fn() self.date_picker.SetCopticDate(CopticDateTime.from_time(time).to_date_tuple()) self.time_picker.SetCopticTime(CopticDateTime.from_time(time).to_time_tuple()) if self.on_change is not None: self.on_change()
def navigate(tp): start = CopticDateTime.from_time(curret_period.start_time) end = CopticDateTime.from_time(curret_period.end_time) start_months = start.year * 13 + start.month end_months = end.year * 13 + end.month month_diff = end_months - start_months month_delta = month_diff * direction new_start_year, new_start_month = _months_to_year_and_month( start_months + month_delta) new_end_year, new_end_month = _months_to_year_and_month(end_months + month_delta) try: new_start = start.replace(year=new_start_year, month=new_start_month) new_end = end.replace(year=new_end_year, month=new_end_month) start = new_start.to_time() end = new_end.to_time() if end > CopticTimeType().get_max_time(): raise ValueError() if start < CopticTimeType().get_min_time(): raise ValueError() except ValueError: if direction < 0: raise TimeOutOfRangeLeftError() else: raise TimeOutOfRangeRightError() return tp.update(start, end)
def increment_day(self, date): year, month, day = date time = CopticDateTime.from_ymd(year, month, day).to_time() if time < CopticTimeType().get_max_time() - CopticDelta.from_days(1): return CopticDateTime.from_time( time + CopticDelta.from_days(1)).to_date_tuple() return date
def now(self): py = datetime.now() julian_day = gregorian_ymd_to_julian_day(py.year, py.month, py.day) year, month, day = julian_day_to_coptic_ymd(julian_day) coptic = CopticDateTime(year, month, day, py.hour, py.minute, py.second) return coptic.to_time()
def time_to_wx_date(self, time): year, month, day = CopticDateTime.from_time(time).to_date_tuple() try: return wx.DateTime.FromDMY(day, month - 1, year, 0, 0, 0) except OverflowError: if year < 0: year, month, day = CopticDateTime.from_time(CopticTime(0, 0)).to_date_tuple() return wx.DateTime.FromDMY(day, month - 1, year, 0, 0, 0)
def label(self, time, major=False): day_of_week = CopticTimeType().get_day_of_week(time) if major: time = CopticDateTime.from_time(time) return "%s %s %s %s" % ( abbreviated_name_of_weekday(day_of_week), time.day, abbreviated_name_of_month(time.month), format_year(time.year)) return (abbreviated_name_of_weekday(day_of_week) + " %s" % CopticDateTime.from_time(time).day)
def fit_week_fn(main_frame, current_period, navigation_fn): mean = CopticDateTime.from_time(current_period.mean_time()) start = CopticDateTime.from_ymd(mean.year, mean.month, mean.day).to_time() weekday = CopticTimeType().get_day_of_week(start) start = start - CopticDelta.from_days(weekday) if not main_frame.week_starts_on_monday(): start = start - CopticDelta.from_days(1) end = start + CopticDelta.from_days(7) navigation_fn(lambda tp: tp.update(start, end))
def fit_millennium_fn(main_frame, current_period, navigation_fn): mean = CopticDateTime.from_time(current_period.mean_time()) if mean.year > get_millenium_max_year(): year = get_millenium_max_year() else: year = max(get_min_year_containing_jan_1(), int(mean.year // 1000) * 1000) start = CopticDateTime.from_ymd(year, 1, 1).to_time() end = CopticDateTime.from_ymd(year + 1000, 1, 1).to_time() navigation_fn(lambda tp: tp.update(start, end))
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( CopticDateTime(year, month, day, 0, 0, 0).to_time(), CopticDateTime(end_year, end_month, end_day, 0, 0, 0).to_time())
def move_period_num_years(period, num): try: delta = num start_year = CopticDateTime.from_time(period.start_time).year end_year = CopticDateTime.from_time(period.end_time).year start_time = CopticDateTime.from_time( period.start_time).replace(year=start_year + delta) end_time = CopticDateTime.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 _time_range_string(self, start, end): start = CopticDateTime.from_time(start) end = CopticDateTime.from_time(end) if start.year == end.year: if start.month == end.month: return "%s-%s %s %s" % (start.day, end.day, abbreviated_name_of_month(start.month), format_year(start.year)) return "%s %s-%s %s %s" % ( start.day, abbreviated_name_of_month(start.month), end.day, abbreviated_name_of_month(end.month), format_year(start.year)) return "%s %s %s-%s %s %s" % ( start.day, abbreviated_name_of_month( start.month), format_year(start.year), end.day, abbreviated_name_of_month(end.month), format_year(end.year))
def increment_year(self, date): max_year = CopticDateTime.from_time( CopticTimeType().get_max_time()).year year, month, day = date if year < max_year - 1: return self._set_valid_day(year + 1, month, day) return date
def human_time_to_coptic(human_time): """ Create a :doc:`CopticTime <timelinelib.calendar.coptic.time>` object from a human readable date and time string. """ (year, month, day, hour, minute, seconds) = human_time_to_ymdhm(human_time) return CopticDateTime(year, month, day, hour, minute, seconds).to_time()
def get_value(self): if self.time_picker.IsShown(): hour, minute, second = self.time_picker.GetCopticTime() else: hour, minute, second = (0, 0, 0) year, month, day = self.date_picker.GetCopticDate() return CopticDateTime(year, month, day, hour, minute, second).to_time()
def label(self, time, major=False): time = CopticDateTime.from_time(time) if major: return "%s %s %s: %s:%s" % ( time.day, abbreviated_name_of_month(time.month), format_year(time.year), time.hour, time.minute) return str(time.minute)
def decrement_month(self, date): year, month, day = date if month > 1: return self._set_valid_day(year, month - 1, day) elif year > CopticDateTime.from_time( CopticTimeType().get_min_time()).year: return self._set_valid_day(year - 1, 13, day) return date
def label(self, time, major=False): if major: coptic_time = CopticDateTime.from_time(time) return self._format_century( self._century_number(self._century_start_year( coptic_time.year)), coptic_time.is_bc()) else: return ""
def _set_valid_day(self, new_year, new_month, new_day): done = False while not done: try: date = CopticDateTime.from_ymd(new_year, new_month, new_day) done = True except Exception: new_day -= 1 return date.to_date_tuple()
def navigate(tp): year_delta = direction * _calculate_year_diff(curret_period) coptic_start = CopticDateTime.from_time(curret_period.start_time) coptic_end = CopticDateTime.from_time(curret_period.end_time) new_start_year = coptic_start.year + year_delta new_end_year = coptic_end.year + year_delta try: new_start = coptic_start.replace(year=new_start_year).to_time() new_end = coptic_end.replace(year=new_end_year).to_time() if new_end > CopticTimeType().get_max_time(): raise ValueError() if new_start < CopticTimeType().get_min_time(): raise ValueError() except ValueError: if direction < 0: raise TimeOutOfRangeLeftError() else: raise TimeOutOfRangeRightError() return tp.update(new_start, new_end)
def _whole_number_of_years(period): """ >>> from timelinelib.test.utils import gregorian_period >>> _whole_number_of_years(gregorian_period("11 Sep 2013", "11 Sep 2014")) True >>> _whole_number_of_years(gregorian_period("9 Sep 1776", "9 Sep 1777")) True >>> _whole_number_of_years(gregorian_period("8 Dec 1776", "8 Dec 1777")) False >>> _whole_number_of_years(gregorian_period("6 Sep 2013", "11 Sep 2014")) False """ return (CopticDateTime.from_time(period.start_time).is_first_day_in_year() and CopticDateTime.from_time( period.end_time).is_first_day_in_year() and _calculate_year_diff(period) > 0)
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()
def navigate_month_step(current_period, navigation_fn, direction): from timelinelib.calendar.coptic.coptic import is_leap_year # TODO: NEW-TIME: (year, month, day, hour, minute, second) -> int (days in # month) tm = current_period.mean_time() gt = CopticDateTime.from_time(tm) if gt.month == 13: if is_leap_year(gt.year): d = 6 d = 5 d = 30 mv = CopticDelta.from_days(d) navigation_fn(lambda tp: tp.move_delta(direction * mv))
def label(self, time, major=False): if major: first_weekday = self.start(time) next_first_weekday = self.increment(first_weekday) last_weekday = next_first_weekday - CopticDelta.from_days(1) range_string = self._time_range_string(first_weekday, last_weekday) if self.appearance.get_week_start() == "tkyriaka": return (_("Week") + " %s (%s)") % ( CopticDateTime.from_time(time).week_number, range_string) else: # It is Psabbaton (don't know what to do about week numbers here) return range_string # This strip should never be used as minor return ""
def _whole_number_of_months(period): """ >>> from timelinelib.test.utils import gregorian_period >>> _whole_number_of_months(gregorian_period("9 Jan 2013", "9 Jan 2014")) True >>> _whole_number_of_months(gregorian_period("6 Jul 1776", "6 Jul 1777")) True >>> _whole_number_of_months(gregorian_period("2 Jan 2013", "2 Mar 2014")) False >>> _whole_number_of_months(gregorian_period("1 Jan 2013 12:00", "1 Mar 2014")) False """ start, end = CopticDateTime.from_time( period.start_time), CopticDateTime.from_time(period.end_time) start_months = start.year * 13 + start.month end_months = end.year * 13 + end.month month_diff = end_months - start_months return (start.is_first_of_month() and end.is_first_of_month() and month_diff > 0)
def parse_time(self, time_string): match = re.search(r"^(-?\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)$", time_string) if match: year = int(match.group(1)) month = int(match.group(2)) day = int(match.group(3)) hour = int(match.group(4)) minute = int(match.group(5)) second = int(match.group(6)) try: return CopticDateTime(year, month, day, hour, minute, second).to_time() except ValueError: raise ValueError("Invalid time, time string = '%s'" % time_string) else: raise ValueError("Time not on correct format = '%s'" % time_string)
def increment(self, time): return time + CopticDelta.from_days( CopticDateTime.from_time(time).days_in_month())
def start(self, time): coptic_time = CopticDateTime.from_time(time) new_coptic = CopticDateTime.from_ymd(coptic_time.year, coptic_time.month, coptic_time.day) return new_coptic.to_time()
def _format_sample_date(self, date_formatter): return date_formatter.format( CopticDateTime.from_time( CopticTimeType().now()).to_date_tuple())[0]
def label_without_time(time): coptic_datetime = CopticDateTime.from_time(time) return "%s %s %s" % (coptic_datetime.day, abbreviated_name_of_month( coptic_datetime.month), format_year(coptic_datetime.year))
def start(self, time): coptic_time = CopticDateTime.from_time(time) return CopticDateTime.from_ymd(coptic_time.year, coptic_time.month, 1).to_time()