1 |
""" |
2 |
------------------------------------------------------- |
3 |
Takes self hour, self day of month, and those of another |
4 |
person. Outputs a string with the hour difference and |
5 |
description of whether the other user is ahead or behind |
6 |
self. |
7 |
|
8 |
Use: get_hr_diff(12, 13, 12, 5) |
9 |
------------------------------------------------------- |
10 |
Preconditions: |
11 |
self_day - day of month of self (int) |
12 |
other_day - day of month of other person (int) |
13 |
self_hour - hour of day of self (int) |
14 |
other_hour - hour of day of other (int) |
15 |
Postconditions: |
16 |
td_msg - the hour difference and ahead/behind (str) |
17 |
------------------------------------------------------- |
18 |
|
19 |
""" |
20 |
|
21 |
def get_hr_diff(self_day, other_day, self_hour, other_hour): |
22 |
|
23 |
if self_day < 1 or other_day < 1 or self_day > 31 or other_day > 31 or self_hour < 0 or self_hour > 23 or other_hour < 0 or other_hour > 23: |
24 |
return "Hour difference cannot be displayed" |
25 |
|
26 |
day_delta = abs(other_day-self_day) |
27 |
|
28 |
if day_delta > 2: # Month wraparound has occurred (up to 26 hrs diff) |
29 |
|
30 |
# Other is 1 or more days ahead |
31 |
if other_day <= 2: |
32 |
td_msg = str(other_hour+(other_day-1)*24+24-self_hour) + " hours ahead of you" |
33 |
|
34 |
# Should never happen |
35 |
elif other_day > 2: |
36 |
td_msg = "Hour difference cannot be displayed" |
37 |
|
38 |
# I am 1 or more days ahead |
39 |
else: |
40 |
td_msg = str(self_hour+(self_day-1)*24+24-other_hour) + " hours behind you" |
41 |
|
42 |
elif day_delta > 0: # Different days but no month wraparound (<= 26 hrs) |
43 |
|
44 |
if other_day > self_day: |
45 |
td_msg = str(other_hour+(day_delta-1)*24+24-self_hour) + " hours ahead of you" |
46 |
|
47 |
else: |
48 |
td_msg = str(self_hour+(day_delta-1)*24+24-other_hour) + " hours behind you" |
49 |
|
50 |
else: # If we get here, it must be the same day |
51 |
|
52 |
if other_hour > self_hour: |
53 |
td_msg = str(other_hour-self_hour)+ " hours ahead of you" |
54 |
|
55 |
elif self_hour > other_hour: |
56 |
td_msg = str(self_hour-other_hour)+ " hours behind you" |
57 |
|
58 |
else: # Hey buddy, we're in the same timezone! |
59 |
td_msg = "Same time as you" |
60 |
|
61 |
return td_msg |