如何將帶有偏移量的字串時間從 UTC 轉換為 UTC unix?
uj5u.com熱心網友回復:
代碼:
from datetime import datetime, timezone, timedelta
def strtime_to_unix(str_time: str, utc_offset: int, format: str = '%d.%m.%Y %H:%M') -> int:
return int(datetime.strptime(str_time, format).replace(
tzinfo=timezone(timedelta(seconds=utc_offset * 60 * 60))).timestamp())
str_time = '27.06.2022 12:35'
print(strtime_to_unix(str_time, 0)) # 12:35 UTC -> 1656333300
print(strtime_to_unix(str_time, -4)) # 16:35 UTC -> 1656347700
print(strtime_to_unix(str_time, 3)) # 09:35 UTC -> 1656322500
輸出:
1656333300
1656347700
1656322500
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/497471.html
