Python日期时间范围介于start_date和end_date之间

时间:2022-07-15 10:20:22

I am trying to make a list of objects with a date_and_time attribute between a start_date and end_date

我试图在start_date和end_date之间创建一个具有date_and_time属性的对象列表

start_date and end_date are stored in a database and I call the database fields simple like this:

start_date和end_date存储在数据库中,我将数据库字段调用如下:

>>> data = Oncall.objects.all()
>>> data
[<Oncall: Oncall object>, <Oncall: Oncall object>, <Oncall: Oncall object>, <Oncall: Oncall object>]

I am trying to create a list with oncall objects for each date within the range as follows:

我正在尝试为范围内的每个日期创建一个包含oncall对象的列表,如下所示:

>>> event_list = []
>>> for i in range(1, calendar.monthrange(datetime.now().year, datetime.now().month)[1]):
...  for d in data:
...   if d.start_date <= datetime(year=datetime.now().year, month=datetime.now().month, day=i) <= d.end_date:
...    oncall=d
...    oncall.date_and_time=datetime(year=datetime.now().year, month=datetime.now().month, day=i)
...    event_list.append(oncall)

Unfortunately - I end up with this list:

不幸的是 - 我最终得到了这个列表:

>>> for i in event_list:
...  print str(i.user_id) + ' ' + str(i.date_and_time)
... 
amencke 2015-04-20 00:00:00
amencke 2015-04-20 00:00:00
amencke 2015-04-20 00:00:00
amencke 2015-04-20 00:00:00
amencke 2015-04-20 00:00:00
amencke 2015-04-20 00:00:00
amencke 2015-04-20 00:00:00
jbolggs 2015-04-27 00:00:00
jbolggs 2015-04-27 00:00:00
jbolggs 2015-04-27 00:00:00
jbolggs 2015-04-27 00:00:00
jbolggs 2015-04-27 00:00:00
jbolggs 2015-04-27 00:00:00
jbolggs 2015-04-27 00:00:00
jdoe 2015-04-29 00:00:00
jdoe 2015-04-29 00:00:00

Any ideas on how I could get the date to increment correctly?

关于如何让日期正确递增的任何想法?

Cheers, Arthur

2 个解决方案

#1


0  

I might not have properly understood the question. If that is the case please comment and I will update the answer.

我可能没有正确理解这个问题。如果是这种情况请评论,我会更新答案。

Generate sample data:

生成样本数据:

import datetime
import operator

mylist = []
# Demo class
class myclass:
    def __init__(self, name, date):
        self.name = name
        self.date = date

# List creation
names=["amencke", "amencke", "amencke", "amencke", "amencke", "amencke", "amencke", "jbolggs", "jbolggs", "jbolggs", "jbolggs", "jbolggs", "jbolggs", "jbolggs", "jdoe", "jdo"]
date=["2015-04-28 00:00:00", "2015-04-05 00:00:00", "2015-04-20 00:00:00", "2015-04-20 00:00:00", "2015-04-20 00:00:00", "2015-04-20 00:00:00", "2015-04-20 00:00:00", "2015-04-27 00:00:00", "2015-04-27 00:00:00", "2015-04-27 00:00:00", "2015-04-27 00:00:00", "2015-04-27 00:00:00", "2015-04-27 00:00:00", "2015-04-27 00:00:00", "2015-04-29 00:00:00", "2015-04-29 00:00:00"]        
#Create the list
for i in range(len(names)):
    mylist.append(myclass(names[i],datetime.datetime.strptime(date[i], "%Y-%m-%d %H:%M:%S")))

Sorting the list by date:

按日期排序列表:

sorted_mylist = sorted(mylist, key=operator.attrgetter('date'))
#print the original list
for i in range(len(mylist)):
    print mylist[i].name, mylist[i].date
#print the sorted list
for i in range(len(sorted_mylist)):
    print sorted_mylist[i].name, mylist[i].date
#sort and print the original list
mylist.sort(key=operator.attrgetter('date'))
for i in range(len(mylist)):
    print mylist[i].name, mylist[i].date

#2


0  

It seems oncall is a pointer to the d objects. You are effectively setting (and overwriting!) the matching d object's date_and_time to midnight on the i day:

