Skip to content

Commit 28e2a66

Browse files
committed
i1abstraction/
1 parent 06aab8c commit 28e2a66

1 file changed

Lines changed: 58 additions & 0 deletions

File tree

  • Modular-Programming-with-Python/ch5Working with Module Patterns/i1abstraction
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# happy_hour.py
2+
#
3+
# This program demonstrates how to calculate "happy hour", where happy hour
4+
# takes place each day between 5 and 6 pm, except on Sundays, Christmas Day and
5+
# Easter.
6+
7+
import datetime
8+
9+
#############################################################################
10+
11+
def is_happy_hour():
12+
today = datetime.date.today()
13+
14+
easter_sunday = calc_easter_sunday(today.year)
15+
easter_friday = easter_sunday - datetime.timedelta(days=2)
16+
17+
if today == easter_friday:
18+
return False
19+
20+
if today.month == 12 and today.day == 25: # Christmas day.
21+
return False
22+
23+
if today.weekday() == 6: # Sunday.
24+
return False
25+
26+
if datetime.datetime.now().hour == 17: # 5pm.
27+
return True
28+
29+
return False
30+
31+
#############################################################################
32+
33+
# The following function calculates Easter Sunday for a given year, returning a
34+
# datetime.date object. This code is taken from:
35+
#
36+
# http://code.activestate.com/recipes/576517
37+
38+
def calc_easter_sunday(year):
39+
a = year % 19
40+
b = year // 100
41+
c = year % 100
42+
d = (19 * a + b - b // 4 - ((b - (b + 8) // 25 + 1) // 3) + 15) % 30
43+
e = (32 + 2 * (b % 4) + 2 * (c // 4) - d - (c % 4)) % 7
44+
f = d + e - 7 * ((a + 11 * d + 22 * e) // 451) + 114
45+
month = f // 31
46+
day = f % 31 + 1
47+
return datetime.date(year, month, day)
48+
49+
#############################################################################
50+
51+
if __name__ == "__main__":
52+
# Testing code.
53+
54+
if is_happy_hour():
55+
print("It's happy hour!")
56+
else:
57+
print("It's not happy hour.")
58+

0 commit comments

Comments
 (0)