本地时间字符串转成UTC时间
def local_str_to_utc_datetime(local_str, formatter='%Y-%m-%d %H:%M:%S'):
u"""本地时间字符串转成UTC时间(datetime格式)。
:param local_str: 本地时间字符串
:param formatter: 字符串格式
:return: UTC时间(datetime格式)
:rtype: datetime
"""
if local_str is None:
return None
# 将本地时间字符串转成datetime类型
local_dt = datetime.strptime(local_str, formatter)
# 将本地时间(datetime类型)转成UTC时间(datetime类型)
return local_dt.replace(tzinfo=tz.gettz()).astimezone(
tz.gettz('UTC')).replace(tzinfo=None).strftime(formatter)
utc时间戳转本地时间字符串
def timestamp_to_time(time_stamp):
"""utc时间戳转本地时间字符串"""
# utc 时间转本地时间字符串
time_stamp_array = time.localtime(time_stamp)
return time.strftime(DATETIME_FMT, time_stamp_array)
UTC日期转utc时间戳
def times_to_timestamp(time_stamp):
"""utc日期转utc时间戳"""
return int(calendar.timegm(time.strptime(time_stamp, DATETIME_FMT)))