def service_calendar_from_timezone(gtfsdb, timezone_name): timezone = pytz.timezone(timezone_name) # grab date, day service bounds start_date, end_date = gtfsdb.date_range() # init empty calendar cal = ServiceCalendar() # for each day in service range, inclusive for currdate in iter_dates(start_date, end_date): # get and encode in utf-8 the service_ids of all service periods running thos date service_ids = [ x.encode('utf8') for x in gtfsdb.service_periods(currdate) ] # figure datetime.datetime bounds of this service day currdate_start = datetime.combine(currdate, time(0)) currdate_local_start = timezone.localize(currdate_start) service_period_begins = timezone.normalize(currdate_local_start) service_period_ends = timezone.normalize(currdate_local_start + timedelta(hours=24)) # enter as entry in service calendar cal.add_period(TimeHelpers.datetime_to_unix(service_period_begins), TimeHelpers.datetime_to_unix(service_period_ends), service_ids) return cal
def gtfsdb_to_service_calendar(gtfsdb, agency_id=None): """Given gtfsdb and agency_id, returns graphserver.core.ServiceCalendar""" # grab pytz timezone by agency_id, via gtfsdb timezone_name = gtfsdb.agency_timezone_name( agency_id ) timezone = pytz.timezone( timezone_name ) # grab date, day service bounds start_date, end_date = gtfsdb.date_range() # init empty calendar cal = ServiceCalendar() # for each day in service range, inclusive for currdate in iter_dates(start_date, end_date): # get and encode in utf-8 the service_ids of all service periods running thos date service_ids = [x.encode('utf8') for x in gtfsdb.service_periods( currdate )] # figure datetime.datetime bounds of this service day currdate_start = datetime.combine(currdate, time(0)) currdate_local_start = timezone.localize(currdate_start) service_period_begins = timezone.normalize( currdate_local_start ) service_period_ends = timezone.normalize( currdate_local_start + timedelta(hours=24) ) # enter as entry in service calendar cal.add_period( TimeHelpers.datetime_to_unix(service_period_begins), TimeHelpers.datetime_to_unix(service_period_ends), service_ids ) return cal
def service_calendar_from_timezone(gtfsdb, timezone_name): MAX_CALENDAR_SIZE = 1024 sc_count = list(gtfsdb.execute( "SELECT DISTINCT count(*) FROM (SELECT service_id FROM calendar_dates UNION SELECT service_id FROM calendar )" ))[0][0] if sc_count > MAX_CALENDAR_SIZE: raise Exception( "Service period count %d is greater than the maximum of %d"%(sc_count, MAX_CALENDAR_SIZE) ) timezone = pytz.timezone( timezone_name ) # grab date, day service bounds start_date, end_date = gtfsdb.date_range() # init empty calendar cal = ServiceCalendar() # for each day in service range, inclusive for currdate in iter_dates(start_date, end_date): # get and encode in utf-8 the service_ids of all service periods running thos date service_ids = [x.encode('utf8') for x in gtfsdb.service_periods( currdate )] # figure datetime.datetime bounds of this service day currdate_start = datetime.combine(currdate, time(0)) currdate_local_start = timezone.localize(currdate_start) service_period_begins = timezone.normalize( currdate_local_start ) service_period_ends = timezone.normalize( currdate_local_start + timedelta(hours=24) ) # enter as entry in service calendar cal.add_period( TimeHelpers.datetime_to_unix(service_period_begins), TimeHelpers.datetime_to_unix(service_period_ends), service_ids ) return cal
def service_calendar_from_timezone(gtfsdb, timezone_name): MAX_CALENDAR_SIZE = 1024 sc_count = list(gtfsdb.execute( "SELECT DISTINCT count(num) FROM (SELECT service_id FROM gtfs_calendar_dates UNION SELECT service_id FROM gtfs_calendar ) AS num" ))[0][0] if sc_count > MAX_CALENDAR_SIZE: raise Exception( "Service period count %d is greater than the maximum of %d"%(sc_count, MAX_CALENDAR_SIZE) ) timezone = pytz.timezone( timezone_name ) # grab date, day service bounds start_date, end_date = gtfsdb.date_range() # init empty calendar cal = ServiceCalendar() # for each day in service range, inclusive for currdate in iter_dates(start_date, end_date): # get and encode in utf-8 the service_ids of all service periods running thos date service_ids = [x.encode('utf8') for x in gtfsdb.service_periods( currdate )] # figure datetime.datetime bounds of this service day currdate_start = datetime.combine(currdate, time(0)) currdate_local_start = timezone.localize(currdate_start) service_period_begins = timezone.normalize( currdate_local_start ) service_period_ends = timezone.normalize( currdate_local_start + timedelta(hours=24) ) # enter as entry in service calendar cal.add_period( TimeHelpers.datetime_to_unix(service_period_begins), TimeHelpers.datetime_to_unix(service_period_ends), service_ids ) return cal
def schedule_to_service_calendar(sched, agency_id): timezone = timezone_from_agency(sched, agency_id) day_start, day_end = day_bounds_from_sched(sched) startdate, enddate = [ datetime.datetime( *parse_date(x) ) for x in sched.GetDateRange() ] cal = ServiceCalendar() for currdate in iter_dates(startdate, enddate): local_dt = timezone.localize(currdate) service_ids = [x.encode("ascii") for x in get_service_ids( sched, currdate )] this_day_begins = timezone.normalize( local_dt + timedelta(seconds=day_start) ) this_day_ends = timezone.normalize( local_dt + timedelta(seconds=day_end) ) cal.add_period( TimeHelpers.datetime_to_unix(this_day_begins), TimeHelpers.datetime_to_unix(this_day_ends), service_ids ) return cal