gpt4 book ai didi

python - RuntimeWarning : DateTimeField Model. 当时区支持处于事件状态时,日期收到了幼稚的日期时间

转载 作者:行者123 更新时间:2023-12-03 08:13:22 26 4
gpt4 key购买 nike

我正在尝试过滤查询集以获取今年的所有帖子。

def thisYearQuerySet(objects):
start_day = datetime.date(datetime.date.today().year, 1, 1)
end_day = datetime.date(datetime.date.today().year, 12, 31)
return objects.filter(date__range=[start_day, end_day])

django 提示 start_dayend_day声明可能会发生冲突django.utils.timezone ,我认为这没什么大不了的。


但是这个警告很烦人,任何关于忽略它的建议(而不是禁用 django 警告)将不胜感激。比如,如何从 django.utils 获取一年中的第一天和最后一天

完整警告

RuntimeWarning: DateTimeField Model.date received a naive datetime (2021-01-01 00:00:00) while time zone support is active.
RuntimeWarning: DateTimeField Model.date received a naive datetime (2021-12-31 00:00:00) while time zone support is active.

看来我必须设置一个区域来消除警告。

我附上了我的项目中实现的模块,其中的方法可能会对您有所帮助。

import pytz
import datetime
from django.utils import timezone
from django.conf import settings
from django.db.models import Q
from django.utils.dateparse import parse_datetime


# MM/DD/YY
def when(create):
return '%s/%s/%s' % (create.month, create.day, create.year)


def current():
return when(timezone.now())


# create: date of object creation
# now: time now
# li: a list of string indicate time (in any language)
# lst: suffix (in any language)
# long: display length
def howLongAgo(
create,
now,
# li=[
# 'sec',
# 'min',
# 'h',
# 'day',
# 'week',
# 'month',
# 'year',
# ],
li=[
'秒',
'分',
'时',
'天',
'周',
'月',
'年',
],
# lst='ago',
lst='之前',
long=2
):
dif = create - now
sec = dif.days * 24 * 60 * 60 + dif.seconds
minute = sec // 60
sec %= 60
hour = minute // 60
minute %= 60
day = hour // 24
hour %= 24
week = day // 7
day %= 7
month = (week * 7) // 30
week %= 30
year = month // 12
month %= 12
s = []
for ii, tt in enumerate([sec, minute, hour, day, week, month, year]):
ss = li[ii]
if tt != 0:
s.append(str(tt) + ss)
# if tt == 1:
# s.append(str(tt) + ss)
# else:
# s.append(str(tt) + ss + 's')

return ' '.join(list(reversed(s))[:long]) + lst


# conversion
def dateToDatetime(li):
res = []
for ar in li:
res.append(datetime.datetime.combine(ar, datetime.datetime.min.time()))
return res


def datespan(startDate, endDate, delta=datetime.timedelta(days=1)):
currentDate = startDate
while currentDate < endDate:
yield currentDate
currentDate += delta


# queryset
def thisMonthQuerySet(objects):
today = timezone.now()
year = today.year
start_day = pytz.timezone('Asia/Shanghai').localize((datetime.datetime(year, today.month, 1)))
end_day = start_day + datetime.timedelta(30)
return objects.filter(date__range=[start_day, end_day])


def lastMonthQuerySet(objects):
today = timezone.now()
year = today.year
month = today.month - 1
if month == 0:
year -= 1
month = 12
start_day = pytz.timezone('Asia/Shanghai').localize((datetime.datetime(year, month, 1)))
end_day = start_day + datetime.timedelta(30)
return objects.filter(date__range=[start_day, end_day])


def thisYearQuerySet(objects):
today = timezone.now()
start_day = pytz.timezone('Asia/Shanghai').localize((datetime.datetime(today.year, 1, 1)))
end_day = pytz.timezone('Asia/Shanghai').localize((datetime.datetime(today.year + 1, 1, 1)))
return objects.filter(date__range=[start_day, end_day])


def lastYearQuerySet(objects):
today = timezone.now()
start_day = pytz.timezone('Asia/Shanghai').localize((datetime.datetime(today.year - 1, 1, 1)))
end_day = pytz.timezone('Asia/Shanghai').localize((datetime.datetime(today.year, 1, 1)))
return objects.filter(date__range=[start_day, end_day])


