zoukankan      html  css  js  c++  java
  • 获得指定时间的前一个时间的分组

    #include <iostream>
    #include <string>
    #include <vector>
    #include <sstream>
    using namespace std;
    
    
    
    // /*******************************************************
    /// @brief 获得指定时间的开始结束时间列表
    ///
    /// @param: tTime 时间戳
    /// @param: iInterval 时间分段间隔(分钟)
    /// @param: iOffset 偏移量(分钟)
    ///
    /// @returns:   vec[0]:starttime  vec[1]:endtime vec[2]:sleeptime
    // *******************************************************/
    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);
    
        stringstream str;
        str<<mktime(&endTime) + iInterval * 60 + iOffset * 60 - tTime;
        timelist.push_back(str.str());
    
        return timelist;
    }
    
    // /*******************************************************
    /// @brief 字符串转换为时间
    ///
    /// @param: a_strTimeStr
    ///
    /// @returns:  时间戳
    // *******************************************************/
    time_t timeStr2Timestamp(const string &a_strTimeStr)
    {
        string::size_type pos = a_strTimeStr.find("-");
        string normalTime = "";
        if(pos != string::npos)
            normalTime = a_strTimeStr;
        else
        {
            normalTime = a_strTimeStr.substr(0, 4) + "-" + a_strTimeStr.substr(4,2) + "-" + 
                a_strTimeStr.substr(6,2) + " " + a_strTimeStr.substr(8,2) + ":" + a_strTimeStr.substr(10,2) + ":" + a_strTimeStr.substr(12,2); } tm time; strptime(normalTime.c_str(), "%Y-%m-%d %H:%M:%S", &time); return mktime(&time); } int main() { time_t now = time(NULL); vector<string> timelist = getTimeList(now, 20, 5); cout<<"now is "<<now<<",start:"<<timelist[0]<<", "<<"end:"<<timelist[1]<<",sleep "<<timelist[2]<<" seconds"<<endl; cout<<"@1 str to time is "<<timeStr2Timestamp("20160922173000")<<endl; cout<<"@2 str to time is "<<timeStr2Timestamp("2016-09-22 17:30:00")<<endl; return 0; }

  • 相关阅读:
    Java 获取当前时间最近12个月(字符串)
    Java 取本月第一天和最后一天
    find_in_set()和in()比较
    Latex 箭头、下标、符号上下写文字、正方形和三角形
    LaTeX 公式字体大小设置
    哈希学习(2)—— Hashing图像检索资源
    局部敏感哈希-Locality Sensitivity Hashing
    Mathtype 公式显示方框
    Linux下安装MATLAB
    局部敏感哈希(Locality-Sensitive Hashing, LSH)
  • 原文地址:https://www.cnblogs.com/foreverstars/p/5895434.html
Copyright © 2011-2022 走看看