from pathlib import Path
from typing import Tuple
from datetime import datetime


def parse_dates_from_filename(file: Path) -> Tuple[datetime, datetime]:
    """
    Extracts the start and end time from an existing file.

    :param file:

    :return: tuple of start and end time
    """
    split_names = file.stem.split("__")
    if len(split_names) < 4:
        raise ValueError(f"Cannot extract datetimes from file name: {file.name}")
    start_time = split_names[2]
    end_time = split_names[3].replace("_temp", "")
    start_time = datetime.strptime(start_time, "%Y_%m_%dT%H_%M_%SZ")
    end_time = datetime.strptime(end_time, "%Y_%m_%dT%H_%M_%SZ")
    return start_time, end_time