Exemple #1
0
    def __init__(self, config, sim_st: datetime, all_sampling_data, all_future_data):
        self.logger = logging.getLogger(self.__class__.__name__)

        self.config = config

        self.sim_root = self.config.sim_root + sim_st.date().isoformat() + "/" + sim_st.time().isoformat() + "/"

        self.params_root = self.config.params_output_root + sim_st.date().isoformat() + "/"
        pathlib.Path(self.params_root).mkdir(parents=True, exist_ok=True)

        sim_logs_dir = self.config.sim_logs_root + sim_st.date().isoformat() + "/"

        pathlib.Path(sim_logs_dir).mkdir(parents=True, exist_ok=True)

        self.sim_logs_path = sim_logs_dir + sim_st.time().isoformat() + ".log"

        self.sim_st = sim_st

        self.sampling_window_start_time = sim_st - timedelta(seconds=self.config.sampling_window)
        self.sampling_window_end_time = sim_st

        self.orderbook_window_start_time = sim_st - datetime.timedelta(seconds=self.config.orderbook_window)
        self.orderbook_window_end_time = sim_st

        self.all_sampling_data = all_sampling_data
        self.all_future_data = all_future_data

        self.graphing = Graphing(config, "Backtest @ " + sim_st.isoformat())
Exemple #2
0
def date_available(date_start: datetime, date_end: datetime,
                   hairdresser_id: str, customer_id: str) -> dict:
    """
    Checks if the given date is available. Expected date format:
    05/04/2020 12:00
    :param date_start: Start date of the visit as string
    :param date_end: End date of the visit as string
    :param hairdresser_id: id of the hairdresser that will conduct the service
    :param customer_id: id of the customer that will receive the service
    :return: Boolean stating whether the provided date is available for booking
    """
    for visit in get_hairdresser_visits_for_day(hairdresser_id,
                                                date_start.date()):
        if dates_collide(visit, date_start, date_end):
            return {
                "success": False,
                "hairdresser_taken": True,
                "customer_taken": False
            }

    for visit in get_customer_visits_for_day(customer_id, date_start.date()):
        if dates_collide(visit, date_start, date_end):
            return {
                "success": False,
                "hairdresser_taken": False,
                "customer_taken": True
            }
    return {
        "success": True,
        "hairdresser_taken": False,
        "customer_taken": False
    }
def get_all_time_entries(start_date: datetime, end_date: datetime, time_lines,
                         employee_id: int):
    """
    Get all the time logs of an employee from a given date range.
    :param start_date: Inclusive on the range
    :param end_date: Inclusive on the range
    :param time_lines: The lines of the .dat file that was read by file.readlines()
    :param employee_id: Employee id in integer.
    :return:
    """
    time_entries: [datetime] = []

    start_date = start_date.date()
    end_date = end_date.date()

    for line in time_lines:
        elems = line.split()
        id = int(elems[0])
        if id == employee_id:
            _date = elems[1]
            time = elems[2]
            _dt = _date + ' ' + time

            _entry = datetime.datetime.strptime(_dt, '%Y-%m-%d %H:%M:%S')

            if start_date <= datetime.datetime.strptime(
                    _date, '%Y-%m-%d').date() <= end_date:
                time_entries.append(_entry)

    return time_entries
def get_all_entries(start_date: datetime, end_date: datetime, time_lines,
                    employee_id: int):
    """
    Get all the logs of an employee from a given date range.
    :param start_date: Inclusive on the range
    :param end_date: Inclusive on the range
    :param time_lines: The lines of the .dat file that was read by file.readlines()
    :param employee_id: Employee id in integer.
    :return: List of log data
    """
    entries = []

    start_date = start_date.date()
    end_date = end_date.date()

    for line in time_lines:
        elems = line.split()
        id = int(elems[0])
        if id == employee_id:
            _date = elems[1]

            entry = datetime.datetime.strptime(_date, '%Y-%m-%d')
            entry = entry.date()

            if start_date <= entry <= end_date:
                entries.append(line)

    return entries
