我正在尝试查询/过滤表格以查找东部时间今天发生的事件,但我一直遇到问题。它返回我的预期结果,但我收到运行时警告。我没有尝试大约 15 种不同的实现,但我要么收到运行时警告,要么没有返回预期的数据。虽然数据库将所有内容存储为应有的 UTC,但整个应用程序仅基于东部时间。我要做的就是过滤发生在 >= 至 12:00 AM EST/EDT 的对象
RuntimeWarning:在时区支持处于活动状态时,DateTimeField 收到了一个幼稚的日期时间 (2013-08-13 00:00:00)。
我追求的功能是检查用户今天在东部时间有多少条目。
使用 Sqlite3
Settings.py:
USE_TZ = True
TIME_ZONE = 'America/New_York'
Models.py
class Entry(models.Model):
id = models.AutoField(primary_key=True, null=False)
prize_id = models.ForeignKey(Prize)
member = models.ForeignKey(Member)
createdate = models.DateTimeField(auto_now_add=True)
Views.py
from django.utils import timezone
from datetime
#this is part of my view class
def get_user_entries(memberid, prizeid):
today = datetime.date.today()
e = Entry.objects.filter(member_id=memberid
).filter(prize_id=prizeid
).filter(createdate__gte=today
).count()
return e
以下是我厌倦了今天约会的一些方法:
today = datetime.date.today()
today = '%s-%s-%s' % (today.year, today.month, today.day)
date = datetime.date.today()
time = datetime.time(00, 00, 00, 000000)
combo = datetime.datetime.combine(date, time)
hours = timezone.now().hour
minutes = timezone.now().minute
seconds = timezone.now().second
mseconds = timezone.now().microsecond
#convert timezone now to utc 04:00:00:000000 UTC
# this works now and doesnt throw a runtime error and gives me the correct data im expecting, but i think it will be an hour off during dst
t = timezone.now().replace(hour=04, minute=00, second=00, microsecond=000000)
#I also tired this but still didnt get the expected results:
e = Entry.objects.filter(member_id=1
).filter(prize_id=1
).filter(createdate__gte=timezone.now() -
datetime.timedelta(hours=datetime.datetime.now().hour,
minutes=datetime.datetime.now().minute,
seconds=datetime.datetime.now().second,
microseconds=datetime.datetime.now().microsecond))
有没有人有一种简单的方法来获取时间戳 >= 到今天的对象?