首页 > 代码库 > c++时间增加
c++时间增加
// 123.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <vector>
#include <iostream>
#include <stdio.h>
#include <windows.h>
#include <string>
using namespace std;
void SplitSpring(const string &src,const char &csplit, vector<string> &vResult)
{
string sTemp = "";
for(unsigned int iIndex = 0; iIndex < src.length(); iIndex ++)
{
if(src[iIndex] == csplit)
{
vResult.push_back(sTemp);
sTemp = "";
continue;
}
sTemp += src[iIndex];
}
if("" != sTemp)
{
vResult.push_back(sTemp);
}
}
string IntToTheString(int &iValue)
{
char cTemp[100];
sprintf(cTemp,"%02d",iValue);
string sTemp(cTemp);
return sTemp;
}
string GetAbsolulyTime(const string &sLastTime,const int &iAddSeconds)
{
vector<string> vResult;
SplitSpring(sLastTime,‘:‘,vResult);
if(vResult.size() < 3)
{
return sLastTime;
}
int iHour =atoi(vResult[0].c_str());
int iMinite =atoi(vResult[1].c_str());
int iSecond =atoi(vResult[2].c_str());
int iAddTime = 0;
iSecond += iAddSeconds;
if(iSecond >= 60)
{
iAddTime = iSecond / 60;
iSecond = iSecond % 60;
iMinite += iAddTime;
if(iMinite >= 60)
{
iAddTime = iMinite / 60;
iMinite = iMinite % 60;
iHour += iAddTime;
}
}
string sHour = IntToTheString(iHour);
string sMinite= IntToTheString(iMinite);
string sSecond= IntToTheString(iSecond);
return sHour+":"+sMinite+":"+sSecond;
}
int _tmain(int argc, _TCHAR* argv[])
{
string sTest = "00:00:00";
string sResult;
while(true)
{
sTest = GetAbsolulyTime(sTest,80);
Sleep(1000);
cout<<"Time is"<<sTest<<endl;
}
return 0;
}
本文出自 “风清扬song” 博客,请务必保留此出处http://2309998.blog.51cto.com/2299998/1532452