def _fix_hijri_gregorian_feb_mismatch(self, date_formats, languages): # Now, search for 29th or 30th day of 2nd month. # If found, reduce it by 10 days and use regular parse # function again, if succeeds this time, then add 10 # days to parsed Hijri form. for lang_shortname in languages: language = default_language_loader.get_language(lang_shortname) translated = language.translate(self.source) def _sub_fn(m): digit = int(m.group(0)) return "{:02d}".format(digit - 10) fixed_date_string, nreplaced = re.subn(r"(?<!\d)(29|30)", _sub_fn, translated, 1) if not nreplaced: continue date_data = self._parser_get_date(fixed_date_string, date_formats, languages) date_obj = date_data.get("date_obj") if date_obj: # Remember that, we have subtracted 10 days. date_data["date_obj"] = self._hijri_to_gregorian( date_obj.year, date_obj.month, date_obj.day + 10, date_obj ) return date_data
def _fix_hijri_gregorian_feb_mismatch(self, date_formats, languages): # Now, search for 29th or 30th day of 2nd month. # If found, reduce it by 10 days and use regular parse # function again, if succeeds this time, then add 10 # days to parsed Hijri form. for lang_shortname in languages: language = default_language_loader.get_language(lang_shortname) translated = language.translate(self.source) def _sub_fn(m): digit = int(m.group(0)) return '{:02d}'.format(digit - 10) fixed_date_string, nreplaced = re.subn(r'(?<!\d)(29|30)', _sub_fn, translated, 1) if not nreplaced: continue date_data = self._parser_get_date(fixed_date_string, date_formats, languages) date_obj = date_data.get('date_obj') if date_obj: # Remember that, we have subtracted 10 days. date_data['date_obj'] = self._hijri_to_gregorian( date_obj.year, date_obj.month, date_obj.day + 10, date_obj) return date_data
def _detect_parse_error(self, date_formats, languages): """ Check following cases: * 2nd month in Hijri calendar can be 29 or 30 days whilst this is not possible for Gregorian calendar. """ for lang_shortname in languages: language = default_language_loader.get_language(lang_shortname) translated = language.translate(self.source) for date_format in date_formats: try: datetime.strptime(date_format, translated) except ValueError: sep = find_date_separator(date_format) m = re.search(r"(?<!\d)(?:(?:(0?2){sep}(29|30))|(?:(29|30){sep}(0?2)))".format(sep=sep), translated) if m: raise HijriGregorianFebruaryMismatch()
def _detect_parse_error(self, date_formats, languages): """ Check following cases: * 2nd month in Hijri calendar can be 29 or 30 days whilst this is not possible for Gregorian calendar. """ for lang_shortname in languages: language = default_language_loader.get_language(lang_shortname) translated = language.translate(self.source, settings=settings) for date_format in date_formats: try: datetime.strptime(date_format, translated) except ValueError: sep = find_date_separator(date_format) m = re.search( r'(?<!\d)(?:(?:(0?2){sep}(29|30))|(?:(29|30){sep}(0?2)))'.format(sep=sep), translated) if m: raise HijriGregorianFebruaryMismatch()