Example #1
0
def to_timedelta(td):
    '''
    Convert td to a datetime.timedelta instance if it is not one
    already. Other acceptable input types:

    int, float: number of seconds

    str, unicode: stringified floating-point number of seconds
    '''
    if isinstance(td, (str, unicode)):
        td = float(_to_utf8(td))
    if isinstance(td, (int, float)):
        td = _datetime.timedelta(0, td)
    if not isinstance(td, _datetime.timedelta):
        raise TypeError('TypeError: to_timedelta() argument must be a datetime.timedelta instance, int, float, str, or unicode')
    return td
Example #2
0
def to_timedelta(td):
    '''
    Convert td to a datetime.timedelta instance if it is not one
    already. Other acceptable input types:

    int, float: number of seconds

    str, unicode: stringified floating-point number of seconds
    '''
    if isinstance(td, (str, unicode)):
        td = float(_to_utf8(td))
    if isinstance(td, (int, float)):
        td = _datetime.timedelta(0, td)
    if not isinstance(td, _datetime.timedelta):
        raise TypeError(
            'TypeError: to_timedelta() argument must be a datetime.timedelta instance, int, float, str, or unicode'
        )
    return td
Example #3
0
def to_datetime(dt):
    '''
    Convert dt to a datetime.datetime instance if it is not one
    already. Other acceptable input types:

    tuple or time.struct_time: UTC time tuple, as returned by time.gmtime()

    int or float: timestamp (seconds since the Epoch) as returned by
    time.time()

    str or unicode: UTC timestamp in the following ISO-8601-compatible
    format: "YYYY-mm-ddTHH:MM:SS+00:00" with the following fields:

        "YYYY": year

        "mm": month

        "dd": day

        "T": the letter "T"

        "HH": hours (24-hr)

        "MM": minutes

        "SS": seconds

        "+00:00": time zone indicator for UTC
    '''
    if isinstance(dt, (tuple, _time.struct_time)):
        dt = _time.strftime('%Y-%m-%dT%H:%M:%S+00:00', dt)
    if isinstance(dt, (str, unicode)):
        dt = _time.mktime(
            _time.strptime(_to_utf8(dt), '%Y-%m-%dT%H:%M:%S+00:00'))
        if isinstance(dt, (int, float)):
            dt = _datetime.datetime.fromtimestamp(dt)
    if isinstance(dt, (int, float)):
        dt = _datetime.datetime.utcfromtimestamp(dt)
    if not isinstance(dt, _datetime.datetime):
        raise TypeError(
            'TypeError: to_datetime() argument must be a datetime.datetime instance, tuple, time.struct_time, int, float, str, or unicode'
        )
    return dt
Example #4
0
def to_datetime(dt):
    '''
    Convert dt to a datetime.datetime instance if it is not one
    already. Other acceptable input types:

    tuple or time.struct_time: UTC time tuple, as returned by time.gmtime()

    int or float: timestamp (seconds since the Epoch) as returned by
    time.time()

    str or unicode: UTC timestamp in the following ISO-8601-compatible
    format: "YYYY-mm-ddTHH:MM:SS+00:00" with the following fields:

        "YYYY": year

        "mm": month

        "dd": day

        "T": the letter "T"

        "HH": hours (24-hr)

        "MM": minutes

        "SS": seconds

        "+00:00": time zone indicator for UTC
    '''
    if isinstance(dt, (tuple, _time.struct_time)):
        dt = _time.strftime('%Y-%m-%dT%H:%M:%S+00:00', dt)
    if isinstance(dt, (str, unicode)):
        dt = _time.mktime(_time.strptime(_to_utf8(dt),
                                         '%Y-%m-%dT%H:%M:%S+00:00'))
        if isinstance(dt, (int, float)):
            dt = _datetime.datetime.fromtimestamp(dt)
    if isinstance(dt, (int, float)):
        dt = _datetime.datetime.utcfromtimestamp(dt)
    if not isinstance(dt, _datetime.datetime):
        raise TypeError('TypeError: to_datetime() argument must be a datetime.datetime instance, tuple, time.struct_time, int, float, str, or unicode')
    return dt