<legend id='U5mnf'><style id='U5mnf'><dir id='U5mnf'><q id='U5mnf'></q></dir></style></legend>
      <bdo id='U5mnf'></bdo><ul id='U5mnf'></ul>
    <tfoot id='U5mnf'></tfoot>

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

    1. 在 Python 中使用 pytz 本地化纪元时间

      时间:2023-07-03

      • <bdo id='DYqse'></bdo><ul id='DYqse'></ul>
        <tfoot id='DYqse'></tfoot>
        • <legend id='DYqse'><style id='DYqse'><dir id='DYqse'><q id='DYqse'></q></dir></style></legend>

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

                <tbody id='DYqse'></tbody>
              1. <i id='DYqse'><tr id='DYqse'><dt id='DYqse'><q id='DYqse'><span id='DYqse'><b id='DYqse'><form id='DYqse'><ins id='DYqse'></ins><ul id='DYqse'></ul><sub id='DYqse'></sub></form><legend id='DYqse'></legend><bdo id='DYqse'><pre id='DYqse'><center id='DYqse'></center></pre></bdo></b><th id='DYqse'></th></span></q></dt></tr></i><div id='DYqse'><tfoot id='DYqse'></tfoot><dl id='DYqse'><fieldset id='DYqse'></fieldset></dl></div>
                本文介绍了在 Python 中使用 pytz 本地化纪元时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在使用 pytz 将纪元时间戳转换为不同时区的日期.我想要做的是创建一个 DateTime 对象,它接受一个 Olson 数据库时区和一个纪元时间并返回一个本地化的 datetime 对象.最终,我需要回答诸如纪元时间 1350663248 时在纽约几点钟?"

                Im working on converting epoch timestamps to dates in different timezones with pytz. What I am trying to do is create a DateTime object that accepts an Olson database timezone and an epoch time and returns a localized datetime object. Eventually I need to answer questions like "What hour was it in New York at epoch time 1350663248?"

                这里有些东西不能正常工作:

                Something is not working correctly here:

                import datetime, pytz, time
                
                class DateTime:
                    def __init__(self, timezone, epoch):
                        self.timezone = timezone
                        self.epoch = epoch
                        timezoneobject = pytz.timezone(timezone)
                        datetimeobject = datetime.datetime.fromtimestamp( self.epoch )
                        self.datetime = timezoneobject.localize(datetimeobject)
                
                    def hour(self):
                        return self.datetime.hour
                
                if __name__=='__main__':
                    epoch = time.time()
                    dt = DateTime('America/Los_Angeles',epoch)
                    print dt.datetime.hour
                    dt = DateTime('America/New_York',epoch)
                    print dt.datetime.hour
                

                这会打印相同的小时,而应该提前 3 小时左右.这里出了什么问题?我是一个 Python 初学者,感谢任何帮助!

                This prints the same hour, whereas one should be 3 or so hours ahead. Whats going wrong here? I'm a total Python beginner, any help is appreciated!

                推荐答案

                datetime.fromtimestamp(self.epoch) 返回不应与任意 timezone.localize() 一起使用的本地时间;您需要 utcfromtimestamp() 以 UTC 获取日期时间,然后将其转换为所需的时区:

                datetime.fromtimestamp(self.epoch) returns localtime that shouldn't be used with an arbitrary timezone.localize(); you need utcfromtimestamp() to get datetime in UTC and then convert it to a desired timezone:

                from datetime import datetime
                import pytz
                
                # get time in UTC
                utc_dt = datetime.utcfromtimestamp(posix_timestamp).replace(tzinfo=pytz.utc)
                
                # convert it to tz
                tz = pytz.timezone('America/New_York')
                dt = utc_dt.astimezone(tz)
                
                # print it
                print(dt.strftime('%Y-%m-%d %H:%M:%S %Z%z'))
                

                或者更简单的替代方法是直接从时间戳构造:

                Or a simpler alternative is to construct from the timestamp directly:

                from datetime import datetime
                import pytz
                
                # get time in tz
                tz = pytz.timezone('America/New_York')
                dt = datetime.fromtimestamp(posix_timestamp, tz)
                # print it
                print(dt.strftime('%Y-%m-%d %H:%M:%S %Z%z'))
                

                在这种情况下,它隐式地从 UTC 转换.

                It converts from UTC implicitly in this case.

                这篇关于在 Python 中使用 pytz 本地化纪元时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:Python:datetime tzinfo 时区名称文档 下一篇:pytz:为什么在时区之间转换时需要规范化?

                相关文章

              2. <legend id='nkfsB'><style id='nkfsB'><dir id='nkfsB'><q id='nkfsB'></q></dir></style></legend>

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

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

                    <bdo id='nkfsB'></bdo><ul id='nkfsB'></ul>
                    <tfoot id='nkfsB'></tfoot>