Skip to content

Commit

Permalink
Merge pull request #61 from kshvmdn/master
Browse files Browse the repository at this point in the history
Cleaner timezone offsets
  • Loading branch information
qasim committed Apr 25, 2016
2 parents b7df532 + a35eeb7 commit 04e8d7a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 21 deletions.
8 changes: 4 additions & 4 deletions uoftscrapers/scrapers/exams/utm.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from bs4 import BeautifulSoup
from collections import OrderedDict
from datetime import datetime
import pytz
from pytz import timezone
import re


Expand Down Expand Up @@ -176,8 +176,8 @@ def parse_sections(room):
def parse_time(start, end, date):
def convert_time(t):
h, m, s = [int(x) for x in t.split(':')]
d = datetime.strptime('%s %s %s %s' % (date, h, m, s),
'%Y-%m-%d %H %M %S')
return d.replace(tzinfo=pytz.timezone('US/Eastern')).isoformat()
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 convert_time(start), convert_time(end)
7 changes: 4 additions & 3 deletions uoftscrapers/scrapers/exams/utsc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from bs4 import BeautifulSoup
from collections import OrderedDict
from datetime import datetime
import pytz
from pytz import timezone


class UTSCExams:
Expand Down Expand Up @@ -130,6 +130,7 @@ 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(':')]
d = datetime.strptime('%s %s %s' % (date, h, m), '%Y-%m-%d %H %M')
return d.replace(tzinfo=pytz.timezone('US/Eastern')).isoformat()
dt = datetime.strptime('%s %s %s' % (date, h, m), '%Y-%m-%d %H %M')
return timezone('US/Eastern').localize(dt).isoformat()

return convert_time(start), convert_time(end)
27 changes: 13 additions & 14 deletions uoftscrapers/scrapers/exams/utsg.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from ..utils import Scraper
from bs4 import BeautifulSoup
from collections import OrderedDict
from datetime import datetime, date
import pytz
from datetime import datetime
from pytz import timezone


class UTSGExams:
Expand Down Expand Up @@ -63,16 +63,16 @@ def scrape(location='.', year=None):

location_ = data[4]

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

doc = OrderedDict([
('id', id_),
('course_id', course_id),
('course_code', course_code),
('campus', 'UTSG'),
('period', p.upper()),
('date', date_),
('date', date),
('start_time', start),
('end_time', end),
('sections', [])
Expand Down Expand Up @@ -124,14 +124,14 @@ def parse_course_info(period, course_code):
return exam_id, course_id, course_code

@staticmethod
def parse_date(date_, year):
def parse_date(date, year):
"""Convert date of form `D DD MMM` to ISO 8601 format."""

date_ = date_.split(' ')
if len(date_) == 3:
day, date_, month = date_
date = date.split(' ')
if len(date) == 3:
day, date, month = date

return datetime.strptime('%s %s %s %s' % (day, date_, month, year),
return datetime.strptime('%s %s %s %s' % (day, date, month, year),
'%a %d %b %y').date().isoformat()

@staticmethod
Expand All @@ -142,9 +142,8 @@ 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

date_ = datetime.strptime('%s %s %s' % (d, h, m), '%Y-%m-%d %H %M')
return date_.replace(tzinfo=pytz.timezone('US/Eastern')).isoformat()
dt = datetime.strptime('%s %s %s' % (d, h, m), '%Y-%m-%d %H %M')
return timezone('US/Eastern').localize(dt).isoformat()

time = list(filter(None, time.replace('-', '').split(' ')))
if len(time) == 3:
Expand All @@ -156,7 +155,7 @@ def convert_time(t, is_pm=False):
@staticmethod
def get_exam_periods(year):
if not year:
year = date.today().year
year = datetime.today().year

periods = []
for m in ('dec', 'apr', 'june', 'aug'):
Expand Down

0 comments on commit 04e8d7a

Please sign in to comment.