1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# -*- coding: utf-8 -*-
"""
Created on Wed Aug 22 10:57:16 2018
将形如‘xx分钟前’‘xx小时前’的时间信息转化为时间戳
@author: fangyucheng
"""
import re
import time
import datetime
def trans_strtime_to_timestamp(input_time, missing_year=False):
now = datetime.datetime.now()
real_time = None
input_time = input_time.replace('\n', '')
input_time = input_time.replace('\t', '')
def func_inyear(input_time):
try:
if '-' in input_time:
in_month = int(input_time.split('-')[0])
now = datetime.datetime.now()
now_month = now.month
now_year = now.year
if in_month > now_month:
in_year = now_year -1
elif in_month <= now_month:
in_year = now_year
else:
in_year = 0
return str(in_year)
except:
return 0
if '昨天' in input_time:
if len(input_time)>2:
try:
real_time_lst = re.findall('\d+', input_time)
mint = int(real_time_lst[0])
ss = int(real_time_lst[1])
now_day = datetime.datetime.now()
real_time_dt = datetime.datetime(now_day.year, now_day.month,
now_day.day-1, mint, ss)
real_time = int(datetime.datetime.timestamp(real_time_dt)*1000)
except:
real_time = int((time.time()-24*3600)*1e3)
else:
real_time = int((time.time()-24*3600)*1e3)
return real_time
if '前天' in input_time:
if len(input_time)>2:
try:
real_time_lst = re.findall('\d+', input_time)
mint = int(real_time_lst[0])
ss = int(real_time_lst[1])
now_day = datetime.datetime.now()
real_time_dt = datetime.datetime(now_day.year, now_day.month,
now_day.day-2, mint, ss)
real_time = int(datetime.datetime.timestamp(real_time_dt)*1000)
except:
real_time = int((time.time()-2*24*3600)*1e3)
else:
real_time = int((time.time()-2*24*3600)*1e3)
return real_time
if '分钟' in input_time:
real_time_lst = re.findall('\d+', input_time)
real_time_int = int(' '.join(real_time_lst))
real_time = int((time.time()-real_time_int*60)*1e3)
return real_time
if '小时' in input_time:
real_time_lst = re.findall('\d+', input_time)
real_time_int = int(' '.join(real_time_lst))
real_time = int((time.time()-real_time_int*3600)*1e3)
return real_time
if '天' in input_time:
real_time_lst = re.findall('\d+', input_time)
real_time_int = int(' '.join(real_time_lst))
real_time = int((time.time()-real_time_int*24*3600)*1e3)
return real_time
if ('月' in input_time or '月前' in input_time) and "日" not in input_time:
real_time_lst = re.findall('\d+', input_time)
real_time_int = int(' '.join(real_time_lst))
real_time = int((time.time()-real_time_int*30*24*3600)*1e3)
return real_time
if '月' in input_time and '日' in input_time and '年' not in input_time:
input_time_replace_chinese = str(now.year) + "-" + input_time.replace('月', '-').replace('日', '')
if len(input_time_replace_chinese) == 16:
real_time = int(datetime.datetime.strptime(input_time_replace_chinese,
'%Y-%m-%d %H:%M').timestamp()*1e3)
return real_time
elif len(input_time_replace_chinese) == 20:
real_time = int(datetime.datetime.strptime(input_time_replace_chinese,
'%Y-%m-%d %H:%M:%S').timestamp()*1e3)
return real_time
elif len(input_time_replace_chinese) == 8 or len(input_time_replace_chinese) == 9 or len(input_time_replace_chinese) == 10:
real_time = int(datetime.datetime.strptime(input_time_replace_chinese,
'%Y-%m-%d').timestamp()*1e3)
return real_time
if '年' in input_time and '月' in input_time and '日' in input_time:
input_time_replace_chinese = input_time.replace('年', '-').replace('月', '-').replace('日', '')
if len(input_time_replace_chinese) == 16:
real_time = int(datetime.datetime.strptime(input_time_replace_chinese,
'%Y-%m-%d %H:%M').timestamp()*1e3)
return real_time
elif len(input_time_replace_chinese) == 20:
real_time = int(datetime.datetime.strptime(input_time_replace_chinese,
'%Y-%m-%d %H:%M:%S').timestamp()*1e3)
return real_time
elif len(input_time_replace_chinese) == 8 or len(input_time_replace_chinese) == 9 or len(input_time_replace_chinese) == 10:
real_time = int(datetime.datetime.strptime(input_time_replace_chinese,
'%Y-%m-%d').timestamp()*1e3)
return real_time
length = len(input_time)
if real_time is None and length in [8, 9, 10]:
real_time = int(datetime.datetime.strptime(input_time, '%Y-%m-%d').timestamp()*1e3)
return real_time
if real_time is not None:
return ''
if missing_year == True and len(input_time) > 5:
year = func_inyear(input_time)
if year != str(0):
input_time = year + '-' + input_time
real_time = int(datetime.datetime.strptime(input_time,
'%Y-%m-%d').timestamp()*1e3)
else:
print('error in {input_time}'.format(input_time=input_time))
return real_time
#for str_time %Y-%m
if len(input_time) == 5 :
year = func_inyear(input_time)
if year != str(0):
input_time = year + '-' + input_time
real_time = int(datetime.datetime.strptime(input_time,
'%Y-%m-%d').timestamp()*1e3)
else:
print('error in {input_time}'.format(input_time=input_time))
return real_time
try:
return int(datetime.datetime.strptime(input_time,'%Y-%m-%d %H:%M:%S').timestamp()*1e3)
except:
pass
if real_time is None:
real_time = 0
print('unsuitable format %s' % input_time)
return real_time
def weibo_parse_time(publish_time):
publish_time = publish_time.split('来自')[0]
if '刚刚' in publish_time:
publish_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M')
elif '分钟' in publish_time:
minute = publish_time[:publish_time.find('分钟')]
minute = datetime.timedelta(minutes=int(minute))
publish_time = (datetime.datetime.now() -
minute).strftime('%Y-%m-%d %H:%M')
elif '今天' in publish_time:
today = datetime.datetime.now().strftime('%Y-%m-%d')
time = publish_time[3:]
publish_time = today + ' ' + time
elif '月' in publish_time:
year = datetime.datetime.now().strftime('%Y')
month = publish_time[0:2]
day = publish_time[3:5]
time = publish_time[7:12]
publish_time = year + '-' + month + '-' + day + ' ' + time
else:
publish_time = publish_time
return trans_strtime_to_timestamp(publish_time)
if __name__ == "__main__":
print(trans_strtime_to_timestamp("06-03"))