python 时间转换

时间:2023-01-10 23:04:47
 def getDateTime(time_str):
'''
转换时间
:param time_str:
:return:
'''
if not isinstance(time_str,unicode):
time_str = time_str.decode('utf-8') time_now = datetime.datetime.now()
time_return = time_str if u'秒' in time_str:
reg = r'(\d+)'
t_second = re.search(reg, time_str)
if t_second is not None:
t_second = t_second.group(1)
t_second = string.atoi(t_second)
t_second = datetime.timedelta(seconds=t_second)
time_return = time_now - t_second
time_return = datetime.datetime.strftime(time_return, '%Y-%m-%d %H:%M:%S')
return time_return
elif u'分钟' in time_str:
reg = r'(\d+)'
t_minute = re.search(reg, time_str)
if t_minute is not None:
t_minute = t_minute.group(1)
t_minute = string.atoi(t_minute)
t_minute = datetime.timedelta(minutes=t_minute)
time_return = time_now - t_minute
time_return = datetime.datetime.strftime(time_return, '%Y-%m-%d %H:%M:%S')
return time_return
elif u'小时' in time_str:
reg = r'(\d+)'
t_hour = re.search(reg, time_str)
if t_hour is not None:
t_hour = t_hour.group(1)
t_hour = string.atoi(t_hour)
t_hour = datetime.timedelta(hours=t_hour)
time_return = time_now - t_hour
time_return = datetime.datetime.strftime(time_return, '%Y-%m-%d %H:%M:%S')
return time_return
elif u'今天' in time_str:
time_return = time_now.strftime('%Y-%m-%d %H:%M:%S')
return time_return
elif u'天前' in time_str:
reg = r'(\d+)'
t_day = re.search(reg, time_str)
if t_day is not None:
t_day = t_day.group(1)
t_day = string.atoi(t_day)
t_day = datetime.timedelta(days=t_day)
time_return = time_now - t_day
time_return = datetime.datetime.strftime(time_return, '%Y-%m-%d %H:%M:%S')
return time_return
elif u'周' in time_str:
reg = u'(\d+)'
t_week = re.search(reg, time_str)
if t_week is not None:
t_week = t_week.group(1)
t_week = string.atoi(t_week)
t_week = datetime.timedelta(weeks=t_week)
time_return = time_now - t_week
time_return = datetime.datetime.strftime(time_return, '%Y-%m-%d %H:%M:%S')
return time_return
elif u'月' in time_str:
reg = r'(\d+)'
t_num = re.search(reg, time_str)
if t_num is not None:
t_num = int(t_num.group(1))
date_to = time_now.month - t_num
# 判断是否跨年
if date_to == 0:
month_to = 12
year_to = -1
else:
month_to = date_to % 12
year_to = (date_to) / 12
# 如果是最后一天31日,注意其他月份没有31日
now_year = time_now.year+year_to
d = calendar.monthrange(now_year,month_to)
now_day = time_now.day
if now_day > d[1]:
now_day = d[1]
date_from = datetime.datetime(now_year,month_to,now_day,time_now.hour,time_now.minute,time_now.second)
time_return = datetime.datetime.strftime(date_from, '%Y-%m-%d %H:%M:%S')
return time_return
elif u'年' in time_str:
reg = u'(\d+)'
t_year = re.search(reg, time_str)
if t_year is not None:
t_year = int(t_year.group(1))
date_from = datetime.datetime(time_now.year-t_year,time_now.month,time_now.day,time_now.hour,time_now.minute,time_now.second)
time_return = datetime.datetime.strftime(date_from, '%Y-%m-%d %H:%M:%S')
return time_return
else:
# time_return = time_str
int_year = time_now.year
str_year = str(int_year)
if time_str.find(str_year) < 0:
time_return = str_year + '-' + time_str
t_count = time_str.count(':')
if t_count == 1:
time_return += ':00'
elif t_count == 0:
time_return += ' 00:00:00' # 如果日期大于当前日期,则年份减1
r_date = datetime.datetime.strptime(time_return, '%Y-%m-%d %H:%M:%S')
if r_date > time_now:
int_year_1 = int_year - 1
time_return = time_return.replace(str_year, str(int_year_1)) return time_return if __name__ == '__main__':
print getDateTime('1周前')