似乎oncall是指向d对象的指针。您有效地将匹配的d对象的date_and_time设置(并覆盖!)到第i天的午夜:

oncall=d
oncall.date_and_time=datetime(year=datetime.now().year, month=datetime.now().month, day=i)

The list you print in the end contains multiple pointers to the same Oncall objects which have date_and_time set to the last i day for which the condition was met.

最后打印的列表包含多个指向同一Oncall对象的指针,这些对象的date_and_time设置为满足条件的最后一天。

Something like this could be closer to what you expect (assuming I get your intention correctly):

这样的事情可能更接近你的期望(假设我的意图正确):

date_and_time=datetime(year=datetime.now().year, month=datetime.now().month, day=i)
event_list.append((d, date_and_time))

followed by

for d, date_and_time in event_list:
    print str(d.user_id) + ' ' + str(date_and_time)

I'd also suggest computing year and month once instead of every loop iteration - would greatly improve your code's readability (and even speed a bit).

我还建议一次计算年份和月份,而不是每次循环迭代 - 这将大大提高代码的可读性(甚至可以提高速度)。

#1


0  

I might not have properly understood the question. If that is the case please comment and I will update the answer.

我可能没有正确理解这个问题。如果是这种情况请评论,我会更新答案。

Generate sample data:

生成样本数据:

import datetime
import operator

mylist = []
# Demo class
class myclass:
    def __init__(self, name, date):
        self.name = name
        self.date = date

# List creation
names=["amencke", "amencke", "amencke", "amencke", "amencke", "amencke", "amencke", "jbolggs", "jbolggs", "jbolggs", "jbolggs", "jbolggs", "jbolggs", "jbolggs", "jdoe", "jdo"]
date=["2015-04-28 00:00:00", "2015-04-05 00:00:00", "2015-04-20 00:00:00", "2015-04-20 00:00:00", "2015-04-20 00:00:00", "2015-04-20 00:00:00", "2015-04-20 00:00:00", "2015-04-27 00:00:00", "2015-04-27 00:00:00", "2015-04-27 00:00:00", "2015-04-27 00:00:00", "2015-04-27 00:00:00", "2015-04-27 00:00:00", "2015-04-27 00:00:00", "2015-04-29 00:00:00", "2015-04-29 00:00:00"]        
#Create the list
for i in range(len(names)):
    mylist.append(myclass(names[i],datetime.datetime.strptime(date[i], "%Y-%m-%d %H:%M:%S")))

Sorting the list by date:

按日期排序列表:

sorted_mylist = sorted(mylist, key=operator.attrgetter('date'))
#print the original list
for i in range(len(mylist)):
    print mylist[i].name, mylist[i].date
#print the sorted list
for i in range(len(sorted_mylist)):
    print sorted_mylist[i].name, mylist[i].date
#sort and print the original list
mylist.sort(key=operator.attrgetter('date'))
for i in range(len(mylist)):
    print mylist[i].name, mylist[i].date

#2


0  

It seems oncall is a pointer to the d objects. You are effectively setting (and overwriting!) the matching d object's date_and_time to midnight on the i day:

似乎oncall是指向d对象的指针。您有效地将匹配的d对象的date_and_time设置(并覆盖!)到第i天的午夜:

oncall=d
oncall.date_and_time=datetime(year=datetime.now().year, month=datetime.now().month, day=i)

The list you print in the end contains multiple pointers to the same Oncall objects which have date_and_time set to the last i day for which the condition was met.

最后打印的列表包含多个指向同一Oncall对象的指针,这些对象的date_and_time设置为满足条件的最后一天。

Something like this could be closer to what you expect (assuming I get your intention correctly):

这样的事情可能更接近你的期望(假设我的意图正确):

date_and_time=datetime(year=datetime.now().year, month=datetime.now().month, day=i)
event_list.append((d, date_and_time))

followed by

for d, date_and_time in event_list:
    print str(d.user_id) + ' ' + str(date_and_time)

I'd also suggest computing year and month once instead of every loop iteration - would greatly improve your code's readability (and even speed a bit).

我还建议一次计算年份和月份,而不是每次循环迭代 - 这将大大提高代码的可读性(甚至可以提高速度)。