Exemple #5
0
def business_days(earlier_date: datetime, later_date: datetime):
    full_duration = later_date - earlier_date
    # pylint: disable=no-member
    bus_days = np.busday_count(earlier_date.date(), later_date.date()).item()
    duration = full_duration

    if full_duration.days == 2 and bus_days == 1:
        duration = full_duration - datetime.timedelta(days=(2))
    elif full_duration.days > bus_days:
        duration = full_duration - datetime.timedelta(
            days=(full_duration.days - bus_days))

    return duration
Exemple #6
0
def getNews(mkt:str,symbol:str,startDate:datetime,midDate:datetime):
    newsDf=pd.DataFrame()
    i=0
    while i<2:
        i+=1
        params = (
            ('_appver', '1.0'),
            ('page', i),
            ('symbol', mkt+symbol),
            ('n', '51'),
            ('_var', 'finance_news'),
            ('type', '2'),
            ('_', int(datetime.now().timestamp() * 10000)),
        )

        response = requests.get('https://proxy.finance.qq.com/ifzqgtimg/appstock/news/info/search',
                                headers={"user-agent": "Mozilla", 'Connection': 'close'}, params=params)
        news = (json.loads(response.text[len('finance_news='):])['data']['data'])
        df=pd.DataFrame(news)
        df['time'] = df['time'].astype('datetime64[ns]').dt.date
        newsDf=newsDf.append(df)
        if df['time'].values[-1]<startDate.date():
            break
    df1=newsDf[newsDf['time']<midDate]
    return len(newsDf)-len(df1)
Exemple #7
0
 def is_old_news(self, pubdate_str: str, specified_date: datetime) -> bool:
     if specified_date is None:
         return False
     date_format = '%a, %d %b %Y %H:%M:%S %z'
     pubdate = datetime.datetime.strptime(pubdate_str, date_format)
     # 指定した日付より後のニュースは最新ニュースとして扱う
     # 指定した日付よりも前のニュースは古いのでTrue
     return pubdate.date() < specified_date.date()
def is_market_open(given_date: datetime):
    # if given_date is weekend
    if given_date.date().weekday() > 4:
        return False
    # if given_date is a US holiday
    if given_date in US_HOLIDAYS:
        return False
    return True
Exemple #9
0
def generate_report(date_from: datetime = None, date_to: datetime = None):
    c = get_client()
    detectedIssues = failed_analyses = 0
    if date_from is not None:
        date_from = date_from.date()
        report_range = f"{date_from} to "
        report_range += "present" if date_to is None else f"{date_to.date()}"
    else:
        report_range = "All time"
    analyses = c.analysis_list(date_from=date_from, date_to=date_to)

    # Report title
    # click.echo("=========================================")
    click.echo(f"Analyses report: {report_range}")
    click.echo("=========================================")

    for analysis in analyses:
        if analysis.status is not finished:
            click.echo(
                f"*Cannot generate report for analysis {analysis.uuid}; analysis status: {analysis.status}*\n"
            )
            failed_analyses += 1
            continue
        resp = c.report(analysis.uuid)
        file_to_issue = defaultdict(list)
        formatted_date = analysis.submitted_at.strftime("%m/%d/%Y, %H:%M")
        for issue in resp.issues:
            source_locs = [
                loc.source_map.split(":") for loc in issue.locations
            ]
            source_locs = [(int(o), int(l), int(i)) for o, l, i in source_locs]
            for _, _, file_idx in source_locs:
                filename = resp.source_list[file_idx]
                file_to_issue[filename].append(
                    (issue.swc_id, issue.swc_title, issue.severity,
                     issue.description_short, formatted_date))
        detectedIssues += len(resp.issues)
        for filename, data in file_to_issue.items():
            click.echo(f"Report for **{filename}** | UUID: {analysis.uuid}\n")
            click.echo(
                tabulate(
                    data,
                    tablefmt="rst",
                    headers=(
                        "SWC ID",
                        "SWC Title",
                        "Severity",
                        "Short Description",
                        "Time Submitted",
                    ),
                ))
            click.echo("\n")
    click.echo("Summary:")
    click.echo("--------")
    click.echo(
        f"Total analyses: {analyses.total}; Detected issues: {detectedIssues}; Failed analyses: {failed_analyses}"
    )
