【问题标题】:Getting current time of different timezone by providing timezone value通过提供时区值获取不同时区的当前时间
【发布时间】:2018-09-08 16:03:31
【问题描述】:

在不知道时区名称的场景下,需要在特定的timezone中获取当前时间。基本上,我需要一个方法来传递UTC 时区的值,并期望返回该区域的当前时间。 我需要的方法应该是`

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

我希望它应该给我UTC +5:30 时区的当前时间。 怎么做?

【问题讨论】:

  • @iam.Carrot,我想通过字符串传递UTC并获取该区域的时间,我也有解决方案,但如果还有其他方法,请让我知道。

标签: python python-3.x datetime pytz


【解决方案1】:
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"))

我做到了,不要打扰:),但如果还有其他方法,请告诉我。

【讨论】:

    猜你喜欢
    • 2015-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-30
    • 2012-02-27
    • 1970-01-01
    相关资源
    最近更新 更多