<s> from graphserver . core import ServiceCalendar <EOL> import pytz <EOL> from datetime import timedelta , datetime , time <EOL> from graphserver . util import TimeHelpers <EOL> def iter_dates ( startdate , enddate ) : <EOL> currdate = startdate <EOL> while currdate <= enddate : <EOL> yield currdate <EOL> currdate += timedelta ( <NUM_LIT:1> ) <EOL> def service_calendar_from_timezone ( gtfsdb , timezone_name ) : <EOL> MAX_CALENDAR_SIZE = <NUM_LIT> <EOL> sc_count = list ( gtfsdb . execute ( "<STR_LIT>" ) ) [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> if sc_count > MAX_CALENDAR_SIZE : <EOL> raise Exception ( "<STR_LIT>" % ( sc_count , MAX_CALENDAR_SIZE ) ) <EOL> timezone = pytz . timezone ( timezone_name ) <EOL> start_date , end_date = gtfsdb . date_range ( ) <EOL> cal = ServiceCalendar ( ) <EOL> for currdate in iter_dates ( start_date , end_date ) : <EOL> service_ids = [ x . encode ( '<STR_LIT:utf8>' ) for x in gtfsdb . service_periods ( currdate ) ] <EOL> currdate_start = datetime . combine ( currdate , time ( <NUM_LIT:0> ) ) <EOL> currdate_local_start = timezone . localize ( currdate_start ) <EOL> service_period_begins = timezone . normalize ( currdate_local_start ) <EOL> service_period_ends = timezone . normalize ( currdate_local_start + timedelta ( hours = <NUM_LIT> ) ) <EOL> cal . add_period ( TimeHelpers . datetime_to_unix ( service_period_begins ) , TimeHelpers . datetime_to_unix ( service_period_ends ) , service_ids ) <EOL> return cal </s>
