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

时间:2015-04-21 15:12:46

标签: python django datetime calendar

我正在尝试列出date_and_timestart_date之间end_date属性的对象列表

start_dateend_date存储在数据库中,我将数据库字段调用为这样简单:

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

我正在尝试为范围内的每个日期创建一个包含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)

不幸的是 - 我最终得到了这个清单:

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

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

干杯,亚瑟

2 个答案:

答案 0 :(得分:0)

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

生成样本数据:

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

按日期对列表进行排序:

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

答案 1 :(得分:0)

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

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

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

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

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

接着是

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

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