首页 > 代码库 > 获得指定时间的前一个时间的分组

获得指定时间的前一个时间的分组

#include <iostream>#include <string>#include <vector>using namespace std;// /*******************************************************/// @brief 获得指定时间的开始结束时间列表////// @param: tTime 时间戳/// @param: iInterval 时间分段间隔(分钟)/// @param: iOffset 偏移量(分钟)////// @returns:   vec[0]:starttime  vec[1]:endtime// *******************************************************/vector<string> getTimeList(time_t tTime, int iInterval, int iOffset){    vector<string> timelist;    time_t newTime = tTime - iOffset * 60;    struct tm newTm;    localtime_r(&newTime, &newTm);    struct tm endTime = newTm;    endTime.tm_min = int(newTm.tm_min / iInterval) * iInterval;    endTime.tm_sec = 0;    char buf[64];    strftime(buf, 64, "%Y-%m-%d %H:%M:%S", &endTime);    timelist.insert(timelist.begin(), 1, buf);    time_t startTime = mktime(&endTime) - iInterval * 60;    localtime_r(&startTime, &newTm);    struct tm sTime = newTm;    sTime.tm_min = int(newTm.tm_min / iInterval) * iInterval;    sTime.tm_sec = 0;    memset(buf, 0, sizeof(buf));    strftime(buf, 64, "%Y-%m-%d %H:%M:%S", &sTime);    timelist.insert(timelist.begin(), 1, buf);    return timelist;}int main(){    time_t now = time(NULL);    vector<string> timelist = getTimeList(now, 20, 5);    cout<<"now is "<<now<<",start:"<<timelist[0]<<", "<<"end:"<<timelist[1]<<endl;    return 0;}

技术分享

 

获得指定时间的前一个时间的分组