forked from aronsky/parking
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shared.py
77 lines (57 loc) · 1.85 KB
/
shared.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
66
67
68
69
70
71
72
73
74
75
76
77
import datetime
import time
import hashlib
import random
#############
# Constants #
#############
INSIDE_SPOTS_COUNT = 5
OUTSIDE_SPOTS_COUNT = 28
GUEST_PLATE = 9999999
############
# Timezone #
############
class Ist(datetime.tzinfo):
def dst(self, dt):
return datetime.timedelta(0)
def tzname(self, dt):
return "IST-02IDT"
def utcoffset(self, dt):
return datetime.timedelta(hours=3)
##############
# Exceptions #
##############
class ParkingException(Exception):
pass
class DoubleBookingException(ParkingException):
pass
class NoFreeSpaceException(ParkingException):
pass
class SpotTakenException(ParkingException):
pass
class SpotNotTakenException(ParkingException):
pass
class SpotReservedException(ParkingException):
pass
class SpotNotReservedException(ParkingException):
pass
#############
# Functions #
#############
def make_name(user):
return user.nickname().split('@')[0].replace('.', ' ').title()
def is_downtime():
if is_weekend():
return True
current_time = datetime.datetime.now(Ist())
minimum_time = current_time.replace(hour=5, minute=45, second=0, microsecond=0)
seed_base = current_time.strftime("Year: %Y Month: %m Day:%d")
seed = int(hashlib.new("md5", seed_base).hexdigest(), 16) % 1024
randomizer = random.Random(seed)
return not (current_time - minimum_time).total_seconds() > 60 * randomizer.randint(0,60)
def is_weekend():
return datetime.datetime.now(Ist()).isoweekday() in (5, 6)
def get_current_time():
current_time = datetime.datetime.now(Ist())
minimum_time = current_time.replace(hour=5, minute=45, second=0, microsecond=0)
return "Current time: %s; Minimum time: %s" % (current_time.strftime("%H:%M:%S %Z"), minimum_time.strftime("%H:%M:%S %Z"))