关于time的一些操作
0. C/C++获取系统时间戳,精确到毫秒
- #include <stdio.h>
- #include <sys/time.h> //添加头文件
- int64_t getCurrentTime() //直接调用这个函数就行了,返回值最好是int64_t,long long应该也可以
- {
- struct timeval tv;
- gettimeofday(&tv,NULL); //该函数在sys/time.h头文件中
- return tv.tv_sec * 1000 + tv.tv_usec / 1000;
- }
- int main()
- {
- std::cout<<"nowTime: "<<getCurrentTime()<<"\n"; //如果想要到秒级别的,只要除以1000就行了
- return 0;
- }
另一种方法获取系统当前时间戳, 单位为秒
#include <time.h>
time_t now;
time(&now); //获取time_t类型的当前时间 ,单位为秒
int unixTime = (int)time(&now);
char buf[128];
time_t nowtime;
struct tm * timeinfo;
int now = (int)time(&nowtime); //获取时间戳,单位秒
timeinfo = localtime(&nowtime); // 转换到结构体
strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", timeinfo);
strcpy(buffer,buf); //保存到char数组
string stime(buf); //保存成string
//string stime = buf; 也可以这样写
//时间戳转换为字符串的另一种方法
long nCapTime;
CTime ctime(nCapTime);
char capTime[64];
memset(capTime, 0, sizeof(capTime));
sprintf(capTime, "%04d-%02d-%02d %02d:%02d:%02d", ctime.GetYear(), ctime.GetMonth(), ctime.GetDay(),
、ctime.GetHour(), ctime.GetMinute(), ctime.GetSecond());
获取当前时间:
第一种:
char capTime[64];
SYSTEMTIME st;
GetLocalTime(&st);
sprintf(capTime, "%04d-%02d-%02d %02d:%02d:%02d", st.wYear, st.wMonth, st.wDay,
st.wHour, st.wHour, st.wSecond);
第二种:
//COleDateTime oleDT=COleDateTime::GetCurrentTime(); 调整系统时间后获取时间不准
COleDateTime nowTime(time(NULL)); //获取当前时间
sprintf(capTime, "%04d-%02d-%02d %02d:%02d:%02d", nowTime.GetYear(), nowTime.GetMonth(), nowTime.GetDay(),
nowTime.GetHour(), nowTime.GetMinute(), nowTime.GetSecond());
CString str = nowTime.Format("%Y-%m-%d %H:%M:%S");
建议使用第二种。实际开发中,使用第一种时,如果刚刚重新设置了电脑系统时间,第一种方法取得的数据有时候显示不正确,
用第二种方法则一直正确。
COleDateTime的头文件:#include <ATLComTime.h>
获取时间差:
COleDateTime timeStart, timeEnd;
//timeStart = COleDateTime::GetCurrentTime();调整系统时间后获取时间不准
COleDateTime timeStart(time(NULL)); //获取当前时间
// ... perform time-consuming task
timeEnd = COleDateTime::GetCurrentTime();
COleDateTimeSpan spanElapsed = timeEnd - timeStart;
//int secs = spanElapsed.GetSeconds();
int totalsecs = spanElapsed.GetTotalSeconds();
timeStart也可以用SetDateTime()设置为其他时间:
timeStart.SetDateTime(2018,1,22,17,0,0);
利用时间差比较大小:
先转换成COleDateTime类,用这两个类对象减得到COleTimeSpan类型,
COleDateTime t1(1999, 3, 20, 21, 15, 0);
COleDateTime t2(1999, 3, 20, 22, 15, 0);
COleDateTimeSpan ts = t1 - t2;
double
val = ts.GetTotalSeconds();
if
(val > 0)
{
AfxMessageBox(_T(
"t1 more than t2!!"
));
}
else
if
(val < 0)
{
AfxMessageBox(_T(
"t1 less than t2!!"
));
}
else
{
AfxMessageBox(_T(
"t1 equal t2!!!"
));
}
c++ 时间类型详解(time_t和tm) - CSDN博客 http://blog.csdn.net/luoweifu/article/details/20288549
1,标准时间准换成时间戳
int standard_to_stamp(char *str_time)
{
struct tm stm;
int iY, iM, iD, iH, iMin, iS;
memset(&stm,0,sizeof(stm));
iY = atoi(str_time);
iM = atoi(str_time+5);
iD = atoi(str_time+8);
iH = atoi(str_time+11);
iMin = atoi(str_time+14);
iS = atoi(str_time+17);
stm.tm_year=iY-1900;
stm.tm_mon=iM-1;
stm.tm_mday=iD;
stm.tm_hour=iH;
stm.tm_min=iMin;
stm.tm_sec=iS;
/*printf("%d-%0d-%0d %0d:%0d:%0d\n", iY, iM, iD, iH, iMin, iS);*/ //标准时间格式例如:2016:08:02 12:12:30
return (int)mktime(&stm);
}
2,时间戳转换成标准时间
typedef struct times
{
int Year;
int Mon;
int Day;
int Hour;
int Min;
int Second;
}Times;
Times stamp_to_standard(int stampTime)
{
time_t tick = (time_t)stampTime;
struct tm tm;
char s[100];
Times standard;
//tick = time(NULL);
tm = *localtime(&tick);
strftime(s, sizeof(s), "%Y-%m-%d %H:%M:%S", &tm);
printf("%d: %s\n", (int)tick, s);
standard.Year = atoi(s);
standard.Mon = atoi(s+5);
standard.Day = atoi(s+8);
standard.Hour = atoi(s+11);
standard.Min = atoi(s+14);
standard.Second = atoi(s+17);
return standard;
}
3,如何获取系统标准时间
time_t rawtime ;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
其中:
int Year = timeinfo->tm_year+1900;
int Mon = timeinfo->tm_mon+1;
其余日,时,分,秒都不变。
获取当前时间,精确到ms:
std::string CSocketLogHelper::NowTime()
{
stringstream sStream;
SYSTEMTIME sys;
GetLocalTime(&sys);
sStream << sys.wYear << "-" << sys.wMonth << "-" << sys.wDay << " " << sys.wHour << ":" << sys.wMinute << ":" << sys.wSecond << "." << sys.wMilliseconds;
std::string sNow = sStream.str();
return sNow;
}
时间格式转换:
SYSTEMTIME stTimeF;
GetLocalTime(&stTimeF);
COleDateTime dTimeF(stTimeF);