问题描述
我将所有时间都存储在 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!