Exemple #10
0
 def get_day_complete_task(
     cls, day: datetime = datetime.datetime.today()) -> list:
     """
     Получение выполненых задач в указаный день
     """
     try:
         return cls.databases.query(
             ORM.Task).filter_by(date_completed=day.date()).all()
     except sqlalchemy.exc.OperationalError:
         return []
def get_dates(start_date: datetime, end_date: datetime):
    """
    Get all dates in a range
    :param lines:
    :param start_date:
    :param end_date
    :return:
    """
    _dates = []

    start_date = start_date.date()
    end_date = end_date.date()

    _curr_date: datetime = start_date

    while _curr_date <= end_date:
        _dates.append(_curr_date)
        _curr_date = _curr_date + datetime.timedelta(days=1)

    return _dates
Exemple #12
0
    def get_rate_by_date(self, date: datetime) -> Rate:
        """Search for a participation rate by date

        Args:
            date (datetime): Datetime to search for

        Returns:
            Rate
        """
        for rate in self.rates:
            if rate.date.date() == date.date():
                return rate
Exemple #13
0
 def get_appointments_for_attendee_for_day(attendee: User, date: datetime):
     """
     Gets a list of Appointments for the selected user and date
     :param attendee: User selected
     :param date: Date selected
     :return: apts: list of appointments
     """
     apts = []
     for apt in attendee.get_appointments():
         if date.date() == apt.tstart.date() and not apt.is_in_past():
             apts += [apt]
     return apts
Exemple #14
0
 def endDate(self, v: datetime):
     if v == None:
         self._endDate = v
         return
     if v.date() != self.startDate.date():
         raise ValueError(
             "The endDate must be on the same day as the startDate")
     if v + self.pause < self.startDate:
         raise ValueError(
             f"The endDate ({v}) time has to be later then the startDate ({self.startDate}) time"
         )
     self._endDate = v
def check_if_any_collisions(person: int, service: int, date_time: datetime, client: int = None) -> \
        Union[bool, Dict[str, bool]]:
    current_reservations = Reservation.objects.filter(user=person,
                                                      date=date_time.date())
    service = Service.objects.get(id=service)
    duration = service.duration
    start_time = date_time.time()
    end_time = (datetime.datetime.combine(datetime.date.today(), start_time) +
                datetime.timedelta(minutes=duration)).time()
    time_range = [
        (reservation.start_time,
         (datetime.datetime.combine(datetime.date.today(),
                                    reservation.start_time) +
          datetime.timedelta(minutes=reservation.service.duration)).time())
        for reservation in current_reservations
    ]
    no_collisions_with_current_reservations = check_time_collisions(
        time_range, start_time, end_time)
    no_collisions_with_client_reservations = True
    if client is not None:
        client_reservations = Reservation.objects.filter(client=client,
                                                         date=date_time.date())
        time_range = [
            (reservation.start_time,
             (datetime.datetime.combine(datetime.date.today(),
                                        reservation.start_time) +
              datetime.timedelta(minutes=reservation.service.duration)).time())
            for reservation in client_reservations
        ]
        no_collisions_with_client_reservations = check_time_collisions(
            time_range, start_time, end_time)

    if no_collisions_with_current_reservations and no_collisions_with_client_reservations:
        return True
    else:
        return {
            'current_reservations': no_collisions_with_current_reservations,
            'client_reservations': no_collisions_with_client_reservations
        }
Exemple #16
0
def file_list(request, date: datetime = None):
    template_name = 'index.html'
    list_names_files = os.listdir(FILES_PATH)
    list_files = []
    for name_file in list_names_files:
        item = {}
        item['name'] = name_file
        # тут ниже тоже срабатывает метод to_url конвертера StrDatetimeConverter (подозрительно):
        item['ctime'] = datetime.datetime.fromtimestamp(
            os.stat(os.path.join(FILES_PATH, name_file)).st_ctime)
        item['mtime'] = datetime.datetime.fromtimestamp(
            os.stat(os.path.join(FILES_PATH, name_file)).st_mtime)
        list_files.append(item)
    if date:
        files = filter(lambda item: item['ctime'].date() == date.date(),
                       list_files)
        context_date = date.date()
    else:
        files = list_files
        context_date = None

    context = {'files': files, 'date': context_date}
    return render(request, template_name, context)
