| 859 | @pytest.mark.tzlocal |
| 860 | @unittest.skipIf(IS_WIN, "requires Unix") |
| 861 | class TzLocalNixTest(unittest.TestCase, TzFoldMixin): |
| 862 | # This is a set of tests for `tzlocal()` on *nix systems |
| 863 | |
| 864 | # POSIX string indicating change to summer time on the 2nd Sunday in March |
| 865 | # at 2AM, and ending the 1st Sunday in November at 2AM. (valid >= 2007) |
| 866 | TZ_EST = 'EST+5EDT,M3.2.0/2,M11.1.0/2' |
| 867 | |
| 868 | # POSIX string for AEST/AEDT (valid >= 2008) |
| 869 | TZ_AEST = 'AEST-10AEDT,M10.1.0/2,M4.1.0/3' |
| 870 | |
| 871 | # POSIX string for BST/GMT |
| 872 | TZ_LON = 'GMT0BST,M3.5.0,M10.5.0' |
| 873 | |
| 874 | # POSIX string for UTC |
| 875 | UTC = 'UTC' |
| 876 | |
| 877 | def gettz(self, tzname): |
| 878 | # Actual time zone changes are handled by the _gettz_context function |
| 879 | return tz.tzlocal() |
| 880 | |
| 881 | def _gettz_context(self, tzname): |
| 882 | tzname_map = {'Australia/Sydney': self.TZ_AEST, |
| 883 | 'America/Toronto': self.TZ_EST, |
| 884 | 'America/New_York': self.TZ_EST, |
| 885 | 'Europe/London': self.TZ_LON} |
| 886 | |
| 887 | return TZEnvContext(tzname_map.get(tzname, tzname)) |
| 888 | |
| 889 | def _testTzFunc(self, tzval, func, std_val, dst_val): |
| 890 | """ |
| 891 | This generates tests about how the behavior of a function ``func`` |
| 892 | changes between STD and DST (e.g. utcoffset, tzname, dst). |
| 893 | |
| 894 | It assume that DST starts the 2nd Sunday in March and ends the 1st |
| 895 | Sunday in November |
| 896 | """ |
| 897 | with TZEnvContext(tzval): |
| 898 | dt1 = datetime(2015, 2, 1, 12, 0, tzinfo=tz.tzlocal()) # STD |
| 899 | dt2 = datetime(2015, 5, 1, 12, 0, tzinfo=tz.tzlocal()) # DST |
| 900 | |
| 901 | self.assertEqual(func(dt1), std_val) |
| 902 | self.assertEqual(func(dt2), dst_val) |
| 903 | |
| 904 | def _testTzName(self, tzval, std_name, dst_name): |
| 905 | func = datetime.tzname |
| 906 | |
| 907 | self._testTzFunc(tzval, func, std_name, dst_name) |
| 908 | |
| 909 | def testTzNameDST(self): |
| 910 | # Test tzname in a zone with DST |
| 911 | self._testTzName(self.TZ_EST, 'EST', 'EDT') |
| 912 | |
| 913 | def testTzNameUTC(self): |
| 914 | # Test tzname in a zone without DST |
| 915 | self._testTzName(self.UTC, 'UTC', 'UTC') |
| 916 | |
| 917 | def _testOffset(self, tzval, std_off, dst_off): |
| 918 | func = datetime.utcoffset |
nothing calls this directly
no outgoing calls
no test coverage detected