转换带有UTC偏移的字符串时间到UTC UNIX

发布于 2025-02-10 12:56:04 字数 34 浏览 1 评论 0原文

如何将UTC偏移量的字符串时间转换为UTC UNIX?

How to convert string time with offset from UTC to UTC unix?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

め可乐爱微笑 2025-02-17 12:56:04

代码:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文