代码之家  ›  专栏  ›  技术社区  ›  Steven

Python的美国/纽约时间偏移显示-04:56

  •  0
  • Steven  · 技术社区  · 10 月前

    我目前正在使用Python 3.9.6,并且有以下简单的代码

    import datetime
    import pytz
    
    est = pytz.timezone('America/New_York')
    myDateTime = datetime.datetime(year=2024, month=8, day=15, hour=17, minute=00, tzinfo=est)
    print("America/New_York: ", myDateTime.isoformat())
    print("UTC:              ", myDateTime.astimezone(pytz.utc).isoformat())
    

    这给出了结果

    America/New_York:  2024-08-15T17:00:00-04:56
    UTC:               2024-08-15T21:56:00+00:00
    

    目前在纽约是夏令时。因此,我预计美国/纽约将显示时间偏移 -04:00 ,但它正在显示 -4:56 我想知道是否有人对此有解释。

    1 回复  |  直到 10 月前
        1
  •  1
  •   oneli jayodya    10 月前

    试试这样:

    import datetime  
    import pytz  
    
    est = pytz.timezone('America/New_York')  
    
    
        naive_datetime = datetime.datetime(year=2024, month=8, day=15, hour=17, minute=0)  
        
        # Localize it to the Eastern timezone  
        myDateTime = est.localize(naive_datetime)  
        
        print("America/New_York: ", myDateTime.isoformat())  
        print("UTC:              ", myDateTime.astimezone(pytz.utc).isoformat())
    

    当您使用tzinfo参数创建日期时间对象时,如下所示:

    myDateTime = datetime.datetime(year=2024, month=8, day=15, hour=17, minute=0, tzinfo=est)