在进行获取系统时间时,相关函数 time,ctime,gmtime,localtime。。。,其中asctime是将时间和日期以字符串的形式进行输出,
eg:

#include<time.h>
定义函数
char * asctime(const struct tm * timeptr);
函数说明
asctime()将参数timeptr所指的tm结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果以字符串形态返回。此函数已经由时区转换成当地时间,字符串格式为:“Wed Jun 30 21:49:08 1993\n”

#include <time.h>
#include <stdio.h>
int main() 
{ 
	time_t timep; 
	time(&timep); 
	printf("%s\n",asctime(gmtime(&timep))); 
}
ctime(将时间和日期以字符串格式表示)

表头文件
#include<time.h>
定义函数
char *ctime(const time_t *timep);
函数说明
ctime()将参数timep所指的time_t结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果以字符串形态返回。此函数已经由时区转换成当地时间

#include <time.h>
#include <stdio.h>
int main() 
{ 
	time_t timep; 
	time(&timep); 
	printf("%s\n",ctime(&timep)); 
}
gettimeofday(取得目前的时间)
表头文件
#include <sys/time.h> 
#include <unistd.h>
定义函数
int gettimeofday ( struct timeval * tv , struct timezone * tz )
函数说明
gettimeofday()会把目前的时间有tv所指的结构返回,当地时区的信息则放到tz所指的结构中。 
timeval结构定义为: 

struct timeval{ 
    long tv_sec; /*秒*/ 
    long tv_usec; /*微秒*/ 
};
#include <sys/time.h>
#include <iostream>
using namespace std;

int main() 
{
    struct timeval tv;
    gettimeofday(&tv, nullptr);

    cout << "seconds since 00:00:00, 1 Jan 1970 UTC: " << tv.tv_sec << endl;
    cout << "Additional microsecondsss: " << tv.tv_usec << endl;
}

在Linux环境下

  1. 精确到毫秒
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <unistd.h>
 
int main(int argc, char *argv[])
{
    struct timeval time;
 
    /* 获取时间,理论到us */
    gettimeofday(&time, NULL);
    printf("s: %ld, ms: %ld\n", time.tv_sec, (time.tv_sec*1000 + time.tv_usec/1000));
 
    sleep(3);  //延时
 
    /* 重新获取 */
    gettimeofday(&time, NULL);
    printf("s: %ld, ms: %ld\n", time.tv_sec, (time.tv_sec*1000 + time.tv_usec/1000));
 
 
    exit(0);
}

