date_format.py 1.14 KB
#!/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))))