Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/kubernetes/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Python反序列化使用DataContractJsonSerializer导出的JSON日期?_Python - Fatal编程技术网

如何使用Python反序列化使用DataContractJsonSerializer导出的JSON日期?

如何使用Python反序列化使用DataContractJsonSerializer导出的JSON日期?,python,Python,如何使用Python反序列化使用DataContractJsonSerializer导出的JSON日期 导出的日期看起来像/Date(1440051107000-0500)/,我需要用Python创建实时DateTime对象 有没有现成的代码可以这样做,或者我必须自己编写?我想你会在这个线程中找到答案: 希望能有帮助 编辑: 按照@SuperBiasedMan的建议,以下是链接提供的答案 显然,python的标准库中没有在datetime对象中强制转换此字符串的代码 但是,此代码将实现以下功能:

如何使用Python反序列化使用DataContractJsonSerializer导出的JSON日期

导出的日期看起来像
/Date(1440051107000-0500)/
,我需要用Python创建实时DateTime对象


有没有现成的代码可以这样做,或者我必须自己编写?

我想你会在这个线程中找到答案:

希望能有帮助

编辑: 按照@SuperBiasedMan的建议,以下是链接提供的答案

显然,python的标准库中没有在datetime对象中强制转换此字符串的代码

但是,此代码将实现以下功能:

#!/usr/bin/env python

import datetime

EPOCH = datetime.datetime.utcfromtimestamp(0)

def parse_date(datestring):
    timepart = datestring.split('(')[1].split(')')[0]
    milliseconds = int(timepart[:-5])
    hours = int(timepart[-5:]) / 100
    adjustedseconds = milliseconds / 1000 + hours * 3600

    return EPOCH + datetime.timedelta(seconds=adjustedseconds)

ScheduleDate = "\/Date(1374811200000-0400)\/"
StartTime = "\/Date(-2208931200000-0500)\/"

print(parse_date(ScheduleDate))
print(parse_date(StartTime))

我想你会在这个帖子里找到答案:

希望能有帮助

编辑: 按照@SuperBiasedMan的建议,以下是链接提供的答案

显然,python的标准库中没有在datetime对象中强制转换此字符串的代码

但是,此代码将实现以下功能:

#!/usr/bin/env python

import datetime

EPOCH = datetime.datetime.utcfromtimestamp(0)

def parse_date(datestring):
    timepart = datestring.split('(')[1].split(')')[0]
    milliseconds = int(timepart[:-5])
    hours = int(timepart[-5:]) / 100
    adjustedseconds = milliseconds / 1000 + hours * 3600

    return EPOCH + datetime.timedelta(seconds=adjustedseconds)

ScheduleDate = "\/Date(1374811200000-0400)\/"
StartTime = "\/Date(-2208931200000-0500)\/"

print(parse_date(ScheduleDate))
print(parse_date(StartTime))

虽然这在理论上可以回答问题,但在此处包含答案的基本部分,并提供链接供参考。虽然这在理论上可以回答问题,但在此处包含答案的基本部分,并提供链接供参考。