-
Notifications
You must be signed in to change notification settings - Fork 2
/
eastern_tzinfo.py
36 lines (29 loc) · 1.06 KB
/
eastern_tzinfo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
'''
tzinfo object to convert UTC datetime object to EST/EDT time
Can be used to convert datetime.now() from UTC to EST, e.g.
datetime.datetime.now(eastern_tzinfo())
Code modified from:
https://cloud.google.com/appengine/docs/python/datastore/typesandpropertyclasses?csw=1#datetime
'''
import datetime
class Eastern_tzinfo(datetime.tzinfo):
"""Implementation of the Eastern timezone."""
def utcoffset(self, dt):
return datetime.timedelta(hours=-5) + self.dst(dt)
def _FirstSunday(self, dt):
"""First Sunday on or after dt."""
return dt + datetime.timedelta(days=(6-dt.weekday()))
def dst(self, dt):
# 2 am on the second Sunday in March
dst_start = self._FirstSunday(datetime.datetime(dt.year, 3, 8, 2))
# 1 am on the first Sunday in November
dst_end = self._FirstSunday(datetime.datetime(dt.year, 11, 1, 1))
if dst_start <= dt.replace(tzinfo=None) < dst_end:
return datetime.timedelta(hours=1)
else:
return datetime.timedelta(hours=0)
def tzname(self, dt):
if self.dst(dt) == datetime.timedelta(hours=0):
return "EST"
else:
return "EDT"