def coerce_cycle_point_format(cls, value, keys): """Coerce to a cycle point format (either CCYYMM... or %Y%m...).""" value = cls.strip_and_unquote(keys, value) if not value: return None test_timepoint = TimePoint(year=2001, month_of_year=3, day_of_month=1, hour_of_day=4, minute_of_hour=30, second_of_minute=54) if '/' in value: raise IllegalValueError('cycle point format', keys, value) if '%' in value: try: TimePointDumper().strftime(test_timepoint, value) except ValueError: raise IllegalValueError('cycle point format', keys, value) return value if 'X' in value: for i in range(1, 101): dumper = TimePointDumper(num_expanded_year_digits=i) try: dumper.dump(test_timepoint, value) except ValueError: continue return value raise IllegalValueError('cycle point format', keys, value) dumper = TimePointDumper() try: dumper.dump(test_timepoint, value) except ValueError: raise IllegalValueError('cycle point format', keys, value) return value
def _coerce_cycletime_time_zone(value, keys, _): """Coerce value to a cycle point time zone format - Z, +13, -0800...""" value = _strip_and_unquote(keys, value) if not value: return None try: set_syntax_version(VERSION_NEW, "use of [cylc]cycle point time zone format") except SyntaxVersionError: raise IllegalValueError("cycle point time zone format", keys, value) test_timepoint = TimePoint(year=2001, month_of_year=3, day_of_month=1, hour_of_day=4, minute_of_hour=30, second_of_minute=54) dumper = TimePointDumper() test_timepoint_string = dumper.dump(test_timepoint, "CCYYMMDDThhmmss") test_timepoint_string += value parser = TimePointParser(allow_only_basic=True) try: parser.parse(test_timepoint_string) except ValueError: raise IllegalValueError("cycle point time zone format", keys, value) return value
def coerce_cycle_point_time_zone(cls, value, keys): """Coerce value to a cycle point time zone format - Z, +13, -0800...""" value = cls.strip_and_unquote(keys, value) if not value: return None test_timepoint = TimePoint(year=2001, month_of_year=3, day_of_month=1, hour_of_day=4, minute_of_hour=30, second_of_minute=54) dumper = TimePointDumper() test_timepoint_string = dumper.dump(test_timepoint, 'CCYYMMDDThhmmss') test_timepoint_string += value parser = TimePointParser(allow_only_basic=True) try: parser.parse(test_timepoint_string) except ValueError: raise IllegalValueError( 'cycle point time zone format', keys, value) return value
def _coerce_cycletime_format(value, keys, _): """Coerce value to a cycle point format (either CCYYMM... or %Y%m...).""" value = _strip_and_unquote(keys, value) if not value: return None try: set_syntax_version(VERSION_NEW, "use of [cylc]cycle point format") except SyntaxVersionError: raise IllegalValueError("cycle point format", keys, value) test_timepoint = TimePoint(year=2001, month_of_year=3, day_of_month=1, hour_of_day=4, minute_of_hour=30, second_of_minute=54) if "/" in value: raise IllegalValueError("cycle point format", keys, value) if "%" in value: try: TimePointDumper().strftime(test_timepoint, value) except ValueError: raise IllegalValueError("cycle point format", keys, value) return value if "X" in value: for i in range(1, 101): dumper = TimePointDumper(num_expanded_year_digits=i) try: dumper.dump(test_timepoint, value) except ValueError: continue return value raise IllegalValueError("cycle point format", keys, value) dumper = TimePointDumper() try: dumper.dump(test_timepoint, value) except ValueError: raise IllegalValueError("cycle point format", keys, value) return value
def _do_test_dates(self, my_date): """Performs a series of tests against a given date. This method does some time consuming operations, which are not IO bound, so this method is a good candidate to be run concurrently. :param my_date: a date to be tested :type my_date: datetime.datetime """ ctrl_data = my_date.isocalendar() test_date = TimePoint(year=my_date.year, month_of_year=my_date.month, day_of_month=my_date.day) test_week_date = test_date.to_week_date() test_data = test_week_date.get_week_date() self.assertEqual(test_data, ctrl_data) ctrl_data = (my_date.year, my_date.month, my_date.day) test_data = test_week_date.get_calendar_date() self.assertEqual(test_data, ctrl_data) ctrl_data = my_date.toordinal() year, day_of_year = test_date.get_ordinal_date() test_data = day_of_year test_data += get_days_since_1_ad(year - 1) self.assertEqual(test_data, ctrl_data) for attribute, attr_max in test_duration_attributes: kwargs = {attribute: random.randrange(0, attr_max)} ctrl_data = my_date + datetime.timedelta(**kwargs) ctrl_data = ctrl_data.year, ctrl_data.month, ctrl_data.day test_data = ((test_date + Duration(**kwargs)).get_calendar_date()) self.assertEqual(test_data, ctrl_data) ctrl_data = my_date - datetime.timedelta(**kwargs) ctrl_data = ctrl_data.year, ctrl_data.month, ctrl_data.day # TBD: the subtraction is quite slow. Much slower than other # operations. Could be related to the fact it converts the value # in kwargs to negative multiplying by -1 (i.e. from __sub__ to # __mul__), and also adds it to the date (i.e. __add__). # Profiling the tests, the __sub__ operation used in the next # line will appear amongst the top of time consuming operations. test_data = ((test_date - Duration(**kwargs)).get_calendar_date()) self.assertEqual(test_data, ctrl_data) kwargs = {} for attribute, attr_max in test_duration_attributes: kwargs[attribute] = random.randrange(0, attr_max) test_date_minus = test_date - Duration(**kwargs) test_data = test_date - test_date_minus ctrl_data = Duration(**kwargs) self.assertEqual(test_data, ctrl_data) test_data = test_date_minus + (test_date - test_date_minus) ctrl_data = test_date self.assertEqual(test_data, ctrl_data) test_data = (test_date_minus + Duration(**kwargs)) ctrl_data = test_date self.assertEqual(test_data, ctrl_data) ctrl_data = (my_date + datetime.timedelta(minutes=450) + datetime.timedelta(hours=5) - datetime.timedelta(seconds=500, weeks=5)) ctrl_data = [(ctrl_data.year, ctrl_data.month, ctrl_data.day), (ctrl_data.hour, ctrl_data.minute, ctrl_data.second)] test_data = (test_date + Duration(minutes=450) + Duration(hours=5) - Duration(weeks=5, seconds=500)) test_data = [ test_data.get_calendar_date(), test_data.get_hour_minute_second() ] self.assertEqual(test_data, ctrl_data)
def _do_test_dates(self, my_date): """Performs a series of tests against a given date. This method does some time consuming operations, which are not IO bound, so this method is a good candidate to be run concurrently. :param my_date: a date to be tested :type my_date: datetime.datetime """ ctrl_data = my_date.isocalendar() test_date = TimePoint( year=my_date.year, month_of_year=my_date.month, day_of_month=my_date.day ) test_week_date = test_date.to_week_date() test_data = test_week_date.get_week_date() self.assertEqual(test_data, ctrl_data) ctrl_data = (my_date.year, my_date.month, my_date.day) test_data = test_week_date.get_calendar_date() self.assertEqual(test_data, ctrl_data) ctrl_data = my_date.toordinal() year, day_of_year = test_date.get_ordinal_date() test_data = day_of_year test_data += get_days_since_1_ad(year - 1) self.assertEqual(test_data, ctrl_data) for attribute, attr_max in test_duration_attributes: kwargs = {attribute: random.randrange(0, attr_max)} ctrl_data = my_date + datetime.timedelta(**kwargs) ctrl_data = ctrl_data.year, ctrl_data.month, ctrl_data.day test_data = ( (test_date + Duration(**kwargs)).get_calendar_date()) self.assertEqual(test_data, ctrl_data) ctrl_data = my_date - datetime.timedelta(**kwargs) ctrl_data = ctrl_data.year, ctrl_data.month, ctrl_data.day # TBD: the subtraction is quite slow. Much slower than other # operations. Could be related to the fact it converts the value # in kwargs to negative multiplying by -1 (i.e. from __sub__ to # __mul__), and also adds it to the date (i.e. __add__). # Profiling the tests, the __sub__ operation used in the next # line will appear amongst the top of time consuming operations. test_data = ( (test_date - Duration(**kwargs)).get_calendar_date()) self.assertEqual(test_data, ctrl_data) kwargs = {} for attribute, attr_max in test_duration_attributes: kwargs[attribute] = random.randrange(0, attr_max) test_date_minus = test_date - Duration(**kwargs) test_data = test_date - test_date_minus ctrl_data = Duration(**kwargs) self.assertEqual(test_data, ctrl_data) test_data = test_date_minus + (test_date - test_date_minus) ctrl_data = test_date self.assertEqual(test_data, ctrl_data) test_data = (test_date_minus + Duration(**kwargs)) ctrl_data = test_date self.assertEqual(test_data, ctrl_data) ctrl_data = ( my_date + datetime.timedelta(minutes=450) + datetime.timedelta(hours=5) - datetime.timedelta(seconds=500, weeks=5)) ctrl_data = [ (ctrl_data.year, ctrl_data.month, ctrl_data.day), (ctrl_data.hour, ctrl_data.minute, ctrl_data.second)] test_data = ( test_date + Duration(minutes=450) + Duration(hours=5) - Duration(weeks=5, seconds=500) ) test_data = [ test_data.get_calendar_date(), test_data.get_hour_minute_second()] self.assertEqual(test_data, ctrl_data)