def set_value(self, time): if time is None: time = self.now_fn() self.date_picker.SetGregorianDate(GregorianDateTime.from_time(time).to_date_tuple()) self.time_picker.SetGregorianTime(GregorianDateTime.from_time(time).to_time_tuple()) if self.on_change is not None: self.on_change()
def _convert_to_datetime(self, d): if isinstance(d, datetime): return GregorianDateTime(d.year, d.month, d.day, d.hour, d.minute, d.second).to_time() elif isinstance(d, date): return GregorianDateTime.from_ymd(d.year, d.month, d.day).to_time() else: raise TimelineIOError("Unknown date.")
def start(self, time): gregorian_time = GregorianDateTime.from_time(time) return GregorianDateTime.from_ymd( gregorian_time.year, gregorian_time.month, gregorian_time.day ).to_time()
def navigate(tp): start = GregorianDateTime.from_time(curret_period.start_time) end = GregorianDateTime.from_time(curret_period.end_time) start_months = start.year * 12 + start.month end_months = end.year * 12 + 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 > GregorianTimeType().get_max_time(): raise ValueError() if start < GregorianTimeType().get_min_time(): raise ValueError() except ValueError: if direction < 0: raise TimeOutOfRangeLeftError() else: raise TimeOutOfRangeRightError() return tp.update(start, end)
def time_to_wx_date(self, time): year, month, day = GregorianDateTime.from_time(time).to_date_tuple() try: return wx.DateTimeFromDMY(day, month - 1, year, 0, 0, 0) except OverflowError: if year < 0: year, month, day = GregorianDateTime.from_time(GregorianTime(0, 0)).to_date_tuple() return wx.DateTimeFromDMY(day, month - 1, year, 0, 0, 0)
def increment_day(self, date): year, month, day = date time = GregorianDateTime.from_ymd(year, month, day).to_time() if time < GregorianTimeType().get_max_time( ) - GregorianDelta.from_days(1): return GregorianDateTime.from_time( time + GregorianDelta.from_days(1)).to_date_tuple() return date
def _assert_period_is_valid(self, new_start, new_end): if new_start is None: raise ValueError(_("Invalid start time")) if new_end is None: raise ValueError(_("Invalid end time")) if new_start > new_end: raise ValueError(_("Start time can't be after end time. Start:%s End:%s" % (GregorianDateTime.from_time(new_start), GregorianDateTime.from_time(new_end))))
def fit_week_fn(main_frame, current_period, navigation_fn): mean = GregorianDateTime.from_time(current_period.mean_time()) start = GregorianDateTime.from_ymd(mean.year, mean.month, mean.day).to_time() weekday = GregorianTimeType().get_day_of_week(start) start = start - GregorianDelta.from_days(weekday) if not main_frame.week_starts_on_monday(): start = start - GregorianDelta.from_days(1) end = start + GregorianDelta.from_days(7) navigation_fn(lambda tp: tp.update(start, end))
def set_value(self, time): if time is None: time = self.now_fn() self.date_picker.SetGregorianDate( GregorianDateTime.from_time(time).to_date_tuple()) self.time_picker.SetGregorianTime( GregorianDateTime.from_time(time).to_time_tuple()) if self.on_change is not None: self.on_change()
def label(self, time, major=False): day_of_week = GregorianTimeType().get_day_of_week(time) if major: time = GregorianDateTime.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" % GregorianDateTime.from_time(time).day)
def fit_millennium_fn(main_frame, current_period, navigation_fn): mean = GregorianDateTime.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 = GregorianDateTime.from_ymd(year, 1, 1).to_time() end = GregorianDateTime.from_ymd(year + 1000, 1, 1).to_time() navigation_fn(lambda tp: tp.update(start, end))
def fit_millennium_fn(main_frame, current_period, navigation_fn): mean = GregorianDateTime.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 = GregorianDateTime.from_ymd(year, 1, 1).to_time() end = GregorianDateTime.from_ymd(year + 1000, 1, 1).to_time() navigation_fn(lambda tp: tp.update(start, end))
def time_to_wx_date(self, time): year, month, day = GregorianDateTime.from_time(time).to_date_tuple() try: return wx.DateTimeFromDMY(day, month - 1, year, 0, 0, 0) except OverflowError: if year < 0: year, month, day = GregorianDateTime.from_time( GregorianTime(0, 0)).to_date_tuple() return wx.DateTimeFromDMY(day, month - 1, year, 0, 0, 0)
def _assert_period_is_valid(self, new_start, new_end): if new_start is None: raise ValueError(_("Invalid start time")) if new_end is None: raise ValueError(_("Invalid end time")) if new_start > new_end: raise ValueError( _("Start time can't be after end time. Start:%s End:%s" % (GregorianDateTime.from_time(new_start), GregorianDateTime.from_time(new_end))))
def move_period_num_years(period, num): try: delta = num start_year = GregorianDateTime.from_time(period.start_time).year end_year = GregorianDateTime.from_time(period.end_time).year start_time = GregorianDateTime.from_time(period.start_time).replace(year=start_year + delta) end_time = GregorianDateTime.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 label(self, time, major=False): day_of_week = GregorianTimeType().get_day_of_week(time) if major: time = GregorianDateTime.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" % GregorianDateTime.from_time(time).day)
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 now(self): py = datetime.now() gregorian = GregorianDateTime( py.year, py.month, py.day, py.hour, py.minute, py.second ) return gregorian.to_time()
def _time_range_string(self, start, end): start = GregorianDateTime.from_time(start) end = GregorianDateTime.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 = GregorianDateTime.from_time( GregorianTimeType().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 label_without_time(time): gregorian_datetime = GregorianDateTime.from_time(time) return "%s %s %s" % ( gregorian_datetime.day, abbreviated_name_of_month(gregorian_datetime.month), format_year(gregorian_datetime.year) )
def human_time_to_gregorian(human_time): """ Create a :doc:`GregorianTime <timelinelib.calendar.gregorian.time>` object from a human readable date and time string. """ (year, month, day, hour, minute, seconds) = human_time_to_ymdhm(human_time) return GregorianDateTime(year, month, day, hour, minute, seconds).to_time()
def label_without_time(time): gregorian_datetime = GregorianDateTime.from_time(time) return u"%s %s %s" % ( gregorian_datetime.day, abbreviated_name_of_month(gregorian_datetime.month), format_year(gregorian_datetime.year) )
def label(self, time, major=False): time = GregorianDateTime.from_time(time) if major: return "%s %s %s" % (time.day, abbreviated_name_of_month(time.month), format_year(time.year)) return str(time.day)
def label(self, time, major=False): time = GregorianDateTime.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 > GregorianDateTime.from_time(GregorianTimeType().get_min_time()).year: return self._set_valid_day(year - 1, 12, day) return date
def decrement_month(self, date): year, month, day = date if month > 1: return self._set_valid_day(year, month - 1, day) elif year > GregorianDateTime.from_time( GregorianTimeType().get_min_time()).year: return self._set_valid_day(year - 1, 12, day) return date
def increment_month(self, date): max_year = GregorianDateTime.from_time(GregorianTimeType().get_max_time()).year year, month, day = date if month < 12: return self._set_valid_day(year, month + 1, day) elif year < max_year - 1: return self._set_valid_day(year + 1, 1, day) return date
def get_value(self): if self.time_picker.IsShown(): hour, minute, second = self.time_picker.GetGregorianTime() else: hour, minute, second = (0, 0, 0) year, month, day = self.date_picker.GetGregorianDate() return GregorianDateTime(year, month, day, hour, minute, second).to_time()
def _set_valid_day(self, new_year, new_month, new_day): done = False while not done: try: date = GregorianDateTime.from_ymd(new_year, new_month, new_day) done = True except Exception: new_day -= 1 return date.to_date_tuple()
def label(self, time, major=False): if major: gregorian_time = GregorianDateTime.from_time(time) return self._format_century( self._century_number( self._century_start_year(gregorian_time.year)), gregorian_time.is_bc()) else: return ""
def _time_range_string(self, start, end): start = GregorianDateTime.from_time(start) end = GregorianDateTime.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 navigate(tp): year_delta = direction * _calculate_year_diff(curret_period) gregorian_start = GregorianDateTime.from_time(curret_period.start_time) gregorian_end = GregorianDateTime.from_time(curret_period.end_time) new_start_year = gregorian_start.year + year_delta new_end_year = gregorian_end.year + year_delta try: new_start = gregorian_start.replace(year=new_start_year).to_time() new_end = gregorian_end.replace(year=new_end_year).to_time() if new_end > GregorianTimeType().get_max_time(): raise ValueError() if new_start < GregorianTimeType().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("1 Jan 2013", "1 Jan 2014")) True >>> _whole_number_of_years(gregorian_period("1 Jan 2013", "1 Jan 2015")) True >>> _whole_number_of_years(gregorian_period("1 Feb 2013", "1 Feb 2014")) False >>> _whole_number_of_years(gregorian_period("1 Jan 2013", "1 Feb 2014")) False """ return (GregorianDateTime.from_time(period.start_time).is_first_day_in_year() and GregorianDateTime.from_time(period.end_time).is_first_day_in_year() and _calculate_year_diff(period) > 0)
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()
def label(self, time, major=False): if major: gregorian_time = GregorianDateTime.from_time(time) return self._format_century( self._century_number( self._century_start_year(gregorian_time.year) ), gregorian_time.is_bc() ) else: return ""
def get_start_end(self): now = GregorianDateTime.from_time(self.db.time_type.now()) start = GregorianDateTime( now.year, now.month, 1, 0, 0, 0 ).to_time() end = start + self.get_delta(30) return (start, end)
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 - GregorianDelta.from_days(1) range_string = self._time_range_string(first_weekday, last_weekday) if self.appearance.get_week_start() == "monday": return (_("Week") + " %s (%s)") % (GregorianDateTime.from_time( time).week_number, range_string) else: # It is sunday (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("1 Jan 2013", "1 Jan 2014")) True >>> _whole_number_of_months(gregorian_period("1 Jan 2013", "1 Mar 2014")) 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 = GregorianDateTime.from_time(period.start_time), GregorianDateTime.from_time(period.end_time) start_months = start.year * 12 + start.month end_months = end.year * 12 + 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 _whole_number_of_months(period): """ >>> from timelinelib.test.utils import gregorian_period >>> _whole_number_of_months(gregorian_period("1 Jan 2013", "1 Jan 2014")) True >>> _whole_number_of_months(gregorian_period("1 Jan 2013", "1 Mar 2014")) 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 = GregorianDateTime.from_time( period.start_time), GregorianDateTime.from_time(period.end_time) start_months = start.year * 12 + start.month end_months = end.year * 12 + 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 navigate(tp): start = GregorianDateTime.from_time(curret_period.start_time) end = GregorianDateTime.from_time(curret_period.end_time) start_months = start.year * 12 + start.month end_months = end.year * 12 + 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 > GregorianTimeType().get_max_time(): raise ValueError() if start < GregorianTimeType().get_min_time(): raise ValueError() except ValueError: if direction < 0: raise TimeOutOfRangeLeftError() else: raise TimeOutOfRangeRightError() return tp.update(start, end)
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 GregorianDateTime(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 _event_from_path(db, file_path): stat = os.stat(file_path) # st_atime (time of most recent access), # st_mtime (time of most recent content modification), # st_ctime (platform dependent; time of most recent metadata change on # Unix, or the time of creation on Windows): dt = datetime.fromtimestamp(int(stat.st_mtime)) start_time = GregorianDateTime(dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second).to_time() end_time = start_time if start_time > end_time: start_time, end_time = end_time, start_time text = os.path.basename(file_path) category = _category_from_path(db, file_path) evt = Event().update(start_time, end_time, text, category) return evt
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 - GregorianDelta.from_days(1) range_string = self._time_range_string(first_weekday, last_weekday) if self.appearance.get_week_start() == "monday": return (_("Week") + " %s (%s)") % ( GregorianDateTime.from_time(time).week_number, range_string ) else: # It is sunday (don't know what to do about week numbers here) return range_string # This strip should never be used as minor return ""
def navigate_month_step(current_period, navigation_fn, direction): """ Currently does notice leap years. """ # TODO: NEW-TIME: (year, month, day, hour, minute, second) -> int (days in # month) tm = current_period.mean_time() gt = GregorianDateTime.from_time(tm) if direction > 0: if gt.month == 2: d = 28 elif gt.month in (4, 6, 9, 11): d = 30 else: d = 31 else: if gt.month == 3: d = 28 elif gt.month in (5, 7, 10, 12): d = 30 else: d = 31 mv = GregorianDelta.from_days(d) navigation_fn(lambda tp: tp.move_delta(direction * mv))
def increment(self, time): return time + GregorianDelta.from_days( GregorianDateTime.from_time(time).days_in_month() )
def increment(self, time): gregorian_time = GregorianDateTime.from_time(time) return gregorian_time.replace(year=gregorian_time.year + 1).to_time()
def start(self, time): gregorian_time = GregorianDateTime.from_time(time) new_gregorian = GregorianDateTime.from_ymd(gregorian_time.year, 1, 1) return new_gregorian.to_time()
def label(self, time, major=False): return format_year(GregorianDateTime.from_time(time).year)
def increment(self, time): gregorian_time = GregorianDateTime.from_time(time) return gregorian_time.replace( year=self._next_decacde_start_year(gregorian_time.year) ).to_time()