我将所有时间存储为UTC,并且我的系统设置为UTC(尽管我在东部标准时间)。
我的日期存储格式如下:
Wed, 20 Feb 2013 03:51:39 +0000
然而,我想基于美国东部时间今天的信息进行选择,所以我的尝试如下:
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).
如果这不是做这件事的最好方式,或者我漏掉了某些东西(对我来说似乎过于复杂),请帮助我看到光明!
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
哪个是正确的?我现在需要从中生成完整的东部标准时间,然后转换为协调世界时。这一点我无法弄清楚。实际上,完成后我可能想要将其转换为UTC时代时间戳,因为这将使我的数据库操作非常简单(<,>,==等)。