通过提供时区值获取不同时区的当前时间

问题描述 投票:0回答:1

在我不知道时区区域名称的情况下,我需要在特定的timezone中获取当前时间。基本上,我需要一种方法,我将传递时区的UTC值,并期望返回为该区域的当前时间。我需要的方法应该是'

def get_current_time(utc_value):
    #CODE
    return current_time
print (get_current_time("+05:30"))`

而且我希望它能给我当前时间UTC +5:30时区。怎么做?

python python-3.x datetime pytz
1个回答
0
投票
from pytz import all_timezones, timezone
from datetime import datetime

def get_cuurent_time(utc_value):
    for zone in all_timezones:
        tz = timezone(zone)
        if utc_value in  str(datetime.now(tz)):
            return datetime.now(tz)
print (get_cuurent_time("+05:30"))

我做了,不打扰:),但如果还有其他任何方式,请告诉我。

© www.soinside.com 2019 - 2024. All rights reserved.