Skip to content

Commit

Permalink
Convert time to seconds since midnight, add duration key
Browse files Browse the repository at this point in the history
  • Loading branch information
kashav committed Apr 28, 2016
1 parent 21818f3 commit 0b4ae10
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 12 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,9 @@ uoftscrapers.Exams
"course_code": String
"period": String,
"date": String,
"start_time": String,
"end_time": String,
"start_time": Number,
"end_time": Number,
"duration": Number,
"sections": [{
"lecture_code": String,
"exam_section": String,
Expand Down
7 changes: 3 additions & 4 deletions uoftscrapers/scrapers/exams/utm.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def retrieve_exams(courses):

start, end = UTMExams.parse_time(data[1].split(': ')[1],
data[2].split(': ')[1], date)
duration = end - start

sections = [UTMExams.parse_sections(room.split(': ')[1])
for room in [x for x in data[3:] if 'Room:' in x]]
Expand All @@ -82,6 +83,7 @@ def retrieve_exams(courses):
('date', date),
('start_time', start),
('end_time', end),
('duration', duration),
('sections', [])
])

Expand Down Expand Up @@ -176,8 +178,5 @@ def parse_sections(room):
def parse_time(start, end, date):
def convert_time(t):
h, m, s = [int(x) for x in t.split(':')]
dt = datetime.strptime('%s %s %s %s' % (date, h, m, s),
'%Y-%m-%d %H %M %S')
return timezone('US/Eastern').localize(dt).isoformat()

return (h * 60 * 60) + (m * 60) + s
return convert_time(start), convert_time(end)
6 changes: 3 additions & 3 deletions uoftscrapers/scrapers/exams/utsc.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def scrape(location='.'):

date = data[1]
start, end = UTSCExams.parse_time(data[2], data[3], date)
duration = end - start

location_ = data[4]

Expand All @@ -51,6 +52,7 @@ def scrape(location='.'):
('date', date),
('start_time', start),
('end_time', end),
('duration', duration),
('sections', [])
])

Expand Down Expand Up @@ -130,7 +132,5 @@ def get_course_id(course_code, date):
def parse_time(start, end, date):
def convert_time(t):
h, m = [int(x) for x in t.split(':')]
dt = datetime.strptime('%s %s %s' % (date, h, m), '%Y-%m-%d %H %M')
return timezone('US/Eastern').localize(dt).isoformat()

return (h * 60 * 60) + (m * 60)
return convert_time(start), convert_time(end)
7 changes: 4 additions & 3 deletions uoftscrapers/scrapers/exams/utsg.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def scrape(location='.', year=None):

date = UTSGExams.parse_date(data[2], p[-2:]) or ''
start, end = UTSGExams.parse_time(data[3], date) or (0, 0)
duration = end - start

doc = OrderedDict([
('id', id_),
Expand All @@ -75,6 +76,7 @@ def scrape(location='.', year=None):
('date', date),
('start_time', start),
('end_time', end),
('duration', duration),
('sections', [])
])

Expand Down Expand Up @@ -141,9 +143,8 @@ def parse_time(time, d):
def convert_time(t, is_pm=False):
"""Convert time from `HH:MM` to an ISO 8601 datetime."""
h, m = [int(x) for x in t.split(':')]
h += 12 if is_pm else 0
dt = datetime.strptime('%s %s %s' % (d, h, m), '%Y-%m-%d %H %M')
return timezone('US/Eastern').localize(dt).isoformat()
h += 12 if is_pm and h != 12 else 0
return (h * 60 * 60) + (m * 60)

time = list(filter(None, time.replace('-', '').split(' ')))
if len(time) == 3:
Expand Down

0 comments on commit 0b4ae10

Please sign in to comment.