OLD | NEW |
1 # This Source Code Form is subject to the terms of the Mozilla Public | 1 # This Source Code Form is subject to the terms of the Mozilla Public |
2 # License, v. 2.0. If a copy of the MPL was not distributed with this | 2 # License, v. 2.0. If a copy of the MPL was not distributed with this |
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/. | 3 # file, You can obtain one at http://mozilla.org/MPL/2.0/. |
4 | 4 |
5 """Module for providing specific timezones""" | 5 """Module for providing specific timezones""" |
6 | 6 |
7 from datetime import datetime, timedelta, tzinfo | 7 from datetime import datetime, timedelta, tzinfo |
8 | 8 |
9 | 9 |
10 class PacificTimezone(tzinfo): | 10 class PacificTimezone(tzinfo): |
11 """Class to set the timezone to PST/PDT and automatically adjusts | 11 """Class to set the timezone to PST/PDT and automatically adjusts |
12 for daylight saving. | 12 for daylight saving. |
13 """ | 13 """ |
14 | 14 |
15 def utcoffset(self, dt): | 15 def utcoffset(self, dt): |
16 return timedelta(hours=-8) + self.dst(dt) | 16 return timedelta(hours=-8) + self.dst(dt) |
17 | 17 |
18 | |
19 def tzname(self, dt): | 18 def tzname(self, dt): |
20 return "Pacific" | 19 return "Pacific" |
21 | 20 |
22 | |
23 def dst(self, dt): | 21 def dst(self, dt): |
24 # Daylight saving starts on the second Sunday of March at 2AM standard | 22 # Daylight saving starts on the second Sunday of March at 2AM standard |
25 dst_start_date = self.first_sunday(dt.year, 3) + timedelta(days=7) \ | 23 dst_start_date = self.first_sunday(dt.year, 3) + timedelta(days=7) \ |
26 + timedelta(hours=2) | 24 + timedelta(hours=2) |
27 # Daylight saving ends on the first Sunday of November at 2AM standard | 25 # Daylight saving ends on the first Sunday of November at 2AM standard |
28 dst_end_date = self.first_sunday(dt.year, 11) + timedelta(hours=2) | 26 dst_end_date = self.first_sunday(dt.year, 11) + timedelta(hours=2) |
29 | 27 |
30 if dst_start_date <= dt.replace(tzinfo=None) < dst_end_date: | 28 if dst_start_date <= dt.replace(tzinfo=None) < dst_end_date: |
31 return timedelta(hours=1) | 29 return timedelta(hours=1) |
32 else: | 30 else: |
33 return timedelta(0) | 31 return timedelta(0) |
34 | 32 |
35 | |
36 def first_sunday(self, year, month): | 33 def first_sunday(self, year, month): |
37 date = datetime(year, month, 1, 0) | 34 date = datetime(year, month, 1, 0) |
38 days_until_sunday = 6 - date.weekday() | 35 days_until_sunday = 6 - date.weekday() |
39 | 36 |
40 return date + timedelta(days=days_until_sunday) | 37 return date + timedelta(days=days_until_sunday) |
OLD | NEW |