def test_timestamp_to_datetime(): # timestamp > MAX_INT assert timestamp_to_datetime(18446744071562142720) is None with app.app_context(): g.timezone = pytz.utc # test valid date assert timestamp_to_datetime(1493296245) == datetime.datetime(2017, 4, 27, 12, 30, 45, tzinfo=pytz.UTC) g.timezone = None # test valid date but no timezone assert timestamp_to_datetime(1493296245) is None
def _add_first_last_links(self, resp, params): soonest_departure_ts = min(j.departure_date_time for j in resp.journeys) soonest_departure = timestamp_to_datetime(soonest_departure_ts) if soonest_departure: soonest_departure = soonest_departure.replace(hour=0, minute=0, second=0) params['datetime'] = dt_to_str(soonest_departure) params['datetime_represents'] = 'departure' add_link(resp, rel='first', **params) tardiest_arrival_ts = max(j.arrival_date_time for j in resp.journeys) tardiest_arrival = timestamp_to_datetime(tardiest_arrival_ts) if tardiest_arrival: tardiest_arrival = tardiest_arrival.replace(hour=23, minute=59, second=59) params['datetime'] = dt_to_str(tardiest_arrival) params['datetime_represents'] = 'arrival' add_link(resp, rel='last', **params)
def _filter_passages(self, passages, count, from_dt, duration, timezone=None): """ after getting the next passages from the proxy, we might want to filter some by default we filter: * we keep at most 'count' items * we don't want to display datetime after from_dt """ if not passages: return passages if from_dt: # we need to convert from_dt (which is a timestamp) to a datetime from_dt = timestamp_to_datetime(from_dt, timezone) to_dt = from_dt + datetime.timedelta(seconds=duration) passages = [p for p in passages if from_dt <= p.datetime <= to_dt] if not passages: # if there was some passages and everything was filtered, # we return None to keep the base schedule return None if count: del passages[count:] return passages
def _filter_passages(self, passages, count, from_dt): """ after getting the next passages from the proxy, we might want to filter some by default we filter: * we keep at most 'count' items * we don't want to display datetime after from_dt """ if not passages: return passages if from_dt: # we need to convert from_dt (which is a timestamp) to a datetime from_dt = timestamp_to_datetime(from_dt) passages = filter(lambda p: p.datetime >= from_dt, passages) if not passages: # if there was some passages and everything was filtered, # we return None to keep the base schedule return None if count: del passages[count:] return passages