import datetime
import time


def get_current_timestamp():
    return int(time.time() * 1000)


def get_time_stamp(date_str):
    try:
        if date_str.find('小时') > 0:
            i = date_str.find('小')
            t = float(date_str[0:i])
            return int((time.time() - t * 60 * 60) * 1000)
        elif date_str.find('分钟') > 0:
            i = date_str.find('分')
            t = float(date_str[0:i])
            return int((time.time() - t * 60) * 1000)
        elif date_str.find('昨天') >= 0:
            i = date_str.find('天') + 2
            timeStr = date_str[i:]
            h = int(timeStr.split(':')[0])
            m = int(timeStr.split(':')[1])
            localTime = time.localtime(
                time.time() - 24 * 60 * 60)  # 当前时间减去24小时

            return int(time.mktime((localTime[0], localTime[1], localTime[2], h, m, localTime[5], localTime[6],
                                    localTime[7], localTime[8])) * 1000)
        elif date_str.find('天') >= 0:
            day = int(date_str[0:date_str.find('天')])
            t = time.time()
            return int(time.time() * 1000 - day * 24 * 60 * 60 * 1000)
        elif date_str.find('-') > 1:
            if len(date_str.split('-')) == 2:
                month = int(date_str.split('-')[0])
                day = int(date_str.split('-')[1])
                localTime = time.localtime(time.time())

                return int(time.mktime((localTime[0], month, day, localTime[3], localTime[4], localTime[5],
                                        localTime[6], localTime[7], localTime[8])) * 1000)
            else:
                year = int(date_str.split('-')[0])
                month = int(date_str.split('-')[1])
                day = int(date_str.split('-')[2])
                localTime = time.localtime(time.time())

                return int(time.mktime((year, month, day, localTime[3], localTime[4], localTime[5], localTime[6],
                                        localTime[7], localTime[8])) * 1000)
        elif date_str.find('年') > 1:
            year = int(date_str[0:date_str.find('年')])
            month = int(date_str[date_str.find('年') + 1:date_str.find('月')])
            day = int(date_str[date_str.find('月') + 1:date_str.find('日')])
            h, m = 0, 0
            localTime = time.localtime(time.time())
            try:
                t = date_str.split(' ')[1]
                h = int(t.split(':')[0])
                m = int(t.split(':')[1])
            except:
                h, m = 0, 0
                pass

            return int(time.mktime(
                (year, month, day, h, m, localTime[5], localTime[6], localTime[7], localTime[8])) * 1000)

        elif date_str.find('月') > 0:
            month = int(date_str[date_str.find('年') + 1:date_str.find('月')])
            day = int(date_str[date_str.find('月') + 1:date_str.find('日')])
            h, m = 0, 0
            localTime = time.localtime(time.time())
            try:
                t = date_str.split(' ')[1]
                h = int(t.split(':')[0])
                m = int(t.split(':')[1])
            except:
                h, m = 0, 0
                pass

            return int(time.mktime(
                (localTime[0], month, day, h, m, localTime[5], localTime[6], localTime[7], localTime[8])) * 1000)
        else:
            time_list = date_str.split(' ')
            year = time_list[-1]
            month = time_list[1]
            day = time_list[2]
            t = time_list[3]
            new_time = year + '-' + month + '-' + day + ' ' + t
            return int(time.mktime(time.strptime(new_time, '%Y-%b-%d %H:%M:%S')) * 1000)
    except Exception as e:
        print(f'时间转换失败：{repr(e)}')
        print(date_str)


def transfer_datetime(date_time_value):
    date_time_str = date_time_value.strftime("%Y-%m-%d %H:%M:%S")
    return date_time_str


if __name__ == '__main__':
    print(get_time_stamp("18小时"))
