【问题标题】:UTC to CST time conversion using pytz python packageUTC到CST时间转换使用pytz python包
【发布时间】:2020-09-01 23:47:38
【问题描述】:

我有嵌套的 json 文件,该文件具有 UTC 格式的时区下面贴出代码

def extract_json_data(fpath):
    print("Extracting " + fpath)
    f = open(fpath, 'r')
    json_data = json.loads(f.read())
    data = json_data['data']
    dt = datetime.datetime.strptime(json_data['time'], "%Y-%m-%dT%H:%M:%SZ")
    dt_cst = dt.astimezone(timezone('US/Central'))
    _ = [row.update({'time_UTC': dt.strftime("%Y-%m-%dT%H:%M:%SZ"),
                     'time_CST': dt_cst.strftime("%Y-%m-%dT%H:%M:%S CST")}) for row in data]

【问题讨论】:

  • 如果您正确拼写了pytz,并且将其包含在您的代码示例中,您可能会在这个问题上获得更好的帮助。
  • 你的格式字符串"%Y-%m-%dT%H:%M:%SZ"最后解析了一个文字Z,我想你想解析时区(Z;UTC),所以使用"%Y-%m-%dT%H:%M:%S%z" - 请参阅下面的答案。

标签: python python-3.x datetime pytz


【解决方案1】:

使用格式字符串解析时区,以便您使用的日期时间对象是时区感知的:

from datetime import datetime

# the string actually has timezone information: Z (UTC)
timestring = "2019-01-01T00:00:00Z"

# wrong:
dt = datetime.strptime(timestring, "%Y-%m-%dT%H:%M:%SZ")
# dt is naive:
# datetime.datetime(2019, 1, 1, 0, 0)

# right:
dt = datetime.strptime(timestring, "%Y-%m-%dT%H:%M:%S%z")
# dt now knows it's in UTC:
# datetime.datetime(2019, 1, 1, 0, 0, tzinfo=datetime.timezone.utc)

现在您可以将 datetime 对象的时间更改为不同的时区:

import pytz
tz = pytz.timezone('US/Central')
dt_cst = dt.astimezone(tz)
# datetime.datetime(2018, 12, 31, 18, 0, tzinfo=<DstTzInfo 'US/Central' CST-1 day, 18:00:00 STD>)

更方便的解决方案是跳过pytz 并改用dateutil

import dateutil
timestring = "2019-01-01T00:00:00Z"
dt = dateutil.parser.parse(timestring)

# dt
# datetime.datetime(2019, 1, 1, 0, 0, tzinfo=tzutc())

【讨论】:

    【解决方案2】:

    这是一种方法:

    import datetime
    from dateutil import tz
    
    # create a time-zone object representing UTC. 
    from_zone = tz.gettz('UTC')
    
    # Create another time zone object, representing the target time zone. 
    # note that according to the tz package documentation 
    # (https://dateutil.readthedocs.io/en/stable/tz.html#dateutil.tz.gettz), 
    # they have Windows-specific time-zone names support. 
    to_zone = tz.gettz('America/Chicago')
    
    # This is just a sample dictionary, so I cam extract the 'time' 
    # field like you do in your code. It's really not needed here. 
    json_data = {'time': "2020-05-16T08:17:42Z"} # an example for a datetime
    
    # Create a datetime object, representing the UTC time. 
    utc = datetime.datetime.strptime(json_data['time'], "%Y-%m-%dT%H:%M:%SZ")
    
    # now replace the timezone field of the newly created datetime object, 
    # so it would be UTC. 
    utc = utc.replace(tzinfo=from_zone)
    
    # Convert time zone from UTC to central
    central = utc.astimezone(to_zone)
    
    print(central)
    

    你会得到:

    2020-05-16 03:17:42-05:00

    【讨论】:

    • 您好 Roy,您是否可以逐行解释您的代码,因为我尝试过但没有用,我可能遗漏了一些东西,如果您能指出我要去哪里,我会解释我的错误的是读取我的 JSON 文件,然后从中获取时间并将其放入列中,然后尝试将该列时间转换为 cst 并将其存储在另一个列中
    • @user13073332。添加了一些注释。你说它不起作用 - 如果你按原样运行我的代码会发生什么?
    • 您的示例时间字符串“2020-05-16T08:17:42Z”最后有一个时区,那么为什么要使用replace()?只需对strptime....使用正确的格式字符串即可。
    猜你喜欢
    • 1970-01-01
    • 2021-04-10
    • 2010-11-24
    • 1970-01-01
    • 2017-03-10
    • 2013-08-26
    • 2014-10-05
    • 2013-01-04
    • 1970-01-01
    相关资源
    最近更新 更多