确定当天的开始和结束时间(UTC - > EST - > UTC);蟒蛇 [英] Determine start and end time of current day (UTC -> EST -> UTC) ; Python

查看:294
本文介绍了确定当天的开始和结束时间(UTC - > EST - > UTC);蟒蛇的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我将所有时间都存储在UTC中,我的系统设置为UTC(虽然我在EST中) >

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

然而,我想选择今天关于EST的信息,所以我试图:




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

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

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


  • 一旦我有这些值,我想转换回UTC,所以我有一个很高的和低的价值,我可以通过这是正确的我的EST(我的时区)夹。




<如果这不是最好的方法,或者我错过了一些东西(看起来很重叠)对我来说很复杂)请帮我看看光!



TIA



每个答案更新:

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

这将给我

  02 20 2013 
02 21 2013

哪个是正确的我现在需要从中生成完整的EST时间,然后转换为UTC。这个我无法弄清楚。实际上,我完全可能要转换为UTC时代的时间戳,因为这将使我的数据库操作变得非常简单(<>,==等)。

解决方案

将当前时间作为UTC并将其转换为EST的第一步似乎没有意义。你有没有使用这个时间?



除此之外,似乎相当直观。你想要得到UTC的一天EST的开始和结束,所以你创建它们并将它们转换为UTC。那不是那么复杂: - )



您可能希望查看匹配的例程,以便您可以将今天的开始用作较低的值,并将明天的开始作为更高,所以你不必处理这个23:59:59.9999的时间。



更新:



从我对你的问题的原始理解,这是你想要做的:



首先你想得到当前的日期,因为它是在UTC(所以在美国东部时间晚上11点,第12个,你想要第22个,因为它是在UTC的第22个。然后。

 > >> from datetime import datetime 
>>>今天= datetime.utcnow()。date()
>>>今天
datetime.date(2013, 2,21)

其次,您希望在UTC的当天00:00:00开始一个搜索。

 >> from dateutil import tz 
>>> start = datetime (今天,今天,今天,tzinfo = tz.tzutc())
datetime.datetime(2013,2,21,0, 0,tzinfo = tzutc())

除了你想知道那个时间在纽约:

 >>> 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'))
pre>

而您也希望明天结束:

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

总结:

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


I am storing all my times in UTC and my system is set to UTC (though I am in 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:

  • 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
    

  • 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)

  • 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

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).

解决方案

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?

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. :-)

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.

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.

>>> 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.

>>> 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)

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