Encodes a Python datetime.datetime object as an ECMA-262 compliant
    datetime string.

def encode_datetime(o):
    """ Encodes a Python datetime.datetime object as an ECMA-262 compliant
    datetime string."""
    r = o.isoformat()
    if o.microsecond:
        r = r[:23] + r[26:]
    if r.endswith('+00:00'):
        r = r[:-6] + 'Z'
    return r