对时间按指定时间段进行分组,可用于并发度计算
2016-08-30 14:47
141 查看
#include <iostream> #include <string> #include <sstream> #include <vector> using namespace std; unsigned long long strToull(const string & a_str) { #if defined (WIN32) return strtoul(a_str.c_str(), NULL, 10); #else return strtoull(a_str.c_str(), NULL, 10); #endif } vector<string> getGroupList(int a_iGroup, const string &a_strStartTime, const string &a_strEndTime) { time_t tStartTime = strToull(a_strStartTime.c_str()); time_t tEndTime = strToull(a_strEndTime.c_str()); //cout<<"begin "<<tStartTime<<" end "<<tEndTime<<endl; struct tm tmNow; tm* pTm = localtime_r(&tStartTime, &tmNow); struct tm *tmNew = new tm(); *tmNew = *pTm; tmNew->tm_min = int(pTm->tm_min/a_iGroup) * a_iGroup; tmNew->tm_sec = 0; char tmBuf[64]; memset(tmBuf, 0, sizeof(tmBuf)); strftime(tmBuf, 64, "%Y-%m-%d %H:%M:00", tmNew); stringstream str; str<<a_iGroup<<"-"<<tmBuf; vector<string> valuelist; valuelist.push_back(str.str()); time_t tBegin = mktime(tmNew) + a_iGroup * 60; while(tBegin < tEndTime) { struct tm tmTime; localtime_r(&tBegin, &tmTime); memset(tmBuf, 0, sizeof(tmBuf)); strftime(tmBuf, 64, "%Y-%m-%d %H:%M:00", &tmTime); stringstream strs; strs<<a_iGroup<<"-"<<tmBuf; valuelist.push_back(strs.str()); tBegin += a_iGroup * 60; } return valuelist; } int main(int argc, char* argv[]) { int group = 1; if(argc == 2) { group = atoi(argv[1]); } else { cout<<"help:"<<endl; cout<<" ./get group"<<endl; return 0; } vector<string> valuelist = getGroupList(group, "1472111395", "1472112495"); for(vector<string>::iterator iter = valuelist.begin(); iter != valuelist.end(); ++iter) { cout<<"value is:"<<*iter<<endl; } return 0; }
g++ getgrouplist.cpp -o get
输出:
相关文章推荐
- ACCESS数据库时间应用之按时间段对字段进行分组
- Oracle函数参考(用于计算某个数值所表示的时间)
- 【代码保留】时间器(用于计算时间间隔的类)
- 时间工具类,主要用于sql的时间段查询
- 计算指定时间与当前的时间差 比如,3天前、10分钟前
- 查询分组计算[按时间分组计算][SQL]
- 如何计算时间戳(就是指定时间到1970年1月1日的秒数)
- Asp利用DateAdd 函数进行时间相加计算问题
- SQL Server中按照时间进行分组统计
- 用sql语句按指定时间段分组统计
- PHP计算指定时间段内周末天数(星期日天数)、双休天数、总工作日天数
- SQL中获取指定两个日期时间点或者时间段内所有记录语句
- 输入日期,计算出与当前系统日期相隔多少天(获取系统时间,年月日分别获取,对其进行格式化simpledateformat;输入的日期类似操作)
- mysql date_format 按不同时间单位进行分组统计
- 根据指定两个日期计算出这些时间内有多少天是周末 php程序函数代码
- 【代码保留】时间器(用于计算时间间隔的类)
- 计算指定日期的时间间隔
- 时间进行获取和计算
- 根据指定两个日期计算出这些时间内有多少天是周末 php程序函数代码
- PHP计算指定时间段内周末天数(星期日天数)、双休天数、总工作日天数