[英]Convert string time with offset from UTC to UTC unix
如何將帶有偏移量的字符串時間從 UTC 轉換為 UTC unix?
代碼:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.