精确到秒:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
 
 
int main(int argc, char *argv[])
{
 
    time_t t;
    struct tm *tmp;
    char buf2[64];
 
    /* 获取时间 */
    time(&t);
    tmp = localtime(&t);
 
    /* 转化时间 */
    if (strftime(buf2, 64, "time and data: %r, %a %b %d, %Y", tmp) == 0) {
        printf("buffer length 64 is too small\n");
    } else {
        printf("%s\n", buf2);
    }
 
    exit(0);
}```

在window环境下:
1、精确到毫秒

#include “stdafx.h”
#include <windows.h>
#include

using namespace std;

int main(int argc, _TCHAR* argv[])
{
DWORD time_start, time_end;
/* 获取开始时间 */
time_start = GetTickCount(); //从操作系统启动经过的毫秒数
Sleep(3000);
time_end = GetTickCount();
cout << "Time = " << (time_end - time_start) << "ms\n ";

return 0;

}

#include <time.h>

clock_t start,ends;
start=clock();
Sleep(50);
ends=clock();
cout<<ends-start<<endl;

time_t 获得时间只能精确到秒,clock_t 获得时间能够精确到毫秒
精确到秒:

unsigned long long Utils::GetCurrentTimeMsec()
{
#ifdef _WIN32
struct timeval tv;
time_t clock;
struct tm tm;
SYSTEMTIME wtm;

GetLocalTime(&wtm);
tm.tm_year = wtm.wYear - 1900;
tm.tm_mon = wtm.wMonth - 1;
tm.tm_mday = wtm.wDay;
tm.tm_hour = wtm.wHour;
tm.tm_min = wtm.wMinute;
tm.tm_sec = wtm.wSecond;
tm.tm_isdst = -1;
clock = mktime(&tm);
tv.tv_sec = clock;
tv.tv_usec = wtm.wMilliseconds * 1000;
return ((unsigned long long)tv.tv_sec * 1000 + (unsigned long long)tv.tv_usec / 1000);
#else
struct timeval tv;
gettimeofday(&tv, NULL);
return ((unsigned long long)tv.tv_sec * 1000 + (unsigned long long)tv.tv_usec / 1000);
#endif
}

timezone 结构定义为:
struct timezone{
int tz_minuteswest; /和Greenwich 时间差了多少分钟/
int tz_dsttime; /日光节约时间的状态/
};

上述两个结构都定义在/usr/include/sys/time.h。
tz_dsttime 所代表的状态如下 :

DST_NONE /不使用/
DST_USA /美国/
DST_AUST /澳洲/
DST_WET /西欧/
DST_MET /中欧/
DST_EET /东欧/
DST_CAN /加拿大/
DST_GB /大不列颠/
DST_RUM /罗马尼亚/
DST_TUR /土耳其/
DST_AUSTALT /澳洲(1986年以后)/

返回值
成功则返回0,失败返回-1,错误代码存于errno。附加说明EFAULT指针tv和tz所指的内存空间超出存取权限。
#include<sys/time.h>
#include<unistd.h>
#include<stdio.h>
int main(){
struct timeval tv;
struct timezone tz;
gettimeofday (&tv , &tz);
printf(“tv_sec; %ld\n”, tv.tv_sec) ;
printf(“tv_usec; %ld\n”,tv.tv_usec);
printf(“tz_minuteswest; %d\n”, tz.tz_minuteswest);
printf(“tz_dsttime, %d\n”,tz.tz_dsttime);
}

结果:

tv_sec; 1517112882
tv_usec; 875374
tz_minuteswest; -480
tz_dsttime, 0

gmtime
gmtime(取得目前时间和日期)
表头文件
#include<time.h>
定义函数
struct tm*gmtime(const time_t*timep);
函数说明
gmtime()将参数timep 所指的time_t 结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果由结构tm返回。 
结构tm的定义为 :
struct tm
 {
 int tm_sec;
 int tm_min;
 int tm_hour;
 int tm_mday;
 int tm_mon;
 int tm_year;
 int tm_wday;
 int tm_yday;
 int tm_isdst;
 };
int tm_sec 代表目前秒数,正常范围为0-59,但允许至61秒 
int tm_min 代表目前分数,范围0-59 
int tm_hour 从午夜算起的时数,范围为0-23 
int tm_mday 目前月份的日数,范围01-31 
int tm_mon 代表目前月份,从一月算起,范围从0-11 
int tm_year 从1900 年算起至今的年数 
int tm_wday 一星期的日数,从星期一算起,范围为0-6 
int tm_yday 从今年1月1日算起至今的天数,范围为0-365 
int tm_isdst 日光节约时间的旗标 
此函数返回的时间日期未经时区转换,而是UTC时间。
返回值
返回结构tm代表目前UTC 时间
#include<time.h>
 #include<stdio.h>
 int main(){
 char *wday[] = {“Sun”,“Mon”,“Tue”,“Wed”,“Thu”,“Fri”,“Sat”};
 time_t timep;
 struct tm *p;
 time(&timep);
 p = gmtime(&timep);
 printf(“%d%d%d”,(1900+p->tm_year), (1+p->tm_mon),p->tm_mday);
 printf(“%s%d;%d;%d\n”, wday[p->tm_wday], p->tm_hour, p->tm_min, p->tm_sec);
 }
localtime
localtime(取得当地目前时间和日期)
表头文件
#include<time.h>
定义函数
struct tm *localtime(const time_t * timep);
函数说明
localtime()将参数timep所指的time_t结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果由结构tm返回。结构tm的定义请参考gmtime()。此函数返回的时间日期已经转换成当地时区。
返回值
返回结构tm代表目前的当地时间。
范例


#include<time.h>
 #include<stdio.h>
 int main(){
 char *wday[]={“Sun”,“Mon”,“Tue”,“Wed”,“Thu”,“Fri”,“Sat”};
 time_t timep;
 struct tm *p;
 time(&timep);
 p=localtime(&timep); /取得当地时间/
 printf (“%d%d%d “, (1900+p->tm_year),( 1+p->tm_mon), p->tm_mday);
 printf(”%s%d:%d:%d\n”, wday[p->tm_wday],p->tm_hour, p->tm_min, p->tm_sec);
 }