gpt4 book ai didi

python-3.x - odoo 11/Python 3 : How to find expiry date of subscription if some weekdays are excluded

转载 作者:行者123 更新时间:2023-12-01 03:09:36 25 4
gpt4 key购买 nike

我正在研究订阅管理系统。我在计算订阅到期日时遇到问题。场景如下,
如果一个人订阅了 1 个月,比如 1/11/2018 到 30/11/2018 [DD/MM/YYYY] 总共 30 天,但他想从每周 30 天中排除周五和周六。那么我应该如何计算到期日?

Logic is : Say End Date = Expiry Date then find Fri/Sat from 1/11/2018 to 30/11/2018 which comes out 5 Fri & 4 Sat = 9 days. Add to Expiry Date which will be 09/12/2018. Now search Fur & Sat between End Date & Expiry Date which comes out 1 Fri & 2 Sat = 3 days. Now End Date = Expiry Date and Expiry Date + 3 Days = 12/12/2018. Search between End Date & Expiry Date for Fri & Sat which is 0 so the Expiry Date is 12/12/2018 return value <<



以下代码执行此操作,但方法返回 09/12/2018 而不是 12/12/2018。这有什么问题??
@api.one
def get_expiry_date(self, start, end, day_selection):
print("i am in 2nd", start, end, day_selection)
dayformat = '%Y-%m-%d'
current_date = datetime.now().date()
if start:
start = datetime.strptime(str(start), dayformat).date()
if end:
end = datetime.strptime(str(end), dayformat).date()
if day_selection:
selected_days = day_selection
if start < current_date:
start = datetime.strptime(str(start), dayformat).date()
weekdays = self.weekday_count(start,end)
print("days for start and end date",start,end, day_selection)
adddays = 0
if weekdays:
for i in range(len(day_selection)):
for item in weekdays[0]:
weekdays_dict = item
print("dict", type(weekdays), type(weekdays[0]), weekdays_dict)
print("compare", selected_days[i], weekdays_dict, selected_days[i] == weekdays_dict)
if selected_days[i] == item:
adddays = adddays + weekdays[0].get(item)
new_start = end
end = datetime.strptime(str(end), dayformat).date() + timedelta(days=adddays)
start = new_start
print("New Expiry Date", start, end, adddays)
if adddays > 0:
self.get_expiry_date(start, end, day_selection)
print("type of end is ", type(end))
print("selected days are", selected_days[i],weekdays[0], weekdays[0].get(item), adddays)
print("last returned values is",end)

返回结束

最佳答案

在你的问题中,没有 day_selection 的定义也不是 weekday_count因此很难看出发生了什么。可能问题在于递归,这不是必需的。

day_selection定义为选择排除的天数( strftime('%a') 的列表,如 ['Mon', 'Tue'] ),然后:

from datetime import datetime, timedelta

def get_expiry_date(start, end, day_selection):
dayformat = '%Y-%m-%d'
weekdays= {}
start = datetime.strptime(str(start), dayformat).date()
end = datetime.strptime(str(end), dayformat).date()
# Count weekdays
for i in range((end - start).days+1):
weekday = (start + timedelta(i)).strftime('%a')
weekdays[weekday] = 1 + weekdays.setdefault(weekday, 0)
# Count subscription days to add
sub_days_to_add = 0
for selected in day_selection:
sub_days_to_add += weekdays.setdefault(selected, 0)
# Count calender days to add
cal_days_extension = 0
while sub_days_to_add > 0:
if (end + timedelta(days=cal_days_extension + 1)).strftime('%a') not in day_selection:
sub_days_to_add -= 1
cal_days_extension += 1
# Add to end day
return end + timedelta(days=cal_days_extension)

测试:
print (get_expiry_date('2018-11-01', '2018-11-30', ['Fri', 'Sat']))
# ---> 2018-12-12
print (get_expiry_date('2018-11-01', '2018-11-30', []))
# ---> 2018-11-30

此外,使用 from odoo.tools import DEFAULT_SERVER_DATE_FORMAT 会更安全比 dayformat = '%Y-%m-%d' .对于像 start 这样的参数& end ,如果它们是强制性的,可以在 Odoo 中使这些字段成为必需的。

关于python-3.x - odoo 11/Python 3 : How to find expiry date of subscription if some weekdays are excluded,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53427062/

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