确定当天的开始和结束时间(UTC -> EST -> UTC);Python
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14972802/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Determine start and end time of current day (UTC -> EST -> UTC) ; Python
提问by mr-sk
I am storing all my times in UTC and my system is set to UTC (though I am in EST).
我将所有时间都存储在 UTC 中,并且我的系统设置为 UTC(尽管我在 EST)。
I have dates stored as:
我将日期存储为:
Wed, 20 Feb 2013 03:51:39 +0000
However, I would like to select information based off today for EST, so I am attempting to:
但是,我想根据今天为 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:00Next 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)
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).
获取当前时间为 UTC 并更改为 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)
一旦我有了这些值,我想转换回 UTC,所以我有一个高低值,我可以用正确的 EST(我的时区)来钳制。
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
TIA
Update per answer:
更新每个答案:
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")
That will give me
那会给我
02 20 2013
02 21 2013
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).
哪个是正确的。我现在需要从中生成完整的 EST 时间,然后转换为 UTC。这是我想不通的。实际上,我可能想在完成后转换为 UTC 纪元时间戳,因为这将使我的数据库操作非常简单(<、>、== 等)。
采纳答案by Lennart Regebro
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 的第一步似乎有点毫无意义。你会利用这段时间做任何事情吗?
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. :-)
除此之外,它似乎相当简单。您想以 UTC 格式获取一天 EST 的开始和结束时间,因此您可以创建它们并将它们转换为 UTC。那没那么复杂。:-)
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.
不过,您可能想要查看匹配的例程,以便您可以将今天的开始用作较低的值,将明天的开始用作较高的值,这样您就不必处理 23:59:59.9999 时间.
Update:
更新:
From my original understanding of your question, this is what you want to do:
根据我对您问题的最初理解,这就是您想要做的:
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.
首先,您想获得 UTC 中的当前日期(因此在美国东部时间 12 日晚上 11 点,您需要 22 日,因为它是 UTC 中的 22 日。
>>> from datetime import datetime
>>> today = datetime.utcnow().date()
>>> today
datetime.date(2013, 2, 21)
Secondly you want 00:00:00 of that day in UTC, as start for a search.
其次,您需要 UTC 当天的 00:00:00,作为搜索的开始。
>>> 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'))
Summary:
概括:
today = datetime.utcnow().date()
start = datetime(today.year, today.month, today.day, tzinfo=tz.tzutc()).astimezone(est)
end = start + timedelta(1)
回答by myusuf3
I would definitely give Deloreana look, to solve your problem would follow a few steps.
我肯定会给Delorean看一看,解决您的问题将遵循几个步骤。
You first need to parse your string. Excellent use the Delorean parsemethod.
您首先需要解析您的字符串。很好地使用 Delorean解析方法。
>>> from delorean import parse
>>> d = parse("Wed, 20 Feb 2013 03:51:39 +0000")
>>> d
Delorean(datetime=2013-02-20 03:51:39+00:00, timezone=UTC)
Once you have the datetime that you parsed in a Delorean object you simply convert to EST
一旦您在 Delorean 对象中解析了日期时间,您只需将其转换为 EST
>>> d = d.shift('US/Eastern')
>>> d
Delorean(datetime=2013-02-19 22:51:39-05:00, timezone=US/Eastern)
Albeit pointless. You never use it for anything in your question, but super easy with Delorean.
虽然毫无意义。你永远不会在你的问题中使用它,但使用 Delorean 非常容易。
Then you get the time now in EST
然后你现在有时间在 EST
from delorean import Delorean
从德洛林进口德洛林
>>> d1 = Delorean(timezone="US/Eastern")
>>> d1
Delorean(datetime=2013-02-21 00:35:56.405256-05:00, timezone=US/Eastern)
Now for the truncationstep.
现在进行截断步骤。
>>> d.truncate('day')
Delorean(datetime=2013-02-21 00:00:00-05:00, timezone=US/Eastern)
do the simple shift as above to UTC.
像上面那样简单地转换为 UTC。
Now get the end of day.
现在结束一天。
d = d.next_day(1) # move to the next day
Then to shift back one second. Something that the library needs I will be updating this. Simply get the datetime from the Deloreanexample by asking for it with datetimeattribute.
然后向后移动一秒。图书馆需要的东西我会更新这个。Delorean通过使用datetime属性请求它,只需从示例中获取日期时间。
d.datetime - timedelta(seconds=1)
datetime.datetime(2013, 2, 21, 23, 59, 59, tzinfo=<DstTzInfo 'US/Eastern' EST-1 day, 19:00:00 STD>)
Goodluck, but this library should simply your dealing with datetime operations :)
祝你好运,但这个库应该只是你处理日期时间操作:)
回答by Bruce Edge
Just ran into this, here's the simplest option I found:
刚遇到这个,这是我找到的最简单的选择:
from delorean import Delorean
today_d = Delorean()
sod_dt = today_d.start_of_day
eod_dt = today_d.end_of_day
sod_d = Delorean(sod_dt)
eod_d = Delorean(eod_dt)
sod_e = sod_d.epoch
eod_e = eod_d.epoch
to confirm:
确认:
In [69]: eod_e - sod_e
Out[69]: 86399.99999904633
close enough for most people
对大多数人来说足够接近
回答by Braden Heckman
This is only a partial answer, because the rest has been covered well. I struggled with this for a while, as some technologies have inclusive searches, and I don't want to include any data from the first microsecond of the next day.
这只是部分答案,因为其余部分已经涵盖得很好。我为此苦苦挣扎了一段时间,因为某些技术具有包容性搜索,而且我不想包含第二天第一微秒的任何数据。
My solution for finding the end of day time quickly and correctly is this:
我快速正确地找到一天结束时间的解决方案是:
reference_time.replace(hour=0, minute=0, second=0, microsecond=0) + timedelta(days=1,microseconds=-1)
回答by Lifei Chen
use datetimepytzwill solve your problem.
使用datetimepytz将解决您的问题。
def get_start_and_end():
tz = pytz.timezone('Asia/Shanghai')
today = datetime.now(tz=tz)
start = today.replace(hour=0, minute=0, second=0, microsecond=0)
end = start + timedelta(1)
return start, end
回答by juankysmith
The question is old but maybe this helps:
这个问题很老,但也许这有帮助:
import datetime
end_of_today = datetime.datetime.combine(datetime.datetime.today(), datetime.time(23, 59, 59, 999999))

