(dt, local_time=True)
Get posix timestamp from datetime() object.

    if dt is without timezone, then local_time specifies
    whether it's UTC or local time.

    Returns seconds since epoch as float.

    >>> datetime_to_timestamp(parse_iso_timestamp("2005-06-01 15:00:59.5 +02"))
    1117630859.5
    >>> datetime_to_timestamp(datetime.fromtimestamp(1117630859.5, UTC))
    1117630859.5
    >>> datetime_to_timestamp(datetime.fromtimestamp(1117630859.5))
    1117630859.5
    >>> now = datetime.utcnow()
    >>> now2 = datetime.utcfromtimestamp(datetime_to_timestamp(now, False))
    >>> abs(now2.microsecond - now.microsecond) < 100
    True
    >>> now2 = now2.replace(microsecond = now.microsecond)
    >>> now == now2
    True
    >>> now = datetime.now()
    >>> now2 = datetime.fromtimestamp(datetime_to_timestamp(now))
    >>> abs(now2.microsecond - now.microsecond) < 100
    True
    >>> now2 = now2.replace(microsecond = now.microsecond)
    >>> now == now2
    True
Get posix timestamp from datetime() object.
Get posix timestamp from datetime () object .
def datetime_to_timestamp(dt, local_time=True):
    """Get posix timestamp from datetime() object.

    if dt is without timezone, then local_time specifies
    whether it's UTC or local time.

    Returns seconds since epoch as float.

    >>> datetime_to_timestamp(parse_iso_timestamp("2005-06-01 15:00:59.5 +02"))
    1117630859.5
    >>> datetime_to_timestamp(datetime.fromtimestamp(1117630859.5, UTC))
    1117630859.5
    >>> datetime_to_timestamp(datetime.fromtimestamp(1117630859.5))
    1117630859.5
    >>> now = datetime.utcnow()
    >>> now2 = datetime.utcfromtimestamp(datetime_to_timestamp(now, False))
    >>> abs(now2.microsecond - now.microsecond) < 100
    True
    >>> now2 = now2.replace(microsecond = now.microsecond)
    >>> now == now2
    True
    >>> now = datetime.now()
    >>> now2 = datetime.fromtimestamp(datetime_to_timestamp(now))
    >>> abs(now2.microsecond - now.microsecond) < 100
    True
    >>> now2 = now2.replace(microsecond = now.microsecond)
    >>> now == now2
    True
    """
    if dt.tzinfo:
        delta = dt - TZ_EPOCH
        return delta.total_seconds()
    elif local_time:
        s = time.mktime(dt.timetuple())
        return s + (dt.microsecond / 1000000.0)
    else:
        delta = dt - UTC_NOTZ_EPOCH
        return delta.total_seconds()
def datetime_to_timestamp ( dt , local_time = True ) : if dt . tzinfo : delta = dt - TZ_EPOCH return delta . total_seconds ( ) elif local_time : s = time . mktime ( dt . timetuple ( ) ) return s + ( dt . microsecond / 1000000.0 ) else : delta = dt - UTC_NOTZ_EPOCH return delta . total_seconds ( )