python - 如何在 Python 中设置财政年度的动态开始和结束日期
问题描述
我正在使用一个 python 脚本来处理财政年度及其各自的报告期。请记住,并非每个国家/地区(并且此脚本将在多个国家/地区使用)都没有相同的财政年度,我需要使其具有动态性。
我寻求的是财政年度不是从 1 月 1 日开始而是在不同日期开始的情况。例如
开课日期:2020-04-01(2020年4月1日)
结束日期:2021-03-31(2021 年 3 月 31 日)
这是代码:
start_month = self.data_handler.get_cell_value(excel_file, sheet, 'A2')
start_day = self.data_handler.get_cell_value(excel_file, sheet, 'B2')
end_month = self.data_handler.get_cell_value(excel_file, sheet, 'C2')
end_day = self.data_handler.get_cell_value(excel_file, sheet, 'D2')
for year_ in years:
if start_day != 1 or start_month != 1 or end_day != 31 or end_month != 12:
end_year = year_ + 1
else:
end_year = year_
new_fiscal_year = {
'code': str(year_),
'date_start': str(year_) + "-" + str('%02d' % start_month) + "-" + str('%02d' % start_day),
'date_stop': str(end_year) + "-" + str('%02d' % end_month) + "-" + str('%02d' % end_day),
'name': str(year_),
}
self.openerpclient.create('account.fiscalyear', new_fiscal_year)
current_fiscal_year = self.openerpclient.fetch('account.fiscalyear', [('code', 'like', str(year_))], ['id'])
for month_ in range(1, 13):
# last_day = Since different months have different numbers of days, here we save the last day of the corresponding month
last_day = calendar.monthrange(int(year_), month_)[1]
first_day_of_the_month = datetime(year=int(year_), month=month_, day=1).date()
last_day_of_the_month = datetime(year=int(year_), month=month_, day=last_day).date()
new_periods = {
'date_start': str(first_day_of_the_month),
'date_stop': str(last_day_of_the_month),
'fiscalyear_id': str(current_fiscal_year[0]['id']),
'name': str('%02d' % month_ + "/" + str(year_)),
'code': str('%02d' % month_ + "/" + str(year_)),
}
print("AA", new_periods)
self.openerpclient.create('account.period', new_periods)
但这不是我预期的输出,它是:
AA {'date_start': '2020-04-01', 'date_stop': '2020-04-31', 'fiscalyear_id': '1', 'name': '04/2020', 'code': '01/2020'}
AA {'date_start': '2020-05-01', 'date_stop': '2020-05-31', 'fiscalyear_id': '1', 'name': '05/2020', 'code': '02/2020'}
AA {'date_start': '2020-06-01', 'date_stop': '2020-06-31', 'fiscalyear_id': '1', 'name': '06/2020', 'code': '03/2020'}
AA {'date_start': '2020-07-01', 'date_stop': '2020-07-31', 'fiscalyear_id': '1', 'name': '07/2020', 'code': '04/2020'}
AA {'date_start': '2020-08-01', 'date_stop': '2020-08-31', 'fiscalyear_id': '1', 'name': '08/2020', 'code': '05/2020'}
AA {'date_start': '2020-09-01', 'date_stop': '2020-09-31', 'fiscalyear_id': '1', 'name': '09/2020', 'code': '06/2020'}
AA {'date_start': '2020-10-01', 'date_stop': '2020-10-31', 'fiscalyear_id': '1', 'name': '10/2020', 'code': '07/2020'}
AA {'date_start': '2020-11-01', 'date_stop': '2020-11-31', 'fiscalyear_id': '1', 'name': '11/2020', 'code': '08/2020'}
AA {'date_start': '2020-12-01', 'date_stop': '2020-12-01', 'fiscalyear_id': '1', 'name': '12/2020', 'code': '09/2020'}
AA {'date_start': '2021-01-01', 'date_stop': '2021-01-31', 'fiscalyear_id': '1', 'name': '01/2021', 'code': '10/2020'}
AA {'date_start': '2021-02-01', 'date_stop': '2021-02-28', 'fiscalyear_id': '1', 'name': '02/2021', 'code': '11/2020'}
AA {'date_start': '2021-03-01', 'date_stop': '2021-03-31', 'fiscalyear_id': '1', 'name': '03/2021', 'code': '12/2020'}
简而言之,我希望我的开始和结束日期与我工作的年份相对应,并进入下一年直到该财政年度的结束日期。
解决方案
试试这个方法
import datetime as dt
import time
delta = dt.timedelta(days=365)
def fiscal_years(num_of_years, fiscal_year_type='J'):
start_year = time.localtime().tm_year
if fiscal_year_type == 'J':
start_date = dt.date(start_year, 1, 1)
elif fiscal_year_type == 'A':
start_date = dt.date(start_year, 4, 1)
for i in range(num_of_years):
print {"start": str(start_date), "stop": str(start_date + delta)}
start_date = start_date + delta
fiscal_years(5, 'A')
输出
{'start': '2020-04-01', 'stop': '2021-04-01'}
{'start': '2021-04-01', 'stop': '2022-04-01'}
{'start': '2022-04-01', 'stop': '2023-04-01'}
{'start': '2023-04-01', 'stop': '2024-03-31'}
{'start': '2024-03-31', 'stop': '2025-03-31'}
还有一些更精细的细节(闰年和财政年度开始于 4 月和 1 月之外),但这应该给你一个想法
推荐阅读
- javascript - GraphQL 上的动态变量调用 - React - Gatsby
- html-select - Custom Blazor Select Option - not selecting initial item from bound value
- php - 如何在 WooCommerce 订单跟踪中显示 ajax 结果
- mysql - MySQL如何从数据库顺序中随机获取数据
- swiftui - 检测对 @Published 属性的 UI 更改
- c++ - *& 在 C++ 中做什么
- testing - ETL 和 QA 测试
- mariadb - 交易删除。SELECT 查询中缺少行。打开的事务是否仍然只为一个连接锁定“已删除”行?[MariaDb 10.3.11]
- aws-amplify - 可以在服务器端使用 Amplify Authentication SDK 吗?
- database - 每次我更新行时数据库行都会发生变化