LINUX中时间有两种:
1)日历时间
2)进程时间
日历时间顾名思义即用来获取日历;
主要涉及到的函数有:
time(time_t*);
stime(time_t*);
tm* gmtime(time_t*);
tm* localtime(time_t*);
char *strftime(tm*);
char *asctime(tm*);
time_t* mktime(tm*);
数据结构如下:
time_t
struct tm{
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_wday;
int tm_yday;
int tm_year;
int tm_isdst;
}
转换关系如下图:

示例代码如下:
- struct utsname aname;
- int a=uname(&aname);
- cout << a;
- cout << endl;
- cout << aname.sysname<< endl;
- cout << aname.machine<< endl;
- cout << aname.release<< endl;
- cout << aname.version<<endl;
- cout << aname.nodename<< endl;
-
-
- time_t time1;
- time_t time2;
- time2=time(&time1);
- cout << time1 << endl;
- cout << time2 << endl;
-
- struct tm *tm1;
-
- tm1=localtime(&time1);
-
- cout << tm1->tm_sec<<endl;
- cout << tm1->tm_min<<endl;
- cout << tm1->tm_hour<<endl;
- cout << tm1->tm_mday<<endl;
- cout << tm1->tm_mon<<endl;
- cout << tm1->tm_year<<endl;
- cout << tm1->tm_wday<<endl;
- cout << tm1->tm_yday<<endl;
-
- char *css;
- css=asctime(tm1);
-
- cout << css;
-
- size_t size=strftime(css,100,"%Y-%m-%d %H-%M-%S %w",tm1);
-
- cout << size << ":"<<css;
|