将UTC时区转换为IST python [英] Convert UTC timezone to IST python

查看:58
本文介绍了将UTC时区转换为IST python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码行为我提供了生产服务器上的 UTC 时间.

The following code line gives me the UTC timing on the production server.

timestamp = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')

请建议一种将以上UTC时区转换为 IST(印度标准时间)时间的方法.

Please suggest a way to convert above UTC timezone to IST(Indian Standard Time) timing.

推荐答案

datetime.now()为您提供了一个朴素的datetime对象,该对象根据运行脚本的计算机的设置表示本地时间(在这种情况下,由于它是服务器,因此恰好是UTC ).请参见文档.

datetime.now() gives you a naive datetime object that represents local time according to the setting of the machine you run the script on (which in this case just happens to be UTC since it's a server). See the docs.

要获取立即"在特定时区,您可以适当地设置 tz 属性.

To get "now" in a specific timezone, you can set the tz property appropriately.

Python<3.9:我建议使用 dateutil 软件包.>

Python < 3.9: I'd recommend the dateutil package to do so.

from datetime import datetime
from dateutil.tz import gettz
dtobj = datetime.now(tz=gettz('Asia/Kolkata'))
print(dtobj)
>>> 2020-07-23 11:08:54.032651+05:30

Python> = 3.9:,您有 zoneinfo 在标准库中执行以下操作:

Python >= 3.9: you have zoneinfo in the standard lib to do this:

from zoneinfo import ZoneInfo
dtobj = datetime.now(tz=ZoneInfo('Asia/Kolkata'))
print(dtobj)
>>> 2020-07-23 11:11:43.594442+05:30

这篇关于将UTC时区转换为IST python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