python 1970年以前 字符串转换时间戳

代码1

  

   from datetime import datetime
    result = '1970-01-01 07:00:00'
    time_1 = '1966-07-17 06:03:00'
    if time_1 > result:
        array = time.strptime(time_1, "%Y-%m-%d %H:%M:%S")
        publishTime = int(time.mktime(array)) * 1000
        print(publishTime)
    else:
        time_1_struct = datetime.strptime(time_1, "%Y-%m-%d %H:%M:%S")
        time_2_struct = datetime.strptime(result, "%Y-%m-%d %H:%M:%S")
        seconds = (time_2_struct - time_1_struct).seconds
        print('-' + str(seconds))

  代码2

  

from datetime import datetime
epoch = datetime(1970, 1, 1)
t = datetime(1956, 3, 2)
diff = t - epoch
print(diff.days * 24 * 3600 + diff.seconds)

 

posted @ 2023-04-12 16:52  淋哥  阅读(60)  评论(0编辑  收藏  举报