|
1 | | -from datetime import timedelta, timezone, tzinfo |
2 | | -from zoneinfo import ZoneInfo |
3 | | - |
4 | 1 | import pytest |
5 | 2 |
|
6 | 3 | from allotropy.exceptions import AllotropeConversionError |
7 | | -from allotropy.parsers.utils.timestamp_parser import TimestampParser |
8 | | - |
9 | | - |
10 | | -@pytest.mark.parametrize( |
11 | | - "time_str,expected", |
12 | | - [ |
13 | | - ("10-11-08", "2008-10-11T00:00:00+00:00"), |
14 | | - ("Fri, 11 Nov 2011 03:18:09", "2011-11-11T03:18:09+00:00"), |
15 | | - ("Fri, 11 Nov 2011 03:18:09 -0400", "2011-11-11T03:18:09-04:00"), |
16 | | - ("Tue Jun 22 07:46:22 EST 2010", "2010-06-22T07:46:22-05:00"), |
17 | | - ("Tue Jun 22 07:46:22 EDT 2010", "2010-06-22T07:46:22-04:00"), |
18 | | - ("Tue Jun 22 07:46:22 GMT 2010", "2010-06-22T07:46:22+00:00"), |
19 | | - ], |
20 | | -) |
21 | | -def test_timestamp_parser_default_utc(time_str: str, expected: str | None) -> None: |
22 | | - assert TimestampParser().parse(time_str) == expected |
23 | | - |
24 | | - |
25 | | -# Similar timezones, but different DST info. |
26 | | -US_PACIFIC = ZoneInfo("US/Pacific") |
27 | | -UTC_MINUS_7 = timezone(timedelta(hours=-7)) |
28 | | - |
29 | | - |
30 | | -@pytest.mark.parametrize( |
31 | | - "default_timezone,time_str,expected", |
32 | | - [ |
33 | | - (US_PACIFIC, "10-11-08", "2008-10-11T00:00:00-07:00"), |
34 | | - (US_PACIFIC, "Fri, 11 Nov 2011 03:18:09", "2011-11-11T03:18:09-08:00"), |
35 | | - (US_PACIFIC, "Fri, 11 Jun 2011 03:18:09", "2011-06-11T03:18:09-07:00"), |
36 | | - (US_PACIFIC, "Fri, 11 Nov 2011 03:18:09 -0400", "2011-11-11T03:18:09-04:00"), |
37 | | - (US_PACIFIC, "Tue Jun 22 07:46:22 EST 2010", "2010-06-22T07:46:22-05:00"), |
38 | | - (UTC_MINUS_7, "10-11-08", "2008-10-11T00:00:00-07:00"), |
39 | | - (UTC_MINUS_7, "Fri, 11 Nov 2011 03:18:09", "2011-11-11T03:18:09-07:00"), |
40 | | - (UTC_MINUS_7, "Fri, 11 Jun 2011 03:18:09", "2011-06-11T03:18:09-07:00"), |
41 | | - (UTC_MINUS_7, "Fri, 11 Nov 2011 03:18:09 -0400", "2011-11-11T03:18:09-04:00"), |
42 | | - (UTC_MINUS_7, "Tue Jun 22 07:46:22 EST 2010", "2010-06-22T07:46:22-05:00"), |
43 | | - ], |
| 4 | +from allotropy.parsers.utils.locale_context import set_locale_context |
| 5 | +from allotropy.parsers.utils.timestamp_parser import ( |
| 6 | + _should_use_day_first, |
| 7 | + TimestampParser, |
44 | 8 | ) |
45 | | -def test_timestamp_parser_provided_timezone( |
46 | | - default_timezone: tzinfo, time_str: str, expected: str |
47 | | -) -> None: |
48 | | - assert TimestampParser(default_timezone).parse(time_str) == expected |
49 | 9 |
|
50 | 10 |
|
51 | | -@pytest.mark.parametrize("time_str", ["blah"]) |
52 | | -def test_timestamp_parser_fails_on_invalid_timestamp(time_str: str) -> None: |
53 | | - parser = TimestampParser() |
54 | | - with pytest.raises(AllotropeConversionError, match="Could not parse time 'blah'."): |
55 | | - parser.parse(time_str) |
| 11 | +class TestShouldUseDayFirst: |
| 12 | + """Test the _should_use_day_first helper function.""" |
| 13 | + |
| 14 | + def test_iso_8601_year_first_returns_false(self) -> None: |
| 15 | + # ISO 8601 formats are always YYYY-MM-DD (month before day) |
| 16 | + assert _should_use_day_first("2023-01-15", "de_DE") is False |
| 17 | + assert _should_use_day_first("2023/01/15", "de_DE") is False |
| 18 | + assert _should_use_day_first("2023.01.15", "de_DE") is False |
| 19 | + assert _should_use_day_first("2023-12-31", "en_GB") is False |
| 20 | + |
| 21 | + def test_american_locale_returns_false(self) -> None: |
| 22 | + # en_US uses MM/DD/YYYY format |
| 23 | + assert _should_use_day_first("01/15/2023", "en_US") is False |
| 24 | + assert _should_use_day_first("12/31/2023", "en_US") is False |
| 25 | + |
| 26 | + def test_european_locales_return_true(self) -> None: |
| 27 | + # European locales use DD/MM/YYYY format |
| 28 | + assert _should_use_day_first("15/01/2023", "en_GB") is True |
| 29 | + assert _should_use_day_first("15.01.2023", "de_DE") is True |
| 30 | + assert _should_use_day_first("15/01/2023", "fr_FR") is True |
| 31 | + assert _should_use_day_first("15/01/2023", "es_ES") is True |
| 32 | + assert _should_use_day_first("15/01/2023", "it_IT") is True |
| 33 | + |
| 34 | + def test_asian_locales_return_false(self) -> None: |
| 35 | + # Asian locales typically use year-first (YMD) which is month-before-day |
| 36 | + assert _should_use_day_first("15/01/2023", "ja_JP") is False |
| 37 | + assert _should_use_day_first("15/01/2023", "zh_CN") is False |
| 38 | + assert _should_use_day_first("15/01/2023", "ko_KR") is False |
| 39 | + |
| 40 | + def test_none_locale_returns_false(self) -> None: |
| 41 | + # No locale defaults to False (American/month-first) |
| 42 | + assert _should_use_day_first("01/15/2023", None) is False |
| 43 | + assert _should_use_day_first("15/01/2023", None) is False |
| 44 | + |
| 45 | + def test_invalid_locale_returns_false(self) -> None: |
| 46 | + # Invalid locales default to False |
| 47 | + assert _should_use_day_first("01/15/2023", "invalid_LOCALE") is False |
| 48 | + |
| 49 | + |
| 50 | +class TestTimestampParser: |
| 51 | + """Test the TimestampParser class with locale support.""" |
| 52 | + |
| 53 | + def test_parse_iso_8601_no_locale(self) -> None: |
| 54 | + parser = TimestampParser() |
| 55 | + result = parser.parse("2023-01-15T10:30:00Z") |
| 56 | + assert result == "2023-01-15T10:30:00+00:00" |
| 57 | + |
| 58 | + def test_parse_iso_8601_ignores_locale(self) -> None: |
| 59 | + # ISO 8601 format should ignore locale and always parse as YYYY-MM-DD |
| 60 | + parser = TimestampParser() |
| 61 | + |
| 62 | + with set_locale_context("en_US"): |
| 63 | + result_us = parser.parse("2023-01-15") |
| 64 | + with set_locale_context("de_DE"): |
| 65 | + result_de = parser.parse("2023-01-15") |
| 66 | + |
| 67 | + assert "2023-01-15" in result_us |
| 68 | + assert "2023-01-15" in result_de |
| 69 | + |
| 70 | + def test_parse_ambiguous_date_american_locale(self) -> None: |
| 71 | + parser = TimestampParser() |
| 72 | + # 01/02/2023 in American format = January 2nd |
| 73 | + with set_locale_context("en_US"): |
| 74 | + result = parser.parse("01/02/2023") |
| 75 | + assert "2023-01-02" in result |
| 76 | + |
| 77 | + def test_parse_ambiguous_date_european_locale(self) -> None: |
| 78 | + parser = TimestampParser() |
| 79 | + # 01/02/2023 in European format = February 1st |
| 80 | + with set_locale_context("de_DE"): |
| 81 | + result = parser.parse("01/02/2023") |
| 82 | + assert "2023-02-01" in result |
| 83 | + |
| 84 | + def test_parse_unambiguous_date_same_result(self) -> None: |
| 85 | + parser = TimestampParser() |
| 86 | + |
| 87 | + # 31/01/2023 is unambiguous (only valid as January 31st) |
| 88 | + with set_locale_context("en_US"): |
| 89 | + result_us = parser.parse("31/01/2023") |
| 90 | + with set_locale_context("de_DE"): |
| 91 | + result_de = parser.parse("31/01/2023") |
| 92 | + |
| 93 | + assert "2023-01-31" in result_us |
| 94 | + assert "2023-01-31" in result_de |
| 95 | + |
| 96 | + def test_parse_with_timezone(self) -> None: |
| 97 | + parser = TimestampParser() |
| 98 | + with set_locale_context("de_DE"): |
| 99 | + result = parser.parse("15/01/2023 10:30:00 PST") |
| 100 | + assert "2023-01-15" in result |
| 101 | + assert "-08:00" in result # PST is UTC-8 |
| 102 | + |
| 103 | + def test_parse_applies_default_timezone(self) -> None: |
| 104 | + from datetime import timedelta, timezone |
| 105 | + |
| 106 | + custom_tz = timezone(timedelta(hours=5)) |
| 107 | + parser = TimestampParser(default_timezone=custom_tz) |
| 108 | + |
| 109 | + # Date without timezone should get default timezone |
| 110 | + with set_locale_context("de_DE"): |
| 111 | + result = parser.parse("15/01/2023 10:30:00") |
| 112 | + assert "2023-01-15" in result |
| 113 | + assert "+05:00" in result |
| 114 | + |
| 115 | + def test_parse_no_locale_defaults_to_american(self) -> None: |
| 116 | + parser = TimestampParser() |
| 117 | + # Without locale context, should use American format (MM/DD/YYYY) |
| 118 | + result = parser.parse("01/02/2023") |
| 119 | + assert "2023-01-02" in result # January 2nd, not February 1st |
| 120 | + |
| 121 | + def test_parse_various_european_locales(self) -> None: |
| 122 | + # Test multiple European locales to ensure they all use day-first |
| 123 | + locales = ["en_GB", "fr_FR", "es_ES", "it_IT", "pt_PT", "nl_NL"] |
| 124 | + parser = TimestampParser() |
| 125 | + |
| 126 | + for locale in locales: |
| 127 | + with set_locale_context(locale): |
| 128 | + result = parser.parse("15/01/2023") |
| 129 | + assert "2023-01-15" in result, f"Failed for locale {locale}" |
| 130 | + |
| 131 | + def test_parse_fuzzy_matching(self) -> None: |
| 132 | + parser = TimestampParser() |
| 133 | + # dateutil.parser with fuzzy=True can extract dates from text |
| 134 | + with set_locale_context("de_DE"): |
| 135 | + result = parser.parse("Date of measurement: 15/01/2023 at noon") |
| 136 | + assert "2023-01-15" in result |
| 137 | + |
| 138 | + def test_parse_invalid_date_raises_error(self) -> None: |
| 139 | + parser = TimestampParser() |
| 140 | + with pytest.raises( |
| 141 | + AllotropeConversionError, match="Could not parse time 'not a date'" |
| 142 | + ): |
| 143 | + parser.parse("not a date") |
56 | 144 |
|
| 145 | + def test_real_world_examples(self) -> None: |
| 146 | + """Test real-world date formats from various instruments.""" |
| 147 | + parser = TimestampParser() |
| 148 | + test_cases = [ |
| 149 | + # (input, locale, expected_date) |
| 150 | + ("2023-12-25 14:30:00", "en_US", "2023-12-25"), # ISO format |
| 151 | + ("12/25/2023", "en_US", "2023-12-25"), # American Christmas |
| 152 | + ("25/12/2023", "en_GB", "2023-12-25"), # British Christmas |
| 153 | + ("25.12.2023", "de_DE", "2023-12-25"), # German Christmas |
| 154 | + ("2023/12/25", "ja_JP", "2023-12-25"), # Japanese ISO-like |
| 155 | + ("01-FEB-2023", "en_US", "2023-02-01"), # Named month (unambiguous) |
| 156 | + ] |
57 | 157 |
|
58 | | -def test_timestamp_parser_handles_24h_pm() -> None: |
59 | | - assert ( |
60 | | - TimestampParser().parse("2023-03-16 16:52:37 PM") == "2023-03-16T16:52:37+00:00" |
61 | | - ) |
| 158 | + for date_str, locale, expected in test_cases: |
| 159 | + with set_locale_context(locale): |
| 160 | + result = parser.parse(date_str) |
| 161 | + assert ( |
| 162 | + expected in result |
| 163 | + ), f"Failed: {date_str} with {locale} -> {result} (expected {expected})" |
0 commit comments