Exemple #17
0
def compute_rates(rates: dict, dt_in: datetime, dt_out: datetime) -> tuple:
    '''Return a trip's scale rate.'''

    delta = dt_out - dt_in
    exp = ""
    res = delta.days * (rates["24 hour rate"] + rates["Room rate"])
    exp += f"{delta.days} x 24 hour rate (incl. room rate)"

    if delta.seconds / 3600 > 10:
        res += rates["Over 10 hours"]
        exp += " + over 10 hours"

    elif delta.seconds / 3600 > 5:
        res += rates["Over 5 hours"]
        exp += " + Over 5 hours"

    if (delta.days == 0 and dt_out.date() != dt_in.date()) \
        or dt_out.date() - dt_in.date() > delta:
        res += rates["Room rate"]
        exp += " + Room rate"

    exp += "."

    return res, exp
Exemple #18
0
def get_quarter(date: datetime):
    """
    获取日期所在的季度及季度区间
    :param date: 日期,datetime
    :return: tuple(quarter_start_date, quarter_end_date, quarter_n)
    """
    if isinstance(date, datetime.datetime):
        date = date.date()
    qbegins = [datetime.date(date.year, month, 1) for month in (1, 4, 7, 10)]
    qends = [
        datetime.date(date.year, month,
                      calendar.monthrange(date.year, month)[1])
        for month in (3, 6, 9, 12)
    ]
    bidx = bisect.bisect(qbegins, date)
    return qbegins[bidx - 1], qends[bidx - 1], bidx
Exemple #19
0
def file_list(request, date: datetime = None):
    template_name = 'index.html'
    # Реализуйте алгоритм подготавливающий контекстные данные для шаблона по примеру:
    files = listdir(settings.FILES_PATH)
    parsedFiles = []
    if not date:
        for file in files:
            parsedFiles.append(createDict(file))
    else:
        for file in files:
            f_date = datetime.datetime.fromtimestamp(
                int(stat(f"{settings.FILES_PATH}/{file}").st_ctime)).date()
            if f_date == date.date():
                parsedFiles.append(createDict(file))
    context = {'files': parsedFiles, 'date': date}

    return render(request, template_name, context)
def check_if_available(person: int, service: int, date_time: datetime) -> bool:
    bool_list = []
    current_reservations = Reservation.objects.filter(user=person,
                                                      date=date_time.date())
    service = Service.objects.get(id=service)
    duration = service.duration
    start_time = date_time.time()
    end_time = date_time.time() + datetime.timedelta(minutes=duration)
    for reservation in current_reservations:
        reservation_end_time = reservation.start_time + datetime.timedelta(
            minutes=duration)
        bool_list.append(
            reservation.start_time <= start_time <= reservation_end_time
            or reservation.start_time <= end_time <= reservation_end_time)
    if bool_list:
        return True not in bool_list
    else:
        return True
Exemple #21
0
def is_date_in_busines_day(date_to_check: datetime) -> bool:
    """"
    Checks if a date is part of a business day. Weekends and ZA public holidays are not business days.
    @param date_to_check: The date to check
    @return: Whether to exclude the date or not
    """
    previous_day = date_to_check - datetime.timedelta(days=1)
    current_day_tuple = (date_to_check.day, date_to_check.month)
    previous_day_tuple = (previous_day.day, previous_day.month)
    if current_day_tuple in ZA_HOLIDAYS or (previous_day.weekday() == SUNDAY
                                            and previous_day_tuple
                                            in ZA_HOLIDAYS):
        return False
    if date_to_check.weekday() in (SATURDAY, SUNDAY):
        return False
    if date_to_check.date() in EASTER_HOLIDAYS:
        return False
    return True
