std::map<int, std::pair<CTime, CTime>> YearOfWeek(int year) { std::map<int, std::pair<CTime, CTime>> weekMap; CTime time(year, 1, 1, 0, 0, 0); int dow = time.GetDayOfWeek(); //第一周比较特殊,先拿第一周的数据 int idx = 1; int firstWeek[] = { 0, 0, 6, 5, 4, 3, 2, 1 };//1.1号到星期日需要几天 weekMap[idx++] = std::make_pair(time, time + CTimeSpan(firstWeek[dow], 23, 59, 59)); //第二周开始,时间调整到星期一 time = weekMap[idx - 1].second + CTimeSpan(0, 0, 0, 1);//加一秒 while (year == time.GetYear()) { CTime weekst = time; time += CTimeSpan(6, 23, 59, 59); weekMap[idx++] = std::make_pair(weekst, time); time += CTimeSpan(0, 0, 0, 1); } //最后一周比较特殊,今年可能不足一周,直接将最后的结束时间置为12.31 23:59:59 weekMap[--idx].second = CTime(year, 12, 31, 23, 59, 59); return weekMap; }
以上代码按照中国人习惯将星期一设置为一周的第一天,星期日是最后一天,输入2022,执行结果:
标签:59,idx,获取,int,year,C++,weekMap,一年,time From: https://www.cnblogs.com/kuaixue/p/16949276.html