<p>[替代答案]</p>
<p>这是python2.x版本。对于python3.x,只需要更改测试部分中的print语句</p>
<p>据我通过谷歌搜索可以确定:主要成分是自1970年1月1日以来的毫秒数。可能是阴性。正数不需要<code>+</code>符号。后面可以跟UTC的可选偏移量,它由5个字符组成:一个强制符号(<code>+</code>或<code>-</code>),2个数字表示小时,2个数字表示分钟。以上所有内容前加“/Date”(“后接“)/”。在</p>
<p>此答案提供了一个函数来转换JSON.NET网站一个Python<code>datetime.datetime</code>(timestamp)对象的字符串,以及两个函数,分别返回被截断为秒和毫秒的ISO格式。在</p>
<p>脚本:</p>
<pre><code># /Date(1154970000000+0700)/
# 0123456............7654321
# without timezone:
# /Date(1154970000000)/
# 0123456............21
# dodgy case
# /Date(-1234)/
# 3210987654321
import datetime
def json_date_as_datetime(jd):
sign = jd[-7]
if sign not in '-+' or len(jd) == 13:
millisecs = int(jd[6:-2])
else:
millisecs = int(jd[6:-7])
hh = int(jd[-7:-4])
mm = int(jd[-4:-2])
if sign == '-': mm = -mm
millisecs += (hh * 60 + mm) * 60000
return datetime.datetime(1970, 1, 1) \
+ datetime.timedelta(microseconds=millisecs * 1000)
def datetime_as_iso(dt):
return dt.strftime("%Y-%m-%dT%H:%M:%SZ") # truncates
def datetime_as_iso_ms(dt): # with millisecs as fraction
return dt.strftime("%Y-%m-%dT%H:%M:%S.%%03dZ") \
% (dt.microsecond // 1000) # truncate
if __name__ == "__main__":
tests = """\
/Date(1154970000000+0700)/
/Date(-1234)/
/Date(1000+0200)/
/Date(0+0000)/
/Date(0)/
/Date(0-0700)/
/Date(0-0730)/
/Date(0-0030)/
/Date(-1577923200000+0000)/
/Date(1)/
/Date(499)/
/Date(500)/
/Date(501)/
/Date(999)/
/Date(1000)/
/Date(-1)/
""".splitlines()
for test in tests:
test = test.strip()
if not test: continue
d = json_date_as_datetime(test)
print datetime_as_iso_ms(d), test
</code></pre>
<p>输出:</p>
^{pr2}$