<i id='Cy0TV'><tr id='Cy0TV'><dt id='Cy0TV'><q id='Cy0TV'><span id='Cy0TV'><b id='Cy0TV'><form id='Cy0TV'><ins id='Cy0TV'></ins><ul id='Cy0TV'></ul><sub id='Cy0TV'></sub></form><legend id='Cy0TV'></legend><bdo id='Cy0TV'><pre id='Cy0TV'><center id='Cy0TV'></center></pre></bdo></b><th id='Cy0TV'></th></span></q></dt></tr></i><div id='Cy0TV'><tfoot id='Cy0TV'></tfoot><dl id='Cy0TV'><fieldset id='Cy0TV'></fieldset></dl></div>
      <bdo id='Cy0TV'></bdo><ul id='Cy0TV'></ul>
  1. <legend id='Cy0TV'><style id='Cy0TV'><dir id='Cy0TV'><q id='Cy0TV'></q></dir></style></legend>
    1. <tfoot id='Cy0TV'></tfoot>

    2. <small id='Cy0TV'></small><noframes id='Cy0TV'>

      如何在 python 中使用带有 datetime 对象的时区?

      时间:2023-07-03
      • <bdo id='lf6Wt'></bdo><ul id='lf6Wt'></ul>
        <tfoot id='lf6Wt'></tfoot>

        <small id='lf6Wt'></small><noframes id='lf6Wt'>

            <tbody id='lf6Wt'></tbody>

          • <i id='lf6Wt'><tr id='lf6Wt'><dt id='lf6Wt'><q id='lf6Wt'><span id='lf6Wt'><b id='lf6Wt'><form id='lf6Wt'><ins id='lf6Wt'></ins><ul id='lf6Wt'></ul><sub id='lf6Wt'></sub></form><legend id='lf6Wt'></legend><bdo id='lf6Wt'><pre id='lf6Wt'><center id='lf6Wt'></center></pre></bdo></b><th id='lf6Wt'></th></span></q></dt></tr></i><div id='lf6Wt'><tfoot id='lf6Wt'></tfoot><dl id='lf6Wt'><fieldset id='lf6Wt'></fieldset></dl></div>

              • <legend id='lf6Wt'><style id='lf6Wt'><dir id='lf6Wt'><q id='lf6Wt'></q></dir></style></legend>

                本文介绍了如何在 python 中使用带有 datetime 对象的时区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                如何在我的时区中正确表示不同的时区?下面的示例仅适用,因为我知道 EDT 比我早一小时,所以我可以取消注释 myTimeZone() 的减法

                How do I properly represent a different timezone in my timezone? The below example only works because I know that EDT is one hour ahead of me, so I can uncomment the subtraction of myTimeZone()

                import datetime, re
                from datetime import tzinfo
                
                class myTimeZone(tzinfo):
                    """docstring for myTimeZone"""
                    def utfoffset(self, dt):
                        return timedelta(hours=1)
                
                def myDateHandler(aDateString):
                    """u'Sat,  6 Sep 2008 21:16:33 EDT'"""
                    _my_date_pattern = re.compile(r'w+,s+(d+)s+(w+)s+(d+)s+(d+):(d+):(d+)')
                    day, month, year, hour, minute, second = _my_date_pattern.search(aDateString).groups()
                    month = [
                            'JAN', 'FEB', 'MAR', 
                            'APR', 'MAY', 'JUN', 
                            'JUL', 'AUG', 'SEP', 
                            'OCT', 'NOV', 'DEC'
                    ].index(month.upper()) + 1
                    dt = datetime.datetime(
                        int(year), int(month), int(day), 
                        int(hour), int(minute), int(second)
                    )                   
                    # dt = dt - datetime.timedelta(hours=1)
                    # dt = dt - dt.tzinfo.utfoffset(myTimeZone())
                    return (dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second, 0, 0, 0)
                
                def main():
                    print myDateHandler("Sat,  6 Sep 2008 21:16:33 EDT")
                
                if __name__ == '__main__':
                    main()
                

                推荐答案

                在使用时区时,我推荐 babelpytz.保持您的内部日期时间对象天真并使用 UTC 并转换为您的时区以进行格式化.您可能想要简单对象(没有时区信息的对象)的原因是许多库和数据库适配器不知道时区.

                I recommend babel and pytz when working with timezones. Keep your internal datetime objects naive and in UTC and convert to your timezone for formatting only. The reason why you probably want naive objects (objects without timezone information) is that many libraries and database adapters have no idea about timezones.

                • 巴别塔
                • pytz

                这篇关于如何在 python 中使用带有 datetime 对象的时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:让 Python 响应 Windows 时区的变化 下一篇:如何检查日期时间对象是否使用 pytz 本地化?

                相关文章

                  <tfoot id='948ZM'></tfoot>
                1. <legend id='948ZM'><style id='948ZM'><dir id='948ZM'><q id='948ZM'></q></dir></style></legend>

                  <small id='948ZM'></small><noframes id='948ZM'>

                    <i id='948ZM'><tr id='948ZM'><dt id='948ZM'><q id='948ZM'><span id='948ZM'><b id='948ZM'><form id='948ZM'><ins id='948ZM'></ins><ul id='948ZM'></ul><sub id='948ZM'></sub></form><legend id='948ZM'></legend><bdo id='948ZM'><pre id='948ZM'><center id='948ZM'></center></pre></bdo></b><th id='948ZM'></th></span></q></dt></tr></i><div id='948ZM'><tfoot id='948ZM'></tfoot><dl id='948ZM'><fieldset id='948ZM'></fieldset></dl></div>
                    • <bdo id='948ZM'></bdo><ul id='948ZM'></ul>