forked from ceorourke/cat-texts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper_functions.py
95 lines (67 loc) · 2.15 KB
/
helper_functions.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#########################Helper functions for Cat Texts#########################
from datetime import datetime
from pytz import timezone
import pytz
def parse_time(time):
"""Parse hours and minutes from time"""
index = 0
for char in time:
if char == ":":
break
index += 1
hour = int(time[:index])
minutes = int(time[index+1:])
times = []
times.append(hour)
times.append(minutes)
return times
def make_hour(hour):
"""Formats hour for proper time display"""
if len(str(hour)) < 2:
new_hour = "0"
new_hour += str(hour)
else:
new_hour = str(hour)
return new_hour
def make_minutes(minutes):
"""Formats minutes for proper time display"""
if len(str(minutes)) < 2:
new_minutes = "0"
new_minutes += str(minutes)
else:
new_minutes = str(minutes)
return new_minutes
def make_24_hour_time(ampm, hour):
"""Convert 12 hour time to 24 hour time"""
if ampm == "pm":
if hour == 12:
hour == 0
else:
hour += 12
return hour
def am_or_pm(hour):
"""Return whether the time is am or pm based on 24 hour time formatting"""
if hour > 12:
return "pm"
return "am"
def make_12_hour_time(hour):
"""Convert 24 hour time from db to 12 hour time for display"""
if hour == 0:
hour = 12
if hour > 12:
hour -= 12
return hour
def convert_to_utc(hour, minutes, user_timezone):
"""Take in user inputted time and create a UTC datetime object"""
date = datetime.now() # create a datetime object (in UTC time by default)
utc = pytz.utc
date = date.replace(tzinfo=utc) # add utc timezone info
# TODO get the users' timezone rather than hardcoding PST conversion
# this_timezone = timezone('US/Pacific')
this_timezone = timezone(user_timezone)
date = date.astimezone(this_timezone)
# change the hours and minutes to user input, clear seconds and microseconds
date = date.replace(hour=hour, minute=minutes, second=0, microsecond=0)
# change back to UTC to store in database
date = date.astimezone(utc)
return date