from datetime import datetime


def str2datetime(timestamp, tz=False):
    """
    This function translates the time stamp into a datetime object.

    Args:
        timestamp (str): the time stamp from command `zdump -v`
        tz (bool): True if it's UTC TimeZone.

    Returns:
        time (datetime): the datetime object about the time stamp
        time_string (str): the formatted time stamp
    """
    time, time_string = None, timestamp.strip()

    # Fixed the problem that the program running this python code doesn't
    # has the corresponding TimeZone where strptime will raise ValueError.
    # So, we skip the `TimeZone`
    time_s = time_string.rsplit(None, 1)[0]
    time_f = "%a %b %d %H:%M:%S %Y"

    if tz:
        # In some version, `zdump` prints 'UT' instead of 'UTC'
        # 'UC' is an invalid TimeZone for function `strptime`
        time_s = time_s + " UTC"
        time_f = "%a %b %d %H:%M:%S %Y %Z"

    try:
        time = datetime.strptime(time_s, time_f)
    except ValueError:
        pass

    return time, time_string