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

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

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

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

      1. 确定当天的开始和结束时间(UTC -> EST -> UTC);Python

        Determine start and end time of current day (UTC -gt; EST -gt; UTC) ; Python(确定当天的开始和结束时间(UTC - EST - UTC);Python)
        <legend id='wGThQ'><style id='wGThQ'><dir id='wGThQ'><q id='wGThQ'></q></dir></style></legend>

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

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

            <tfoot id='wGThQ'></tfoot>
              <tbody id='wGThQ'></tbody>
                • <bdo id='wGThQ'></bdo><ul id='wGThQ'></ul>
                  本文介绍了确定当天的开始和结束时间(UTC -> EST -> UTC);Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我将所有时间都存储在 UTC 中,并且我的系统设置为 UTC(尽管我在 EST).

                  I am storing all my times in UTC and my system is set to UTC (though I am in EST).

                  我将日期存储为:

                  Wed, 20 Feb 2013 03:51:39 +0000
                  

                  但是,我想根据今天为 EST 选择信息,因此我尝试:

                  However, I would like to select information based off today for EST, so I am attempting to:

                  • 获取当前时间为 UTC 并更改为 EST

                  • Get current time as UTC and change to EST

                  datetime.utcnow().replace(tzinfo=tz.tzutc()).astimezone(tz.gettz('America/New_York'))
                  2013-02-19 23:17:20.560898-05:00
                  

                • 接下来我想获取 EST 日的开始时间 (2013-02-19 00:00:00.000000-05:00) 和结束时间 (2013-02-19 23:59:59.99999-05:00)

                • Next I want to get the start time for the EST day (2013-02-19 00:00:00.000000-05:00) and the end time (2013-02-19 23:59:59.99999-05:00)

                  一旦我有了这些值,我想转换回 UTC,所以我有一个高低值,我可以通过正确的 EST(我的时区)来钳制它.

                  Once I have those values, I'd like to convert back to UTC, so I have a high and low value I can clamp by that's correct my EST (my timezone).

                  如果这不是最好的方法,或者我遗漏了一些东西(对我来说似乎过于复杂),请帮我看看!

                  If this isn't the best way to do this, or I'm missing something (does seem overly complicated to me) please help me see the light!

                  TIA

                  每个答案更新:

                  d1 = datetime.utcnow().replace(tzinfo=tz.tzutc()).astimezone(tz.gettz('America/New_York'))
                  print d1.strftime("%m %d %Y") ; d2 = d1.replace(day=d1.day + 1) ; print d2.strftime("%m %d %Y")
                  

                  这会给我

                  02 20 2013
                  02 21 2013
                  

                  这是正确的.我现在需要从中生成完整的 EST 时间,然后转换为 UTC.这个我想不通.实际上,我可能希望在完成后转换为 UTC 纪元时间戳,因为这将使我的数据库操作非常容易(<、>、== 等).

                  Which is correct. I now need to generate the full EST time from that and then convert to UTC. This I cannot figure out. Actually, I probably want to convert to UTC epoch timestamp when complete because that will make my database operations pretty easy (<, >, ==, etc).

                  推荐答案

                  获取当前时间为 UTC 并将其转换为 EST 的第一步似乎有点毫无意义.你有没有把这段时间用在什么事情上?

                  The first step of getting current time as UTC and converting it to EST seems a bit pointless. Do you use that time for anything?

                  除此之外,它似乎相当直截了当.您想以 UTC 获取一天 EST 的开始和结束,因此您创建它们并将它们转换为 UTC.那没那么复杂.:-)

                  Other than that it seems rather straighforward. You want to get the start and end of a day EST in UTC, so you create them and convert them to UTC. That's not so complicated. :-)

                  不过,您可能希望查看匹配例程,以便可以使用今天的开始作为较低的值,将明天的开始作为较高的值,这样您就不必处理 23:59:59.9999 时间.

                  You might want to look at your matching routines though, so that you can use the start of today as the lower value, and the start of tomorrow as the higher, so you don't have to deal with that 23:59:59.9999 time.

                  更新:

                  根据我对您问题的最初理解,这就是您想要做的:

                  From my original understanding of your question, this is what you want to do:

                  首先,您想要获取 UTC 中的当前日期(因此,在 12 日东部标准时间晚上 11 点,您想要 22 日,因为它是 UTC 中的 22 日.

                  First you want to get the current date as it is in UTC (so at 11pm EST the 12st, you want the 22nd, as it is the 22nd in UTC then.

                  >>> from datetime import datetime
                  >>> today = datetime.utcnow().date()
                  >>> today
                  datetime.date(2013, 2, 21)
                  

                  其次,您希望 UTC 当天的 00:00:00 作为搜索的开始.

                  Secondly you want 00:00:00 of that day in UTC, as start for a search.

                  >>> from dateutil import tz
                  >>> start = datetime(today.year, today.month, today.day, tzinfo=tz.tzutc())
                  datetime.datetime(2013, 2, 21, 0, 0, tzinfo=tzutc())
                  

                  除非您想知道纽约的时间:

                  Except that you want to know what that time is in New York:

                  >>> from dateutil import tz
                  >>> est = tz.gettz('America/New_York')
                  >>> start = start.astimezone(est)
                  >>> start
                  datetime.datetime(2013, 2, 20, 19, 0, tzinfo=tzfile('/usr/share/zoneinfo/America/New_York'))
                  

                  而且你还希望明天作为结束:

                  And you also want tomorrow as the end:

                  >>> from datetime import timedelta
                  >>> end = start + timedelta(1)
                  >>> end
                  datetime.datetime(2013, 2, 21, 19, 0, tzinfo=tzfile('/usr/share/zoneinfo/America/New_York'))
                  

                  总结:

                  today = datetime.utcnow().date()
                  start = datetime(today.year, today.month, today.day, tzinfo=tz.tzutc()).astimezone(est)
                  end = start + timedelta(1)
                  

                  这篇关于确定当天的开始和结束时间(UTC -> EST -> UTC);Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!
                • 相关文档推荐

                  env: python: No such file or directory(env: python: 没有这样的文件或目录)
                  How to evaluate environment variables into a string in Python?(如何在 Python 中将环境变量评估为字符串?)
                  Python - temporarily modify the current process#39;s environment(Python - 临时修改当前进程的环境)
                  Change current process environment#39;s LD_LIBRARY_PATH(更改当前进程环境的 LD_LIBRARY_PATH)
                  Reading and writing environment variables in Python?(在 Python 中读写环境变量?)
                  When to use sys.path.append and when modifying %PYTHONPATH% is enough(何时使用 sys.path.append 以及何时修改 %PYTHONPATH% 就足够了)

                  • <tfoot id='lE5Vl'></tfoot>

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

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

                        • <bdo id='lE5Vl'></bdo><ul id='lE5Vl'></ul>

                            <tbody id='lE5Vl'></tbody>

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