文章目录
1、时间类型
真实时间:主要针对需要周期性操作或定期从外部输入设备进行度量的程序;
进程时间:一个进程所使用的CPU时间总量,适用于对程序、算法性能的检查或优化;
2、日历时间
#include <sys/time.h>
int gettimeofday(struct timeval *tv, struct timezone *tz);
/**
@func: 获取时间,两个参数为传输参数;
struct timeval {
time_t tv_sec; // 秒
suseconds_t tv_usec; // 微妙
};
struct timezone {
int tz_minuteswest;
int tz_dsttime;
};
*/
#include <time.h>
time_t time(time_t *timep);
/**
@func: 返回以自Epoch(1970-01-01 00:00:00 +0000 (UTC))以来的秒数返回时间;
*/
3、时间转换函数
3.1 将time_t转换为可打印格式
#include <time.h>
char *ctime(const time_t *timep);
/**
@func: 返回一个长度26字节的字符串,自动对本地时区和DST设置加以考虑;
return: 返回字符串由静态分配,下一次对ctime的调用会将其覆盖;
*/
3.2 time_t和分解时间之间的转换
#include <time.h>
struct tm *gmtime(const time_t *timep);
/**
@func: 把日历时间转换为一个对应于UTC的分解时间;
*/
struct tm *localtime(const time_t *timep);
/**
@func: 将日历时间转化为本地时间;
*/
struct tm {
int tm_sec; /* seconds */
int tm_min; /* minutes */
int tm_hour; /* hours */
int tm_mday; /* day of the month */
int tm_mon; /* month */
int tm_year; /* year */
int tm_wday; /* day of the week */
int tm_yday; /* day in the year */
int tm_isdst; /* daylight saving time */
};
#include <time.h>
time_t mktime(struct tm *timeptr);
/**
@func: 将本地时区的分解时间翻译为time_t值,并返回;
*/
3.3 分解时间和打印格式之间的转换
从分解时间转换为打印格式
#include <time.h>
char *asctime(const struct tm *timeptr);
/**
@func: 分解时间;
return: 返回静态分配的字符串(时间);
*/
3.4 获取和转换日历时间
#include <locale.h>
#include <time.h>
#include <iostream>
#include <sys/time.h>
#include "../Jxiepc/tlpi_hdr.h"
#define SECONDS_IN_TROPICAL_YEAR (365.24219 * 24 * 60 * 60)
using namespace std;
void test_time() {
/* 获取1970年到现在的时间 */
time_t t;
t = time(NULL);
cout << "Seconds since the Epoch (1 Jan 1970): " << (long)t << endl;
cout << "(about " << (int)t/SECONDS_IN_TROPICAL_YEAR << " years)" << endl;
/* 获取1970到现在秒或微秒 */
struct timeval tv;
if(gettimeofday(&tv, NULL) == -1)
errExit("gettimeofday");
cout << "gettimeofday return " << (long)tv.tv_sec
<< ", " << (long)tv.tv_usec << " microsecs" << endl;
struct tm *gmp, *locp;
struct tm gm, loc;
/** 获取时间tm */
gmp = gmtime(&t);
if(gmp == NULL)
errExit("gmtime");
gm = *gmp;
cout << "【gmtime】: "
<< " year: " << gm.tm_year
<< " mon: " << gm.tm_mon
<< " mday: " << gm.tm_mday
<< " hour: " << gm.tm_hour
<< " min: " << gm.tm_min
<< " sec: " << gm.tm_sec
<< endl;
cout << "\twday: " << gm.tm_wday
<< " yday: " << gm.tm_yday
<< " isdst: " << gm.tm_isdst << endl;
locp = localtime(&t);
if(locp == NULL)
errExit("localtime");
loc = *locp;
cout << "【localtime】: "
<< " year: " << loc.tm_year
<< " mon: " << loc.tm_mon
<< " mday: " << loc.tm_mday
<< " hour: " << loc.tm_hour
<< " min: " << loc.tm_min
<< " sec: " << loc.tm_sec
<< endl;
cout << "\twday: " << loc.tm_wday
<< " yday: " << loc.tm_yday
<< " isdst: " << loc.tm_isdst << endl;
cout << "asctime() formats the gmtime() value as: " << asctime(&gm);
cout << "ctime() formats the time() value as: "<< ctime(&t);
cout << "mktime() of gmtime() value: " << mktime(&gm) << endl;
cout << "mktime() of localtime() value : " << mktime(&loc) << endl;
}
int main() {
test_time();
return 0;
}
时间精确控制
#include <time.h>
size_t strftime(char *outstr, size_t maxsize, const char *format
, const struct tm *timeptr);
/**
@func: 返回按format格式的字符串;
*/
将打印格式时间转换为分解时间
#incluide <time.h>
char *strptime(const char *str, const char *format, struct tm *timeptr);
/**
@func: 将str转换为存储在tm结构中,使用format指定的格式;
*/
4、时区
#include <time.h>
#include <iostream>
#include <locale.h>
#include "../Jxiepc/tlpi_hdr.h"
using namespace std;
#define BUF_SIZE 200
void test_stime() {
time_t t;
struct tm *loc;
char buf[BUF_SIZE];
if(setlocale(LC_ALL, "") == NULL)
errExit("setlocale");
t = time(NULL);
cout << "ctime() of time() value is: " << ctime(&t) << endl;
loc = localtime(&t);
if(loc == NULL)
errExit("localtime");
cout << "asctime() of local time is: " << asctime(loc) << endl;
if(strftime(buf, BUF_SIZE, "%A, %d %B %Y , %H:%M:%S %Z", loc) == 0)
fatal("strftime returned 0");
cout << "strftime() of local time is: " << buf << endl;
}
int main() {
test_stime();
return 0;
}
5、地区
设置地区
#include <locale.h>
char *setlocale(int category, const char *locale);
/**
@func: 设置或查询程序当前地区;
category为LC_ALL,可设置为任意地区;
*/
6、进程时间
即进程创建后使用的CPU时间数量;内核将CPU分成两部分:
- 【用户CPU时间】是再用户模式下执行所花费的时间数量;
- 【系统CPU时间】是再内核模式中执行所花费的时间数量;
#include <sys/time.h>
clock_t times(struct tms *buf);
/**
@func: 检索进程时间信息,将结果通过buf指向的结构体返回;
*/
struct tms {
clock_t tms_utime; // 用户CPU使用时间
clock_t tms_stime; // 系统CPU使用时间
clock_t tms_cutime; // 所有子用户CPU时间
clock_t tms_cstime; // 所有子程序的系统
};
clock_t clock(void);
/**
@func: 获取进程使用总CPU时间;
以CLOCKS_PER_SEC为计量单位,需要将其除去;
*/
案例
#include <sys/times.h>
#include <time.h>
#include <iostream>
#include "../Jxiepc/tlpi_hdr.h"
using namespace std;
static void displyProcessTimes(const char *msg) {
struct tms t;
clock_t clockTime;
static long clockTicks = 0;
if(msg != NULL)
cout << msg << endl;
if(clockTicks == 0){
clockTicks = sysconf(_SC_CLK_TCK);
if(clockTicks == -1)
errExit("sysconf");
}
clockTime = clock();
if(clockTime == -1)
errExit("clock");
cout << "clock() return: " << (long)clockTime
<< "clocks-per-sec (" << (double)clockTime/CLOCKS_PER_SEC << ")" << endl;
if(times(&t) == -1)
errExit("times");
cout << " times() yields: user CPU=" << (double)t.tms_utime / clockTicks
<< "; system CPU: " << ((double)t.tms_stime / clockTicks);
}
int main(int argc, char* argv[]) {
int numCalls, j;
cout << "CLOCKS_PER_SEC=" << (long)CLOCKS_PER_SEC <<
"sysconf(_SC_CLK_TCK) =" << sysconf(_SC_CLK_TCK);
displyProcessTimes("At program start: \n");
numCalls = 100000;
for(j = 0 ; j < numCalls; ++j) {
(void)getppid();
}
displyProcessTimes("After gtppid() loop:\n");
return 0;
}