date_format.py
1.14 KB
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
#!/usr/bin/env python
# @auther : Ylei
# @date : 2019/7/12
# @File : date_format.py
# @Software : PyCharm
from datetime import datetime, timedelta
time_str = "2019-06-26 08:41:41.572"
#time_str = "20190910045354"
def strtime_format_nomsec(strtime):
strtime = utc_time_transform(strtime)
return ("".join(strtime.split()[0].split("-")) \
+ ''.join(strtime.split()[1].split('.')[0].split(':')))
def strtime_format_msec(strtime):
strtime = utc_time_transform(strtime)
return ("".join(strtime.split()[0].split("-")) \
+ "".join(strtime.split()[1].split(":"))).replace('.', '')
def utc_time_transform(strtime):
strtime_to_datetime = datetime.strptime(strtime, '%Y-%m-%d %H:%M:%S.%f')
format_time = str((strtime_to_datetime + timedelta(hours=8)))
if '.' not in format_time:
format_time = format_time + '.000000'
# return str((strtime_to_datetime + timedelta(hours=8)))[:-3]
return format_time[:-3]
if __name__ == '__main__':
print(strtime_format_msec(time_str))
print(strtime_format_nomsec(time_str))
#print(type(str(utc_time_transform(time_str))))