3

我得到的答复是时间格式,例如

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

我怎样才能将上述时间转换为格式

"2012-01-01T10:30:00-05:00"
4

1 回答 1

3

这是我想出的,但是您的示例输入都与您的示例输出不匹配,所以我不确定这里是否存在时区偏移错误。

#!/usr/bin/env python

import datetime

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

    dt = datetime.datetime.utcfromtimestamp(time + hours * 3600)
    return dt.strftime("%Y-%m-%dT%H:%M:%S") + '%02d:00' % hours

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

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

Windows 似乎不喜欢datetime.(utc)?fromtimestamp(). 可以要求它计算 Unix 纪元的负时间增量:

#!/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

    dt = EPOCH + datetime.timedelta(seconds=adjustedseconds)
    return dt.strftime("%Y-%m-%dT%H:%M:%S") + '%02d:00' % hours

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

print(parse_date(ScheduleDate))
print(parse_date(StartTime))
于 2013-08-04T04:29:32.643 回答