def lastQuaterQuerySet(objects):
today = timezone.now()
four_month_before = today - timezone.timedelta(30 * 4)
start_day = pytz.timezone('Asia/Shanghai').localize((datetime.datetime(four_month_before.year, four_month_before.month, 1)))
return objects.filter(date__gte=start_day)


# timespan
def getLastWeek():
date = datetime.date.today()
end_day = date - datetime.timedelta(date.weekday())
start_day = end_day - datetime.timedelta(7)
return dateToDatetime([day for day in datespan(start_day, end_day)])


def getThisWeek():
date = datetime.date.today()
start_day = date - datetime.timedelta(date.weekday())
end_day = start_day + datetime.timedelta(7)
return dateToDatetime([day for day in datespan(start_day, end_day)])


def getLastMonth():
today = datetime.date.today()
start_day = pytz.timezone('Asia/Shanghai').localize((datetime.datetime(today.year, today.month - 1 if today.month - 1 != 0 else 1, 1)))
end_day = start_day + datetime.timedelta(30)
return [day for day in datespan(start_day, end_day)]


def getThisMonth():
today = datetime.date.today()
start_day = pytz.timezone('Asia/Shanghai').localize((datetime.datetime(today.year, today.month, 1)))
end_day = start_day + datetime.timedelta(30)
return [day for day in datespan(start_day, end_day)]


def getLastYear():
today = datetime.date.today()
start_day = pytz.timezone('Asia/Shanghai').localize((datetime.datetime(today.year - 1, 1, 1)))
end_day = pytz.timezone('Asia/Shanghai').localize((datetime.datetime(today.year, 1, 1)))
return [day for day in datespan(start_day, end_day)]


def getThisYear():
today = datetime.date.today()
start_day = pytz.timezone('Asia/Shanghai').localize((datetime.datetime(today.year, 1, 1)))
end_day = pytz.timezone('Asia/Shanghai').localize((datetime.datetime(today.year + 1, 1, 1)))
return [day for day in datespan(start_day, end_day)]


# exclude
def dayNotIn(objects):
return objects.filter(~Q(date=timezone.now()))


def weekNotIn(objects):
return objects.filter(date__lt=timezone.now() - datetime.timedelta(days=7))


def monthNotIn(objects):
return objects.filter(date__lt=timezone.now() - datetime.timedelta(days=30))


def yearNotIn(objects):
return objects.filter(date__lt=timezone.now() - datetime.timedelta(days=365))


# month only
def getQSbyYM(objects, y, m):
# m in [0, 12]
em = m + 1
ey = y
if em == 13:
em = 1
ey = y + 1
start_day = pytz.timezone('Asia/Shanghai').localize((datetime.datetime(y, m, 1)))
end_day = pytz.timezone('Asia/Shanghai').localize((datetime.datetime(ey, em, 1)))
return objects.filter(date__lt=end_day, date__gte=start_day)


def createInToday(objects):
today = timezone.now()
start_day = pytz.timezone('Asia/Shanghai').localize((datetime.datetime(today.year, today.month, today.day)))
end_day = start_day + datetime.timedelta(1)
return objects.filter(date__lt=end_day, date__gte=start_day)


def dtstr(dt) -> str:
return dt.strftime('%Y-%m-%dT%H:%M:%S')


def strdt(st):
return pytz.timezone('Asia/Shanghai').localize((parse_datetime(st)))

修改date__根据模型日期时间字段名称,还可以根据您网站的工作位置更改时区。

最佳答案

django moans about start_day and end_day declaration may conflicts django.utils.timezone, I think it is not a big deal.

这取决于您能承受的风险程度。如果不考虑用户的时区,thisYearQuerySet 可能会在临近新年时返回去年或当前年份的对象。

这是因为服务器可能在 GMT 时间运行,这意味着在 2022-01-01 00:00:00T00:00,它将开始返回 2022 年日期的数据。但是,任何使用该应用程序的美国人都会还需要 4 个多小时才能知道 2021 年,并希望在午夜之前看到 2021 年的数据。

但是,如果您试图消除警告并且不关心上述情况,请不要在获取今天/现在时使用 python 日期时间函数。使用:

from django.utils import timezone
now = timezone.now()
today = timezone.now().date()

关于python - RuntimeWarning : DateTimeField Model. 当时区支持处于事件状态时,日期收到了幼稚的日期时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70185513/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com