LINUX系统编程之日历时间

来源:互联网 发布:常用的特征提取算法 编辑:程序博客网 时间:2024/06/15 08:42

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;

}

转换关系如下图:

示例代码如下:

[cpp] view plaincopyprint?
struct utsname aname;
int a=uname(&aname);
  1. cout << a;
  2. cout << endl;
  3. cout << aname.sysname<< endl;
  4. cout << aname.machine<< endl;
  5. cout << aname.release<< endl;
  6. cout << aname.version<<endl;
  7. cout << aname.nodename<< endl;
  8. time_t time1;
  9. time_t time2;
  10. time2=time(&time1);
  11. cout << time1 << endl;
  12. cout << time2 << endl;
  13. struct tm *tm1;
  14. tm1=localtime(&time1);
  15. cout << tm1->tm_sec<<endl;
  16. cout << tm1->tm_min<<endl;
  17. cout << tm1->tm_hour<<endl;
  18. cout << tm1->tm_mday<<endl;
  19. cout << tm1->tm_mon<<endl;
  20. cout << tm1->tm_year<<endl;
  21. cout << tm1->tm_wday<<endl;
  22. cout << tm1->tm_yday<<endl;
  23. char *css;
  24. css=asctime(tm1);
  25. cout << css;
  26. size_t size=strftime(css,100,"%Y-%m-%d %H-%M-%S %w",tm1);
  27. cout << size << ":"<<css;

原创粉丝点击