Exemple #22
0
def time_ago(time_in: datetime) -> str:
    """ returns string saying how long ago the time on input was
    """
    now = datetime.datetime.utcnow()
    diff = now - time_in
    sec_diff = int(diff.total_seconds())

    rules = [
        # threshold in seconds, view function, suffix

        (120, lambda x: "", "<2 minutes"),
        (7200, lambda x: int(x / 60),  "minutes"),
        (172800, lambda x: int(x / 3600),  "hours"),
        (5184000, lambda x: int(x / 86400),  "days"),
        # we should provide timezone # but it doesn't really matter here
        (None, lambda x: time_in.date().isoformat(),  ""),

    ]

    for threshold, func, suffix in rules:
        if threshold is None or sec_diff < threshold:
            return "{} {}".format(func(sec_diff), suffix)
    else:
        raise RuntimeError("Unexpected error in time_ago filter,")
Exemple #23
0
def split(dt: _datetime) -> Tuple[_datetime.date, _datetime.time]:
    """Split a datetime into date and time components.  Useful over calling
    .date() and .time() methods, since that dumps tzinfo for the time component."""
    time_ = time(dt.hour, dt.minute, dt.second, dt.microsecond, dt.tzinfo)
    return dt.date(), time_
Exemple #24
0
def split(dt: _datetime) -> Tuple[_datetime.date, _datetime.time]:
    """Split a datetime into date and time components.  Useful over calling
    .date() and .time() methods, since that dumps tzinfo for the time component."""
    time_ = time(dt.hour, dt.minute, dt.second, dt.microsecond, dt.tzinfo)
    return dt.date(), time_
Exemple #25
0
    def get_response_for_near(self, datetime_object: datetime):
        weekday = datetime_object.weekday()
        week = self._get_week_number(datetime_object)

        week_days = self._get_week_days(week)

        next_week = 1 if week == 2 else 2
        next_week_days = self._get_week_days(next_week)

        for day in next_week_days:
            day.weekday += 7

        week_days += next_week_days

        next_days = []
        for offset in range(6):
            for day in week_days:
                if next_days:
                    next_days.append(day)

                elif day.weekday == weekday + offset:
                    next_days.append(day)

            if next_days:
                break

        if next_days:
            for day in next_days:
                for lesson in day.lessons:
                    start, _ = [
                        list(map(int, time.split(':')))
                        for time in lesson.time.split('-')
                    ]

                    date = datetime_object + datetime.timedelta(day.weekday -
                                                                weekday)

                    lesson_start = datetime.datetime(year=date.year,
                                                     month=date.month,
                                                     day=date.day,
                                                     hour=start[0],
                                                     minute=start[1])

                    if datetime_object > lesson_start:
                        continue

                    if datetime_object.date() == lesson_start.date():
                        minutes = (lesson_start -
                                   datetime_object).seconds // 60
                        hours = minutes // 60
                        minutes -= hours * 60

                        hours = '%s ч. ' % hours if hours else ''
                        minutes = '%s мин.' % minutes

                        added = 'сегодня через %s%s' % (hours, minutes)

                    else:
                        days = (lesson_start - datetime_object).days
                        added = 'через %s дн.' % days

                    response = 'Ближайшая пара *%s*\n\n' % added
                    response += self._get_response_for_lesson(lesson)
                    return self._group_label + response

        sad_emoji = EMOJI['sad']
        return self._group_label + 'Ближайшой пары найти *не удалось* %s' % sad_emoji
Exemple #26
0
def same_day(news_date: str, arg_date: datetime):
    """Check if news_date is the same day as arg_date"""
    return parse_date(news_date).date() == arg_date.date()
