通过日期时间点,获取该日期所属周时间段,比如20190117 返回20190114-20190120
string GetWeekPeriodByDate(string sDate){ string sPeriod = ""; int nLength = sDate.length(); if (8 != nLength) return sPeriod; int y = atoi(sDate.substr(0, 4).c_str()); int m = atoi(sDate.substr(4, 2).c_str()); int d = atoi(sDate.substr(6, 2).c_str()); //检测时间有效性 if (y < 1900) return sPeriod; if (m < 1 || m > 12) return sPeriod; if (d < 1 || d > 31) return sPeriod; //日期 转 时间戳 struct tm t; t.tm_year = y - 1900;//年份,其值等于实际年份减去1900 t.tm_mon = m - 1; //月份(从一月开始,0代表一月) - 取值区间为[0,11] t.tm_mday = d; //一个月中的日期 - 取值区间为[1,31] t.tm_hour = 0; //时 - 取值区间为[0,23] t.tm_min = 0; //分 - 取值区间为[0,59] t.tm_sec = 0; //秒 – 取值区间为[0,59] t.tm_isdst = 0; //夏令时标识符,实行夏令时的时候,tm_isdst为正。不实行夏令时的进候,tm_isdst为0 time_t t_of_day; t_of_day = mktime(&t); //获取日期为星期几 if (m == 1 || m == 2) //把一月和二月换算成上一年的十三月和是四月 { m += 12; y--; } int nDayth = (d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400) % 7; //获取日期对应同属周 星期一和星期日 time_t t_of_monday = t_of_day - nDayth * 24 * 3600; time_t t_of_sunday = t_of_day + (6 - nDayth) * 24 * 3600; char s[100]; struct tm *p = localtime(&t_of_monday); strftime(s, sizeof(s), "%Y%m%d", p); string sMonday(s); p = localtime(&t_of_sunday); strftime(s, sizeof(s), "%Y%m%d", p); string sSunday(s); sPeriod = sMonday + "-" + sSunday; return sPeriod;}