在python中获取GMT时间戳

发布于 2025-01-17 20:32:31 字数 597 浏览 0 评论 0原文

我需要获取GMT时间戳,开始一天时的时间戳,而在一天结束时,以下示例是今天的(2022-03-29):

Start of Day (Unix Timestamp): 1648512000 
End of Day (Unix Timestamp): 1648598399

您可以在

from datetime import datetime
start_date = datetime.today().strftime('%Y-%m-%d')
start_date += ' 00:00:00'
print(start_date)
timestamp = time.mktime(datetime.strptime(start_date, "%Y-%m-%d %H:%M:%S").timetuple())
print(timestamp)

2022-03-29 00:00:00
1648533600.0

​看到这不是我在等待的答案,时间戳是我当地的时间,但是我需要GMT时间戳作为答案(在这种情况下为1648512000)

我该怎么办? 提前致谢!!

I need to get GMT timestamps, a timestamp when start a day, and another when end a day, the following example is for today (2022-03-29):

Start of Day (Unix Timestamp): 1648512000 
End of Day (Unix Timestamp): 1648598399

you can check this in https://www.epochconvert.com/

and my code only for the case of start of day is the next one :

from datetime import datetime
start_date = datetime.today().strftime('%Y-%m-%d')
start_date += ' 00:00:00'
print(start_date)
timestamp = time.mktime(datetime.strptime(start_date, "%Y-%m-%d %H:%M:%S").timetuple())
print(timestamp)

and my output:

2022-03-29 00:00:00
1648533600.0

as you can see it's not the answer I'm waiting for, that timestamp is for my Local Time but i need a GMT timestamp as answer (1648512000 in this case)

How could i do it ?
Thanks in Advance!!

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

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

发布评论

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

评论(1

等待圉鍢 2025-01-24 20:32:31

如果要派生 unix time 从dateTime对象中,请确保设置时区域/拥有< em> Aware dateTime:

from datetime import datetime, timezone

now = datetime.now(timezone.utc)
print(now)
# 2022-03-29 23:34:51.701712+00:00

now_floored = datetime.combine(now.date(), now.time().min).replace(tzinfo=timezone.utc)
print(now_floored)
# 2022-03-29 00:00:00+00:00

now_floored_unix = now_floored.timestamp()
print(now_floored_unix)
# 1648512000.0

如果您不这样做(有天真的日期时间),Python将使用 Local Time

now = datetime.now()
now_floored = datetime.combine(now.date(), now.time().min)
print(now_floored.astimezone())
# 2022-03-29 00:00:00-06:00 # <- my current UTC offset is -6 hours

print(now_floored.timestamp(), (now_floored_unix-now_floored.timestamp())//3600)
# 1648533600.0 -6.0

if you want to derive Unix time from datetime objects, make sure to set time zone / have aware datetime:

from datetime import datetime, timezone

now = datetime.now(timezone.utc)
print(now)
# 2022-03-29 23:34:51.701712+00:00

now_floored = datetime.combine(now.date(), now.time().min).replace(tzinfo=timezone.utc)
print(now_floored)
# 2022-03-29 00:00:00+00:00

now_floored_unix = now_floored.timestamp()
print(now_floored_unix)
# 1648512000.0

If you do not do that (have naive datetime), Python will use local time:

now = datetime.now()
now_floored = datetime.combine(now.date(), now.time().min)
print(now_floored.astimezone())
# 2022-03-29 00:00:00-06:00 # <- my current UTC offset is -6 hours

print(now_floored.timestamp(), (now_floored_unix-now_floored.timestamp())//3600)
# 1648533600.0 -6.0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文