def get_data_for_timespan(start: datetime, end: datetime, product):
    mongo = MongoClient()
    db = mongo["coinbase_history"]
    c = db.get_collection("historical_data")
    data = []
    for d in c.find({'Product': product.id}):
        data.append(d)
    days = (end - start).days

    wanted_dates = []
    for i in range(0, days + 1):
        wanted_dates.append((start + datetime.timedelta(i)).timestamp())
    dates, last_date = [], start.timestamp()
    data.reverse()
    # TODO get biggest diff of data to check if data is missing
    wanted_index = 0
    for i in range(0, len(data)):
        try:
            # check if wanted_dates is more than a half day below and upgrade accordingly
            while fucking_date.fromtimestamp(
                    wanted_dates[wanted_index]) < fucking_date.fromtimestamp(
                        data[i]['Timestamp']) - datetime.timedelta(0.5):
                wanted_index += 1
            if abs((fucking_date.fromtimestamp(data[i]['Timestamp']) -
                    fucking_date.fromtimestamp(
                        wanted_dates[wanted_index])).total_seconds()) < 43200:
                dates.append(data[i])
                wanted_dates.pop(wanted_index)
        except Exception as e:
            pass
    # if fucking_date.fromtimestamp(dates[-1]['Timestamp']).date()==fucking_date.fromtimestamp(wanted_dates[0]).date():
    #     wanted_dates.pop(0)

    if len(wanted_dates) == 1 and fucking_date.fromtimestamp(wanted_dates[0]).date() == fucking_date.today().date() \
            or len(dates)>0 and (fucking_date.now().date() - fucking_date.fromtimestamp(dates[-1]['Timestamp']).date()).days == 1 \
            and end.date() == fucking_date.today().date():
        today = client.get_product_24hr_stats(product.id)
        parsed_today = {
            "Product":
            product.id,
            "Timestamp":
            datetime.datetime.combine(datetime.datetime.today(),
                                      datetime.time()).timestamp(),
            "Low":
            float(today['low']),
            "High":
            float(today['high']),
            "Open":
            float(today['open']),
            "Close":
            float(today['last']),
            "Volume":
            float(today['volume'])
        }
        dates.append(parsed_today)
        mongo.close()
        return dates
    elif len(wanted_dates) == 0:
        return dates
    else:
        for i in range(0, wanted_dates.__len__()):
            wanted_dates[i] = fucking_date.fromtimestamp(
                wanted_dates[i]).date()
        # TODO get still needed data from client
        data = []
        if wanted_dates[-1] == fucking_date.now().date():
            wanted_dates.pop(-1)
            data.append(client.get_product_24hr_stats(product.id))
        data.extend(
            client.get_product_historic_rates(product.id,
                                              start=wanted_dates[0],
                                              end=wanted_dates[-1],
                                              granularity=86400))
        data.reverse()
        try:
            db.create_collection(
                "historical_data", **{
                    "_id": [("Product", pymongo.ASCENDING),
                            ("Timestamp", pymongo.ASCENDING)]
                })
        except Exception:
            pass
        finally:
            db_coll = db["historical_data"]
            db_coll.create_index([("Product", pymongo.DESCENDING),
                                  ("Timestamp", pymongo.DESCENDING)],
                                 unique=True)
        for row in data:
            if 'low' in row:
                break
            row = {
                "Product": product.id,
                "Timestamp": row[0],
                "Low": row[1],
                "High": row[2],
                "Open": row[3],
                "Close": row[4],
                "Volume": row[5]
            }
            try:
                db_coll.update_one(
                    {
                        "Product": product.id,
                        "Timestamp": row["Timestamp"]
                    }, {"$set": row},
                    upsert=True)
            except Exception:
                print("Reminder to make a db system completely from scratch")

        temp_timestamps = []
        for temp_date in dates:
            temp_timestamps.append(temp_date['Timestamp'])
        for row in data:
            try:
                dates.append({
                    "Product": product.id,
                    "Timestamp": row[0],
                    "Low": row[1],
                    "High": row[2],
                    "Open": row[3],
                    "Close": row[4],
                    "Volume": row[5]
                })
            except Exception:
                try:
                    data[-1] = {
                        "Product":
                        product.id,
                        "Timestamp":
                        datetime.datetime.combine(datetime.datetime.today(),
                                                  datetime.time()).timestamp(),
                        "Low":
                        float(data[-1]['low']),
                        "High":
                        float(data[-1]['high']),
                        "Open":
                        float(data[-1]['open']),
                        "Close":
                        float(data[-1]['last']),
                        "Volume":
                        float(data[-1]['volume'])
                    }
                except:
                    pass
        try:
            mongo.close()
        except:
            pass
        dates.sort(key=lambda x: x['Timestamp'])
        return dates
    def _filter_stop_times_window(self, stop_times: list,
                                  window_start: datetime,
                                  window_end: datetime) -> list:
        """
        This method will only retain the stop times in the given time window
        :param stop_times: The list of stop times to filter.
        :param window_start: The start of the time window.
        :param window_end: End of the time window
        :return: The filtered list
        """
        logging.debug("Filtering stop times")

        # Check if the time window spans across midnight
        window_crosses_midnight = window_end.date() > window_start.date()
        # Calculate the seconds from midnight. This way we can do all later comparisons using integers
        window_start_secs_since_midnight = window_start.time().hour * 3600 \
                                           + window_start.time().minute * 60 \
                                           + window_start.time().second

        window_end_secs_since_midnight = window_end.time().hour * 3600 \
                                         + window_end.time().minute * 60 \
                                         + window_end.time().second

        # Get the day before the start date, needed to check if a trip that spans multiple days was active on this day.
        day_before_start = window_start.date() - timedelta(days=1)

        filtered_stop_times = list()
        for stop_time in stop_times:
            # We already calculated the seconds from midnight in the StopTimesCache.
            secs_since_midnight = stop_time['departure_seconds']
            # The first, easy, check is to see if the time lies between the start and end time.
            # If this fails, we can skip all other checks
            if not self._is_time_in_window(secs_since_midnight,
                                           window_start_secs_since_midnight,
                                           window_end_secs_since_midnight):
                continue

            # Alright, so the time is valid. Is the trip actually ran on that day? Get the service id so we can check
            trip = self._trips_cache.get_trip(stop_time['trip_id'])
            service_id = trip['service_id']

            # Get the hour part from the time. If it is more than 23, it is a trip that started the day before.
            hour_int = secs_since_midnight // 3600

            # This is a trip that started the same day
            if hour_int < 24:
                # If the window doesn't cross midnight, the departure date is the same as the date of the window start.
                # Check if the service_id is active that day
                if not window_crosses_midnight \
                        and self._calendar_dates_cache.is_serviced(service_id, window_start.date()):
                    filtered_stop_times.append(stop_time)
                # If it crosses midnight, we need to determine the departure date first
                elif window_crosses_midnight:
                    # We have constrained the time window to no more than 24h. This means that, if the time window
                    # crosses midnight, the end time will lie before the start time. This simplifies the following tests
                    if secs_since_midnight >= window_start_secs_since_midnight:
                        if self._calendar_dates_cache.is_serviced(
                                service_id, window_start.date()):
                            filtered_stop_times.append(stop_time)
                    else:
                        if self._calendar_dates_cache.is_serviced(
                                service_id, window_start.date()):
                            filtered_stop_times.append(stop_time)
            # This is a trip that started the day before (it's past midnight),
            # check if it was active on the day it started
            elif hour_int >= 24:
                if not window_crosses_midnight and self._calendar_dates_cache.is_serviced(
                        service_id, day_before_start):
                    filtered_stop_times.append(stop_time)
                # Alright, so the window crosses midnight and this trip started the day before.
                # Since this trip planner is restricted to 1-day intervals, we know the day before is start date
                elif window_crosses_midnight:
                    # We have constrained the time window to no more than 24h. This means that, if the time window
                    # crosses midnight, the end time will lie before the start time. This simplifies the following tests
                    # First day. Comparison corrects for the 24h offset since the hour part is larger than 24h
                    if secs_since_midnight - 86400 >= window_start_secs_since_midnight:
                        if self._calendar_dates_cache.is_serviced(
                                service_id, day_before_start):
                            filtered_stop_times.append(stop_time)
                    # Second day
                    else:
                        if self._calendar_dates_cache.is_serviced(
                                service_id, window_start.date()):
                            filtered_stop_times.append(stop_time)
        return filtered_stop_times
Exemple #29
0
def format_time_for_request(time_datetime: datetime) -> str:
    time = time_datetime.time().strftime('%H:%M:%S')
    date = time_datetime.date().strftime('%Y-%m-%d')
    request_string = date + 'T' + time
    return request_string