def test3get_match_element_with_different_date_formats(self): """Test if different date_formats can be used to match data.""" tz_gmt10 = pytz.timezone("Etc/GMT+10") en_gb_utf8 = "en_GB.utf8" en_us_utf8 = "en_US.utf8" de_at_utf8 = "de_AT.utf8" multi_locale_dtme = MultiLocaleDateTimeModelElement( self.id_, [(b"%d.%m.%Y %H:%M:%S.%f", None, None), (b"%d.%m.%Y %H:%M:%S%z", None, None), (b"%d.%m.%Y %H:%M:%S", None, None), (b"%d.%m.%YT%H:%M:%S", None, None), (b"%d.%m.%Y", None, None), (b"%H:%M:%S:%f", None, de_at_utf8), (b"%H:%M:%S", None, None), (b"%b %d", tz_gmt10, de_at_utf8), (b"%d %b %Y", None, en_gb_utf8), (b"%dth %b %Y", None, en_gb_utf8), (b"%d/%m/%Y", None, en_gb_utf8), (b"%m-%d-%Y", None, en_us_utf8), (b"%d.%m. %H:%M:%S:%f", None, de_at_utf8)]) # test normal date data = b"07.02.2019 11:40:00: it still works" date = b"07.02.2019 11:40:00" match_context = DummyMatchContext(data) match_element = multi_locale_dtme.get_match_element( self.path, match_context) self.compare_match_results(data, match_element, match_context, self.id_ + "/format1", self.path, date, 1549539600, None) # test leap year date data = b"29.02.2020 11:40:00: it still works" date = b"29.02.2020 11:40:00" match_context = DummyMatchContext(data) match_element = multi_locale_dtme.get_match_element( self.path, match_context) self.compare_match_results(data, match_element, match_context, self.id_ + "/format1", self.path, date, 1582976400, None) # test normal date with T data = b"07.02.2019T11:40:00: it still works" date = b"07.02.2019T11:40:00" match_context = DummyMatchContext(data) match_element = multi_locale_dtme.get_match_element( self.path, match_context) self.compare_match_results(data, match_element, match_context, self.id_ + "/format3", self.path, date, 1549539600, None) # test normal date with fractions data = b"07.02.2019 11:40:00.123456: it still works" date = b"07.02.2019 11:40:00.123456" match_context = DummyMatchContext(data) match_element = multi_locale_dtme.get_match_element( self.path, match_context) self.compare_match_results(data, match_element, match_context, self.id_ + "/format0", self.path, date, 1549539600.123456, None) # test normal date with z data = b"07.02.2019 11:40:00+0000: it still works" date = b"07.02.2019 11:40:00+0000" match_context = DummyMatchContext(data) match_element = multi_locale_dtme.get_match_element( self.path, match_context) self.compare_match_results(data, match_element, match_context, self.id_ + "/format1", self.path, date, 1549539600, None) # test with only date defined data = b"07.02.2019: it still works" date = b"07.02.2019" match_context = DummyMatchContext(data) match_element = multi_locale_dtme.get_match_element( self.path, match_context) self.compare_match_results(data, match_element, match_context, self.id_ + "/format4", self.path, date, 1549497600, None) # test with only time defined. Here obviously the seconds can not be tested. data = b"11:40:23: it still works" date = b"11:40:23" match_context = DummyMatchContext(data) match_element = multi_locale_dtme.get_match_element( self.path, match_context) self.compare_match_results(data, match_element, match_context, self.id_ + "/format6", self.path, date, match_element.match_object, None) data = b"Feb 25 something happened" date = b"Feb 25" match_context = DummyMatchContext(data) match_element = multi_locale_dtme.get_match_element( self.path, match_context) dtm = datetime(datetime.now().year, 2, 25, tzinfo=tz_gmt10) # total_seconds should be in UTC, so the timezones are parsed out. total_seconds = (dtm - datetime(1970, 1, 1, tzinfo=tz_gmt10) ).days * 86400 - dtm.utcoffset().total_seconds() self.compare_match_results(data, match_element, match_context, self.id_ + "/format7", self.path, date, total_seconds, None) # British date data = b"13 Apr 2019 something happened" date = b"13 Apr 2019" match_context = DummyMatchContext(data) match_element = multi_locale_dtme.get_match_element( self.path, match_context) self.compare_match_results(data, match_element, match_context, self.id_ + "/format8", self.path, date, 1555113600, None) # British date 2 data = b"13th Apr 2019 something happened" date = b"13th Apr 2019" match_context = DummyMatchContext(data) match_element = multi_locale_dtme.get_match_element( self.path, match_context) self.compare_match_results(data, match_element, match_context, self.id_ + "/format9", self.path, date, 1555113600, None) # British date 3 data = b"13/04/2019 something happened" date = b"13/04/2019" match_context = DummyMatchContext(data) match_element = multi_locale_dtme.get_match_element( self.path, match_context) self.compare_match_results(data, match_element, match_context, self.id_ + "/format10", self.path, date, 1555113600, None) # US date data = b"04-13-2019 something happened" date = b"04-13-2019" match_context = DummyMatchContext(data) match_element = multi_locale_dtme.get_match_element( self.path, match_context) self.compare_match_results(data, match_element, match_context, self.id_ + "/format11", self.path, date, 1555113600, None) # Austrian date no year - year should already be learnt. # start year has to be 2021, because all other formats have defined years. data = b"13.04. 15:12:54:201 something happened" date = b"13.04. 15:12:54:201" match_context = DummyMatchContext(data) match_element = multi_locale_dtme.get_match_element( self.path, match_context) self.compare_match_results(data, match_element, match_context, self.id_ + "/format12", self.path, date, 1618326774.201, None) multi_locale_dtme.latest_parsed_timestamp = None # Austrian time no date data = b"15:12:54:201 something happened" date = b"15:12:54:201" match_context = DummyMatchContext(data) match_element = multi_locale_dtme.get_match_element( self.path, match_context) dtm = datetime(datetime.now().year, datetime.now().month, datetime.now().day, 15, 12, 54, 201, tzinfo=timezone.utc) # total_seconds should be in UTC, so the timezones are parsed out. delta = (dtm - datetime(1970, 1, 1, tzinfo=dtm.tzinfo)) total_seconds = delta.days * 86400 + delta.seconds + delta.microseconds / 1000 self.compare_match_results(data, match_element, match_context, self.id_ + "/format5", self.path, date, total_seconds, None)
def test3get_match_element_with_different_date_formats(self): """Test if different date_formats can be used to match data.""" tz_gmt10 = pytz.timezone("Etc/GMT+10") en_gb_utf8 = "en_GB.utf8" en_us_utf8 = "en_US.utf8" de_at_utf8 = "de_AT.utf8" multi_locale_dtme = MultiLocaleDateTimeModelElement("path", [ (b"%d.%m.%Y %H:%M:%S.%f", None, None), (b"%d.%m.%Y %H:%M:%S%z", None, None), (b"%d.%m.%Y %H:%M:%S", None, None), (b"%d.%m.%YT%H:%M:%S", None, None), (b"%d.%m.%Y", None, None), (b"%H:%M:%S:%f", None, de_at_utf8), (b"%H:%M:%S", None, None), (b"%b %d", tz_gmt10, de_at_utf8), (b"%d %b %Y", None, en_gb_utf8), (b"%dth %b %Y", None, en_gb_utf8), (b"%d/%m/%Y", None, en_gb_utf8), (b"%m-%d-%Y", None, en_us_utf8), (b"%d.%m. %H:%M:%S:%f", None, de_at_utf8)]) # test normal date match_context = DummyMatchContext(b"07.02.2019 11:40:00: it still works") match_element = multi_locale_dtme.get_match_element("match1", match_context) self.assertEqual(match_element.match_string, b"07.02.2019 11:40:00") self.assertEqual(match_element.match_object, 1549539600) self.assertEqual(match_context.match_string, b"07.02.2019 11:40:00") # test leap year date match_context = DummyMatchContext(b"29.02.2020 11:40:00: it still works") match_element = multi_locale_dtme.get_match_element("match1", match_context) self.assertEqual(match_element.match_string, b"29.02.2020 11:40:00") self.assertEqual(match_element.match_object, 1582976400) self.assertEqual(match_context.match_string, b"29.02.2020 11:40:00") # test normal date with T match_context = DummyMatchContext(b"07.02.2019T11:40:00: it still works") match_element = multi_locale_dtme.get_match_element("match1", match_context) self.assertEqual(match_element.match_string, b"07.02.2019T11:40:00") self.assertEqual(match_element.match_object, 1549539600) self.assertEqual(match_context.match_string, b"07.02.2019T11:40:00") # test normal date with fractions match_context = DummyMatchContext(b"07.02.2019 11:40:00.123456: it still works") match_element = multi_locale_dtme.get_match_element("match1", match_context) self.assertEqual(match_element.match_string, b"07.02.2019 11:40:00.123456") self.assertEqual(match_element.match_object, 1549539600.123456) self.assertEqual(match_context.match_string, b"07.02.2019 11:40:00.123456") # test normal date with z match_context = DummyMatchContext(b"07.02.2019 11:40:00+0000: it still works") match_element = multi_locale_dtme.get_match_element("match1", match_context) self.assertEqual(match_element.match_string, b"07.02.2019 11:40:00+0000") self.assertEqual(match_element.match_object, 1549539600) self.assertEqual(match_context.match_string, b"07.02.2019 11:40:00+0000") match_context = DummyMatchContext(b"07.02.2019 11:40:00: it still works") match_element = multi_locale_dtme.get_match_element("match1", match_context) self.assertEqual(match_element.match_string, b"07.02.2019 11:40:00") self.assertEqual(match_element.match_object, 1549539600) self.assertEqual(match_context.match_string, b"07.02.2019 11:40:00") # test with only date defined match_context = DummyMatchContext(b"07.02.2019: it still works") match_element = multi_locale_dtme.get_match_element("match1", match_context) self.assertEqual(match_element.match_string, b"07.02.2019") self.assertEqual(match_element.match_object, 1549497600) self.assertEqual(match_context.match_string, b"07.02.2019") # test with only time defined. Here obviously the seconds can not be tested. match_context = DummyMatchContext(b"11:40:23: it still works") match_element = multi_locale_dtme.get_match_element("match1", match_context) self.assertEqual(match_element.match_string, b"11:40:23") self.assertEqual(match_context.match_string, b"11:40:23") string = b"Feb 25" match_context = DummyMatchContext(string) match_element = multi_locale_dtme.get_match_element("match", match_context) date = datetime(datetime.now().year, 2, 25, tzinfo=tz_gmt10) # total_seconds should be in UTC, so the timezones are parsed out. total_seconds = (date - datetime(1970, 1, 1, tzinfo=tz_gmt10)).days * 86400 - date.utcoffset().total_seconds() self.assertEqual(match_element.match_string, b"Feb 25") self.assertEqual(match_element.match_object, total_seconds) self.assertEqual(match_context.match_string, b"Feb 25") # British date string = b"13 Apr 2019" match_context = DummyMatchContext(string) match_element = multi_locale_dtme.get_match_element("match", match_context) self.assertEqual(match_element.match_string, b"13 Apr 2019") self.assertEqual(match_element.match_object, 1555113600) self.assertEqual(match_context.match_string, b"13 Apr 2019") # British date 2 string = b"13th Apr 2019" match_context = DummyMatchContext(string) match_element = multi_locale_dtme.get_match_element("match", match_context) self.assertEqual(match_element.match_string, b"13th Apr 2019") self.assertEqual(match_element.match_object, 1555113600) self.assertEqual(match_context.match_string, b"13th Apr 2019") # British date 3 string = b"13/04/2019" match_context = DummyMatchContext(string) match_element = multi_locale_dtme.get_match_element("match", match_context) self.assertEqual(match_element.match_string, b"13/04/2019") self.assertEqual(match_element.match_object, 1555113600) self.assertEqual(match_context.match_string, b"13/04/2019") # US date string = b"04-13-2019" match_context = DummyMatchContext(string) match_element = multi_locale_dtme.get_match_element("match", match_context) self.assertEqual(match_element.match_string, b"04-13-2019") self.assertEqual(match_element.match_object, 1555113600) self.assertEqual(match_context.match_string, b"04-13-2019") # Austrian date no year - year should already be learnt. # start year has to be 2021, because all other formats have defined years. string = b"13.04. 15:12:54:201" match_context = DummyMatchContext(string) match_element = multi_locale_dtme.get_match_element("match", match_context) self.assertEqual(match_element.match_string, b"13.04. 15:12:54:201") self.assertEqual(match_element.match_object, 1618326774.201) self.assertEqual(match_context.match_string, b"13.04. 15:12:54:201") multi_locale_dtme.latest_parsed_timestamp = None # Austrian time no date string = b"15:12:54:201" match_context = DummyMatchContext(string) match_element = multi_locale_dtme.get_match_element("match", match_context) date = datetime(datetime.now().year, datetime.now().month, datetime.now().day, 15, 12, 54, 201, tzinfo=timezone.utc) # total_seconds should be in UTC, so the timezones are parsed out. delta = (date - datetime(1970, 1, 1, tzinfo=date.tzinfo)) total_seconds = delta.days * 86400 + delta.seconds + delta.microseconds / 1000 self.assertEqual(match_element.match_string, b"15:12:54:201") self.assertEqual(match_element.match_object, total_seconds) self.assertEqual(match_context.match_string, b"15:12:54:201")