nntp/date.py
def datetimeobj(value, fmt=None):
    """Parse a datetime to a datetime object.

    Uses fast custom parsing for common datetime formats or the slow dateutil
    parser for other formats. This is a trade off between ease of use and speed
    and is very useful for fast parsing of timestamp strings whose format may
    standard but varied or unknown prior to parsing.

    Common formats include:
        1 Feb 2010 12:00:00 GMT
        Mon, 1 Feb 2010 22:00:00 +1000
        20100201120000
        1383470155 (seconds since epoch)

    See the other datetimeobj_*() functions for more details.

    Args:
        value: A string representing a datetime.

    Returns:
        A datetime object.
    """
    if fmt:
        return _datetimeobj_formats.get(fmt,
            lambda v: datetimeobj_fmt(v, fmt)
        )(value)

    l = len(value)

    if 19 <= l <= 24 and value[3] == " ":
        # '%d %b %Y %H:%M:%Sxxxx'
        try:
            return datetimeobj_d_b_Y_H_M_S(value)
        except (KeyError, ValueError):
            pass

    if 30 <= l <= 31:
        # '%a, %d %b %Y %H:%M:%S %z'
        try:
            return datetimeobj_a__d_b_Y_H_M_S_z(value)
        except (KeyError, ValueError):
            pass

    if l == 14:
        # '%Y%m%d%H%M%S'
        try:
            return datetimeobj_YmdHMS(value)
        except ValueError:
            pass

    # epoch timestamp
    try:
        return datetimeobj_epoch(value)
    except ValueError:
        pass

    # slow version
    return datetimeobj_any(value)
def datetimeobj(value, fmt=None):
    """Parse a datetime to a datetime object.

    Uses fast custom parsing for common datetime formats or the slow dateutil
    parser for other formats. This is a trade off between ease of use and speed
    and is very useful for fast parsing of timestamp strings whose format may
    standard but varied or unknown prior to parsing.

    Common formats include:
        1 Feb 2010 12:00:00 GMT
        Mon, 1 Feb 2010 22:00:00 +1000
        20100201120000
        1383470155 (seconds since epoch)

    See the other datetimeobj_*() functions for more details.

    Args:
        value: A string representing a datetime.

    Returns:
        A datetime object.
    """
    if fmt:
        return _datetimeobj_formats.get(fmt,
            lambda v: datetimeobj_fmt(v, fmt)
        )(value)

    l = len(value)

    if 19 <= l <= 24 and value[3] == " ":
        # '%d %b %Y %H:%M:%Sxxxx'
        try:
            return datetimeobj_d_b_Y_H_M_S(value)
        except (KeyError, ValueError):
            pass

    if 30 <= l <= 31:
        # '%a, %d %b %Y %H:%M:%S %z'
        try:
            return datetimeobj_a__d_b_Y_H_M_S_z(value)
        except (KeyError, ValueError):
            pass

    if l == 14:
        # '%Y%m%d%H%M%S'
        try:
            return datetimeobj_YmdHMS(value)
        except ValueError:
            pass

    # epoch timestamp
    try:
        return datetimeobj_epoch(value)
    except ValueError:
        pass

    # slow version
    return datetimeobj_any(value)
Parse a datetime to a datetime object.

    Uses fast custom parsing for common datetime formats or the slow dateutil
    parser for other formats. This is a trade off between ease of use and speed
    and is very useful for fast parsing of timestamp strings whose format may
    standard but varied or unknown prior to parsing.

    Common formats include:
        1 Feb 2010 12:00:00 GMT
        Mon, 1 Feb 2010 22:00:00 +1000
        20100201120000
        1383470155 (seconds since epoch)

    See the other datetimeobj_*() functions for more details.

    Args:
        value: A string representing a datetime.

    Returns:
        A datetime object.