1、time(time_t&)
#include <time.h>
time_t t
time (&t) //获取1970年以来的秒数,此处是utc时间
time_t实际上为32位或者64位整数
2、localtime(time_t) 和 gmtime(time_t)
根据上面获得的秒数t
struct tm * lt = localtime (&t) //转换为本时区时间信息
struct tm * gt=gmtime (&t) //转换为utc时间信息
其中:
struct tm {
int tm_sec //秒
int tm_min //分
int tm_hour //小时
int tm_mday //日
int tm_mon //月
int tm_year //年
int tm_wday //星期,其中0代表星期天,1代表星期一
int tm_yday //从每年的1月1日开始的天数 ,其中0代表1月1日
int tm_isdst //夏令时标识符
}
3、mktime(struct tm*)
t=mktime(lt) //将时间结构lt转为1970年以来的秒数,也可以自己手工构建tm结构
此处lt为本时区时间信息
4、GetLocalTime(SYSTEMTIME&) 获取本地时间,有毫秒
SetLocalTime(SYSTEMTIME&) 设置本地时间
SYSTEMTIME st
GetLocalTime(&st)
其中:
STRUCT SYSTEMTIME
{
WORD wYear 年
WORD wMonth 月
WORD wDayOfWeek 星期,0=星期日,1=星期一...
WORD wDay 日
WORD wHour 时
WORD wMinute 分
WORD wSecond 秒
WORD wMilliseconds 毫秒
}
GetSystemTime(SYSTEMTIME&)和SetSystemTime(SYSTEMTIME&) 操作的是UTC时间,其他相同。
5、void ftime(struct timeb *)
#include <sys/timeb.h>
获取时间,有毫秒
struct timeb tp
ftime(&tp)
其中:
struct timeb{
time_t time /* 为1970-01-01至今的秒数*/
unsigned short millitm /* 毫秒 */
short timezone /* 时区差值,单位为分钟 */
short dstflag /* 夏令时标识 */
}
6、获取文件的时间信息
#include<sys/stat.h>
int stat(const char * file_name, struct stat *buf)
具体信息请自行搜索
7、clock()函数
返回从程序启动到调用时刻的时间间隔。可用于测量两个事件之前的时间:
clock_t start, finish
double duration
start = clock()
//...事件
finish = clock()
duration = (double)(finish - start) / CLOCKS_PER_SEC
printf( "%f secondsn", duration )
8、gettimeofday()
int gettimeofday(struct timeval*tv, struct timezone *tz)
struct timeval{
long int tv_sec // 1970年以来的秒数
long int tv_usec // 微秒数
}
可以计算代码执行时间:
struct timeval tv_begin, tv_end
gettimeofday(&tv_begin, NULL)
//...
gettimeofday(&tv_end, NULL)
9、GetTickCount()
它返回从操作系统启动到当前所经过的毫秒数,返回值以32位的双字类型DWORD存储,因此可以存储的最大值是(2^32-1) ms约为49.71天,因此若系统运行时间超过49.71天时,这个数就会归0。
10、GetSystemTimeAsFileTime(FILETIME *)
struct _FILETIME {
DWORDdwLowDateTime
DWORDdwHighDateTime
} FILETIME
相关函数:
SYSTEMTIME st
FILETIME ft
SystemTimeToFileTime(&st, &ft)
FileTimeToSystemTime(&ft, &st)
11、TimeGetTime
和GetTickCount差不多。GetTickCount精度15毫秒,TimeGetTime精度为1ms