from datetime import datetime


def convert_datetime(value):
    """Convert a date and time to a :class:`~datetime.datetime` object.

    Parameters
    ----------
    value : :class:`bytes`
        The datetime value from an SQLite database.

    Returns
    -------
    :class:`datetime.datetime`
        The `value` as a datetime object.
    """
    try:
        # datetime.fromisoformat is available in Python 3.7+
        return datetime.fromisoformat(value.decode())
    except AttributeError:
        # mimics the sqlite3.dbapi2.convert_timestamp function
        datepart, timepart = value[:10], value[11:]
        year, month, day = map(int, datepart.split(b'-'))
        timepart_full = timepart.split(b'.')
        hours, minutes, seconds = map(int, timepart_full[0].split(b':'))
        if len(timepart_full) == 2:
            microseconds = int('{:0<6.6}'.format(timepart_full[1].decode()))
        else:
            microseconds = 0
        return datetime(year, month, day, hours, minutes, seconds, microseconds)