如何在python中转换1970年之前的日期

如何在python中转换1970年之前的日期,python,date,robotframework,Python,Date,Robotframework,我编写了一个处理mongodb结果的方法,其中日期为datetime.datetime()我使用了dumps方法,该方法将日期转换为非毫秒,如果日期在1970年之前,则日期转换为负值,我无法处理此问题以将日期和时间更改回之后的日期和时间 我的示例代码如下: import datetime from bson.json_util import dumps from bson.objectid import ObjectId import string def get_json_key_value

我编写了一个处理mongodb结果的方法,其中日期为
datetime.datetime()
我使用了
dumps
方法,该方法将日期转换为非毫秒,如果日期在1970年之前,则日期转换为负值,我无法处理此问题以将日期和时间更改回之后的日期和时间

我的示例代码如下:

import datetime
from bson.json_util import dumps
from bson.objectid import ObjectId
import string

def get_json_key_value(mongoResult, key):

    # converts mongoResult which is not into proper json format to proper json format
    result = dumps(dict(eval(mongoResult)))

    # convert unicode format to string format
    data = __convert(dict(eval(result)))

    # code to get the json value.
    value="data"

    jsonlist = key.split('.')

    for eachKey in jsonlist:
        if (eachKey.isdigit()):
            value = value + "["+eachKey+ "]"

        else:
            value = value + "['"+eachKey + "']"

    returnValue = eval(value)
    #processing the date value, if key has $date
    if((string.find(key,"$date"))>=0):
        returnValue = datetime.datetime.fromtimestamp(returnValue/1000.0)

        returnValue = datetime.datetime.strftime(returnValue , format="%Y-%m-%dT%H:%M:%S")
        returnValue = datetime.datetime.strptime(str(returnValue), "%Y-%m-%dT%H:%M:%S")

        returnValue = returnValue - datetime.timedelta(minutes=330)
        returnValue = datetime.datetime.strftime(returnValue , format="%Y-%m-%dT%H:%M:%S")

    return returnValue
示例输入数据可以如下所示:

MongoResult: {'profileDetails': {'basicDetails': {'dateOfBirth': {'$date': -414892800000L}, 'customerCode': 'C017145'}, 'xDirLevel': {'masterCode': 1}, 'createdDate': {'$date': 1467392480962L}}, '_id': {'$oid': '58872e98321a0c863329199d'}}
Key: profileDetails.basicDetails.dateOfBirth.$date

我得到错误
ValueError:platform localtime()/gmtime()函数的时间戳超出范围
如果日期在1970年之前,如何处理此问题

我得到了1970年之前日期转换的解决方案,如下所示:

import datetime
from bson.json_util import dumps
from bson.objectid import ObjectId
import string

def get_json_key_value(mongoResult, key):

    # converts mongoResult which is not into proper json format to proper json format
    result = dumps(dict(eval(mongoResult)))

    # convert unicode format to string format
    data = __convert(dict(eval(result)))

    # code to get the json value.
    value="data"

    jsonlist = key.split('.')

    for eachKey in jsonlist:
        if (eachKey.isdigit()):
            value = value + "["+eachKey+ "]"

        else:
            value = value + "['"+eachKey + "']"

    returnValue = eval(value)
    #processing the date value, if key has $date
    if((string.find(key,"$date"))>=0):
        returnValue = datetime.datetime.fromtimestamp(returnValue/1000.0)

        returnValue = datetime.datetime.strftime(returnValue , format="%Y-%m-%dT%H:%M:%S")
        returnValue = datetime.datetime.strptime(str(returnValue), "%Y-%m-%dT%H:%M:%S")

        returnValue = returnValue - datetime.timedelta(minutes=330)
        returnValue = datetime.datetime.strftime(returnValue , format="%Y-%m-%dT%H:%M:%S")

    return returnValue
如果((string.find(key,$date))>=0,则需要替换
中的代码:
block as

 ndate = datetime.datetime(1970, 1, 1) + datetime.timedelta(seconds=(returnValue)/1000)
returnValue  = str(ndate).replace(' ','T')
我从stackoverflow中的另一个问题得到了答案