《c++时间函数讲解》由会员分享,可在线阅读,更多相关《c++时间函数讲解(6页珍藏版)》请在金锄头文库上搜索。
1、C-C+时间函数的使用 一、获取日历时间time_t 是定义在 time.h 中的一个类型,表示一个日历时间,也就是从 1970 年 1 月 1日 0 时 0 分 0 秒到此时的秒数,原型是:typedef long time_t; /* time value */可以看出 time_t 其实是一个长整型,由于长整型能表示的数值有限,因此它能表示的最迟时间是 2038 年 1 月 18 日 19 时 14 分 07 秒。函数 time 可以获取当前日历时间时间,time 的定义:time_t time(time_t *)#include #include using namespace std
2、;int main(void)time_t nowtime;nowtime = time(NULL); /获取当前时间cout #include using namespace std;void dsptime(const struct tm *); /输出时间。int main(void)time_t nowtime;nowtime = time(NULL); /获取日历时间cout tm_year+1900 tm_mon+1 tm_mday tm_hour tm_min tm_sec tm_wday tm_yday #include using namespace std;int main
3、(void)time_t nowtime;nowtime = time(NULL); /获取日历时间cout #include using namespace std;int main(void)time_t start, ends;clock_t cstart,cends;start=time(NULL);cstart=clock();system(pause);ends=time(NULL);cends=clock();cout #include using namespace std;int main(void)time_t nowtime;nowtime = time(NULL); /获取日历时间cout nowtime endl; /输出 nowtimestruct tm *local;local=localtime( /获取当前系统时间char buf80;strftime(buf,80,格式化输出:%Y-%m-%d %H:%M:%S,local);cout buf endl;return 0;输出结果:1268575163格式化输出:2010-03-14 21:59:23