如何将带有偏移量的字符串时间从 UTC 转换为 UTC unix?
Code:
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
Output:
1656333300
1656347700
1656322500
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.