日期倒推

vc中只提供了一个类CTimeSpan类根据“天”数来倒退日期,但在很多查询任务中需要用到倒推几个月或几年来获取起始时间,此时就不能简单的应用CTimeSpan类来实现了。网上没有找到相关的代码,就自己动手写了一下,下面四个函数分别实现倒推**天/**周/**月/**年,得到新的时间信息。


CTime OnGetLastDays(CTime curTime, int nDays)

{
CTime t1(curTime.GetYear(), curTime.GetMonth(), curTime.GetDay(), 0, 0, 0);
CTimeSpan ts(nDays, 0, 0, 0);
t1 = t1-ts;

return t1;
}


CTime OnGetLastWeeks(CTime curTime, int nWeeks)
{
return OnGetLastDays(curTime, 7*nWeeks);
}


CTime OnGetLastMonths(CTime curTime, int nMonths)
{
static int days[] = {31, 28, 31, 30, 31, 30 , 31, 31, 30, 31, 30, 31};


int nYear = curTime.GetYear();
int nMonth = curTime.GetMonth()-nMonths;
int nDay = curTime.GetDay();


if(nMonth < 1)
{
nYear -= 1;
nMonth += 12;
}


if(nDay > days[nMonth-1])
{
nDay = days[nMonth-1];
if(nMonth == 2)
{
//判断是否为闰年
if(My_IsLeapYear(nYear))
nDay = 29;
}
}


curTime = CTime(nYear, nMonth, nDay, 0, 0, 0);
return curTime;
}


CTime OnGetLastYears(CTime curTime, int nYears)
{
static int days[] = {31, 28, 31, 30, 31, 30 , 31, 31, 30, 31, 30, 31};


int nYear = curTime.GetYear()-nYears;
int nMonth = curTime.GetMonth();
int nDay = curTime.GetDay();

for(int i=0;i<nYears;i++)
{
if(My_IsLeapYear(nYear+i))
nDay -= 1;
}

if(nDay < 1)
{
nMonth -= 1;
nDay += days[nMonth-1];
}
else
{
if(nDay > days[nMonth-1])
{
nDay = days[nMonth-1];
if(nMonth == 2)
{
//判断是否为闰年
if(My_IsLeapYear(nYear))
nDay = 29;
}
}
}

curTime = CTime(nYear, nMonth, nDay, 0, 0, 0);


return curTime;
}
``` def get_5_workdays_back(end_date=None): """计算从结束日期倒推5个工作日的起始日期""" end_date = pd.Timestamp.now().normalize() if end_date is None else end_date count = 0 current_date = end_date # 复用原有节假日配置 holidays = [ # 元旦(不调休) '2025-01-01', # 周三 # 春节(调休2天) '2025-01-28', '2025-01-29', '2025-01-30', '2025-01-31', '2025-02-01', '2025-02-02', '2025-02-03', '2025-02-04', # 1.28(除夕)-2.4 # 清明节(不调休) '2025-04-04', '2025-04-05', '2025-04-06', # 周五-周日 # 劳动节(调休1天) '2025-05-01', '2025-05-02', '2025-05-03', '2025-05-04', '2025-05-05', # 周四-周一 # 端午节(不调休) '2025-05-31', '2025-06-01', '2025-06-02', # 周六-周一 # 中秋节+国庆节(调休2天) '2025-10-01', '2025-10-02', '2025-10-03', '2025-10-04', '2025-10-05', '2025-10-06', '2025-10-07', '2025-10-08' # 周三-下周三 ] holiday_dates = pd.to_datetime(holidays) # 新增调休工作日列表(转换为日期格式) workdays_adjustment = [ '2025-01-26', # 周日补春节 '2025-02-08', # 周六补春节 '2025-04-27', # 周日补劳动节 '2025-09-28', # 周日补国庆 '2025-10-11' # 周六补国庆 ] adjustment_dates = pd.to_datetime(workdays_adjustment) # 转换日期格式 while count < 5: current_date -= pd.Timedelta(days=1) # 判断是否为有效工作日 is_workday = ( (current_date.weekday() < 5 or current_date in adjustment_dates) and current_date not in holiday_dates ) if is_workday: count += 1 return current_date, end_date # 获取处理后的数据 df = getchaoshi() # 计算时间范围 start_date, end_date = get_5_workdays_back() date_mask = (df['收案时间'] >= start_date) & (df['收案时间'] <= end_date) weekly_cases = df[date_mask].copy() # 添加状态分类列 weekly_cases['状态分类'] = weekly_cases['案件状态'].apply( lambda x: '已完成' if x in ['办结', '发件'] else '正在处理' ) # 分组统计 result = weekly_cases.groupby('组别').agg( 总案件数=('案件状态', 'count'), 已完成=('状态分类', lambda x: (x == '已完成').sum()), 处理中=('状态分类', lambda x: (x == '正在处理').sum()), 超时案件数=('是否超时', lambda x: (x == '是').sum()), 缓办案件数=('是否缓办', lambda x: (x == '是').sum()) # 新增行 ).reset_index() # 新增:添加总计行(核心修改点) total_row = { '组别': '总计', '总案件数': result['总案件数'].sum(), '已完成': result['已完成'].sum(), '处理中': result['处理中'].sum(), '超时案件数': result['超时案件数'].sum(), '缓办案件数': result['缓办案件数'].sum() } result = pd.concat([result, pd.DataFrame([total_row])], ignore_index=True) # 结果展示样例 print(result.to_markdown(index=False)) # 创建Excel写入对象(注意:需要安装openpyxl) with ExcelWriter('GCB案件统计结果t3.xlsx', engine='openpyxl') as writer: # 将result写入Sheet1 result.to_excel(writer, sheet_name='按周统计情况', index=False) # 将原始数据写入Sheet2 weekly_cases.to_excel(writer, sheet_name='本周案件', index=False) # 将原始数据写入Sheet3 df.to_excel(writer, sheet_name='所有案件', index=False)```代码获取了“收案时间”在以程序执行时间(结束时间)到往前5个工作日(开始时间)内的案件,增加输出结果列“上周宗案件数”,即“收案时间”在以程序执行时间往前5个工作日作为结束时间到往前10个工作日(开始时间)内的案件,作为上周的案件,同样分组统计,到“上周宗案件数”
最新发布
03-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值