OLD | NEW |
(Empty) | |
| 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 |
| 3 # file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 4 |
| 5 """Module for providing specific timezones""" |
| 6 |
| 7 from datetime import datetime, timedelta, tzinfo |
| 8 |
| 9 |
| 10 class PacificTimezone(tzinfo): |
| 11 """Class to set the timezone to PST/PDT and automatically adjusts |
| 12 for daylight saving. |
| 13 """ |
| 14 |
| 15 def utcoffset(self, dt): |
| 16 return timedelta(hours=-8) + self.dst(dt) |
| 17 |
| 18 |
| 19 def tzname(self, dt): |
| 20 return "Pacific" |
| 21 |
| 22 |
| 23 def dst(self, dt): |
| 24 # 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) \ |
| 26 + timedelta(hours=2) |
| 27 # 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) |
| 29 |
| 30 if dst_start_date <= dt.replace(tzinfo=None) < dst_end_date: |
| 31 return timedelta(hours=1) |
| 32 else: |
| 33 return timedelta(0) |
| 34 |
| 35 |
| 36 def first_sunday(self, year, month): |
| 37 date = datetime(year, month, 1, 0) |
| 38 days_until_sunday = 6 - date.weekday() |
| 39 |
| 40 return date + timedelta(days=days_until_sunday) |
OLD | NEW |