-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
65 lines (54 loc) · 1.94 KB
/
utils.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import os
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), 'lib'))
from apiclient.discovery import build
import datetime
import httplib2
class Utils:
@classmethod
def saturday(cls):
"""Return this week's saturday."""
d = datetime.datetime.today().replace(hour=20, minute=0, second=0)
weekday = d.weekday()
if weekday == 6:
return d + datetime.timedelta(days=6)
elif weekday < 5:
return d + datetime.timedelta(days=5-weekday)
else:
return d
@classmethod
def now(cls):
"""Return current datetime, adjusted for timezone."""
now = datetime.datetime.now()
return now + Pacific_tzinfo().utcoffset(now)
@classmethod
def get_service(cls, Auth):
creds = Auth.get()
if not creds:
raise Exception('Credentials not found.')
http = creds.authorize(httplib2.Http())
creds.refresh(http)
service = build('calendar', 'v3', credentials=creds)
return service
class Pacific_tzinfo(datetime.tzinfo):
"""Implementation of the Pacific timezone."""
def utcoffset(self, dt):
return datetime.timedelta(hours=-8) + 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 "PST"
else:
return "PDT"
# vim: set ts=4 sts=4 sw=4 et: