通过API获得jira 备注(comment)的更新时间,发现获得的UTC时间格式为
2018-08-14T02:52:22.216+0000
这个时候,如果需要转换成datetime :yyyy-MM-dd'T'HH:mm:ss
格式,
或者date:yyyy-MM-dd
,可以用下面的代码:
#!/usr/bin/python
# -*- coding:utf-8 -*-
import re
import time
from datetime import datetime
def utc2local(utc_st):
# UTC时间转本地时间(+8:00)
now_stamp = time.time()
local_time = datetime.fromtimestamp(now_stamp)
utc_time = datetime.utcfromtimestamp(now_stamp)
offset = local_time - utc_time
local_st = utc_st + offset
return local_st
def local2utc(local_st):
# 本地时间转UTC时间(-8:00)
time_struct = time.mktime(local_st.timetuple())
utc_st = datetime.utcfromtimestamp(time_struct)
return utc_st
def utc_format(utcstr):
"""
将不标准的utc时间字符串转换成datetime格式
:param utcstr: 不标准的utc字符串,如‘2018-04-23T03:43:35.000+0000’
:return:
"""
utcstr = re.sub('.ddd+0000', '', utcstr, count=1)
try:
UTC_FORMAT = "%Y-%m-%dT%H:%M:%S"
d_time = datetime.strptime(utcstr, UTC_FORMAT)
except Exception:
UTC_FORMAT = "%Y-%m-%d %H:%M:%S"
d_time = datetime.strptime(utcstr, UTC_FORMAT)
return d_time
def get_local_date(utcstr):
"""
将jira返回的str类型的时间,转换成datetime格式,并去除日期之后的内容(提供给SprintDailyIssuesBar使用的日期处理接口)
:param utcstr: jira返回的time字符串
:return:datetime格式
"""
utc_st = utc_format(utcstr)
# print utc_st
local_st = utc2local(utc_st)
# print local_st
local_str = local_st.strftime('%Y-%m-%d')
local_date = datetime.strptime(local_str, "%Y-%m-%d")
# print local_date
return local_date
def get_local_datetime_str(utcstr):
"""
将jira返回的str类型的UTC时间,转换成str类型的local time,包含具体的时、分、秒(提供给tableData使用,转换时间)
:param utcstr: jira返回的time字符串
:return:string
"""
utc_st = utc_format(utcstr)
# print utc_st
local_st = utc2local(utc_st)
# print local_st
local_str = local_st.strftime('%Y-%m-%d %H:%M:%S')
return local_str
if __name__ == '__main__':
utcstr1 = '2018-08-14T02:52:22.216+0000'
tt=get_local_datetime_str(utcstr1)
print(tt)