1 def getDateTime(time_str): 2 ‘‘‘ 3 转换时间 4 :param time_str: 5 :return: 6 ‘‘‘ 7 if not isinstance(time_str,unicode): 8 time_str = time_str.decode(‘utf-8‘) 9 10 time_now = datetime.datetime.now() 11 time_return = time_str 12 13 if u‘秒‘ in time_str: 14 reg = r‘(\d+)‘ 15 t_second = re.search(reg, time_str) 16 if t_second is not None: 17 t_second = t_second.group(1) 18 t_second = string.atoi(t_second) 19 t_second = datetime.timedelta(seconds=t_second) 20 time_return = time_now - t_second 21 time_return = datetime.datetime.strftime(time_return, ‘%Y-%m-%d %H:%M:%S‘) 22 return time_return 23 elif u‘分钟‘ in time_str: 24 reg = r‘(\d+)‘ 25 t_minute = re.search(reg, time_str) 26 if t_minute is not None: 27 t_minute = t_minute.group(1) 28 t_minute = string.atoi(t_minute) 29 t_minute = datetime.timedelta(minutes=t_minute) 30 time_return = time_now - t_minute 31 time_return = datetime.datetime.strftime(time_return, ‘%Y-%m-%d %H:%M:%S‘) 32 return time_return 33 elif u‘小时‘ in time_str: 34 reg = r‘(\d+)‘ 35 t_hour = re.search(reg, time_str) 36 if t_hour is not None: 37 t_hour = t_hour.group(1) 38 t_hour = string.atoi(t_hour) 39 t_hour = datetime.timedelta(hours=t_hour) 40 time_return = time_now - t_hour 41 time_return = datetime.datetime.strftime(time_return, ‘%Y-%m-%d %H:%M:%S‘) 42 return time_return 43 elif u‘今天‘ in time_str: 44 time_return = time_now.strftime(‘%Y-%m-%d %H:%M:%S‘) 45 return time_return 46 elif u‘天前‘ in time_str: 47 reg = r‘(\d+)‘ 48 t_day = re.search(reg, time_str) 49 if t_day is not None: 50 t_day = t_day.group(1) 51 t_day = string.atoi(t_day) 52 t_day = datetime.timedelta(days=t_day) 53 time_return = time_now - t_day 54 time_return = datetime.datetime.strftime(time_return, ‘%Y-%m-%d %H:%M:%S‘) 55 return time_return 56 elif u‘周‘ in time_str: 57 reg = u‘(\d+)‘ 58 t_week = re.search(reg, time_str) 59 if t_week is not None: 60 t_week = t_week.group(1) 61 t_week = string.atoi(t_week) 62 t_week = datetime.timedelta(weeks=t_week) 63 time_return = time_now - t_week 64 time_return = datetime.datetime.strftime(time_return, ‘%Y-%m-%d %H:%M:%S‘) 65 return time_return 66 elif u‘月‘ in time_str: 67 reg = r‘(\d+)‘ 68 t_num = re.search(reg, time_str) 69 if t_num is not None: 70 t_num = int(t_num.group(1)) 71 date_to = time_now.month - t_num 72 # 判断是否跨年 73 if date_to == 0: 74 month_to = 12 75 year_to = -1 76 else: 77 month_to = date_to % 12 78 year_to = (date_to) / 12 79 # 如果是最后一天31日,注意其他月份没有31日 80 now_year = time_now.year+year_to 81 d = calendar.monthrange(now_year,month_to) 82 now_day = time_now.day 83 if now_day > d[1]: 84 now_day = d[1] 85 date_from = datetime.datetime(now_year,month_to,now_day,time_now.hour,time_now.minute,time_now.second) 86 time_return = datetime.datetime.strftime(date_from, ‘%Y-%m-%d %H:%M:%S‘) 87 return time_return 88 elif u‘年‘ in time_str: 89 reg = u‘(\d+)‘ 90 t_year = re.search(reg, time_str) 91 if t_year is not None: 92 t_year = int(t_year.group(1)) 93 date_from = datetime.datetime(time_now.year-t_year,time_now.month,time_now.day,time_now.hour,time_now.minute,time_now.second) 94 time_return = datetime.datetime.strftime(date_from, ‘%Y-%m-%d %H:%M:%S‘) 95 return time_return 96 else: 97 # time_return = time_str 98 int_year = time_now.year 99 str_year = str(int_year) 100 if time_str.find(str_year) < 0: 101 time_return = str_year + ‘-‘ + time_str 102 t_count = time_str.count(‘:‘) 103 if t_count == 1: 104 time_return += ‘:00‘ 105 elif t_count == 0: 106 time_return += ‘ 00:00:00‘ 107 108 # 如果日期大于当前日期,则年份减1 109 r_date = datetime.datetime.strptime(time_return, ‘%Y-%m-%d %H:%M:%S‘) 110 if r_date > time_now: 111 int_year_1 = int_year - 1 112 time_return = time_return.replace(str_year, str(int_year_1)) 113 114 return time_return 115 116 if __name__ == ‘__main__‘: 117 print getDateTime(‘1周前‘)
时间: 2024-11-03 17:14:11