from datetime import datetime


def _timestamp_manager(timestamp, data):
    """Returns the timestamp.

    Parameters
    ----------
    timestamp : datetime.datetime
        datetime object

    data : dict
        A python dictionary, which is loaded JSON os the availability API.

    Return type:
        datetime.datetime

     If timestamp is not None then sets the value to timestamp itself.
     If timestamp is None the returns the value from the last fetched API data.
     If not timestamp and can not read the archived_snapshots form data return datetime.max
    """

    if timestamp:
        return timestamp

    if not data["archived_snapshots"]:
        return datetime.max

    return datetime.strptime(
        data["archived_snapshots"]["closest"]["timestamp"], "%Y%m%d%H%M%S"
    )