码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-09-22 11:29:24      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:

#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;
}

技术分享

 

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

标签:

原文地址:http://www.cnblogs.com/foreverstars/p/5895434.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!