clock作为Time类的第一个基类,其构成很简单,主要是封装时间的类,用来对程序运行时间进行计时,并处理与时间有关的数据,如返回系统日期,系统当前时间,程序运行时间等。
该类具有三个私有成员变量:
time_t(注1) startTime_; //构造时的时间
mutable time_t lastTime_; //上一次计时的时间
mutable time_t newTime_; //最新计时的时间
该类就是通过上面三个值来实现对程序进行计时的,主要应用体现在下面两个成员函数:
time_t elapsedClockTime() const; //取当前时间并赋值给newTime_成员,同时返回newTime_-startTime_
time_t clockTimeIncrement() const; //计算当前时间距上次取时间的时间间隔,返回newtime_-lastTime_
该类还提供了以下时间处理函数:
time_t getTime(); //通过调用time(0)(c++函数)返回当前系统时间
const struct tm rawDate(); //通过调用localtime(&getTime())(c++函数)返回包含时间相关信息的tm结构
Foam::string date(); //返回一个如格式month day year的字符串,如Sep 02 2008
Foam::string clockTime(); //返回一个如格式hh:mm:ss的字符串,如22:05:12
其中date用到的月份名取自该类的一个静态字符串数组:
char* monthnames[]={"Jan","Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",};
由于Time公有继承该类,因而其便具有clock类的上述成员函数的功能。
注1:在ctime头文件中定义,其实为long类型
评论