Test the datetime_exists / datetime_ambiguous functions
| 2520 | |
| 2521 | |
| 2522 | class DatetimeAmbiguousTest(TzSourceMixin, unittest.TestCase): |
| 2523 | """ Test the datetime_exists / datetime_ambiguous functions """ |
| 2524 | |
| 2525 | def testNoTzSpecified(self): |
| 2526 | with self.assertRaises(ValueError): |
| 2527 | tz.datetime_ambiguous(datetime(2016, 4, 1, 2, 9)) |
| 2528 | |
| 2529 | def _get_no_support_tzinfo_class(self, dt_start, dt_end, dst_only=False): |
| 2530 | # Generates a class of tzinfo with no support for is_ambiguous |
| 2531 | # where dates between dt_start and dt_end are ambiguous. |
| 2532 | |
| 2533 | class FoldingTzInfo(tzinfo): |
| 2534 | def utcoffset(self, dt): |
| 2535 | if not dst_only: |
| 2536 | dt_n = dt.replace(tzinfo=None) |
| 2537 | |
| 2538 | if dt_start <= dt_n < dt_end and dt_n.fold: |
| 2539 | return timedelta(hours=-1) |
| 2540 | |
| 2541 | return timedelta(hours=0) |
| 2542 | |
| 2543 | def dst(self, dt): |
| 2544 | dt_n = dt.replace(tzinfo=None) |
| 2545 | |
| 2546 | if dt_start <= dt_n < dt_end and dt_n.fold: |
| 2547 | return timedelta(hours=1) |
| 2548 | else: |
| 2549 | return timedelta(0) |
| 2550 | |
| 2551 | return FoldingTzInfo |
| 2552 | |
| 2553 | def _get_no_support_tzinfo(self, dt_start, dt_end, dst_only=False): |
| 2554 | return self._get_no_support_tzinfo_class(dt_start, dt_end, dst_only)() |
| 2555 | |
| 2556 | def testNoSupportAmbiguityFoldNaive(self): |
| 2557 | dt_start = datetime(2018, 9, 1, 1, 0) |
| 2558 | dt_end = datetime(2018, 9, 1, 2, 0) |
| 2559 | |
| 2560 | tzi = self._get_no_support_tzinfo(dt_start, dt_end) |
| 2561 | |
| 2562 | self.assertTrue(tz.datetime_ambiguous(datetime(2018, 9, 1, 1, 30), |
| 2563 | tz=tzi)) |
| 2564 | |
| 2565 | def testNoSupportAmbiguityFoldAware(self): |
| 2566 | dt_start = datetime(2018, 9, 1, 1, 0) |
| 2567 | dt_end = datetime(2018, 9, 1, 2, 0) |
| 2568 | |
| 2569 | tzi = self._get_no_support_tzinfo(dt_start, dt_end) |
| 2570 | |
| 2571 | self.assertTrue(tz.datetime_ambiguous(datetime(2018, 9, 1, 1, 30, |
| 2572 | tzinfo=tzi))) |
| 2573 | |
| 2574 | def testNoSupportAmbiguityUnambiguousNaive(self): |
| 2575 | dt_start = datetime(2018, 9, 1, 1, 0) |
| 2576 | dt_end = datetime(2018, 9, 1, 2, 0) |
| 2577 | |
| 2578 | tzi = self._get_no_support_tzinfo(dt_start, dt_end) |
| 2579 |
nothing calls this directly
no outgoing calls
no test coverage detected