TimeStamp类

来源:互联网 发布:淘宝女装数据包 编辑:程序博客网 时间:2024/06/05 15:49
#include <iostream>#include <ctime>#include <string>using namespace std;class Timestamp{public:void set(long s = 0){if(s <= 0)stamp = time(0);elsestamp = s;}time_t get()const{return stamp;}string getstring()const{return extract( 0, 24);}private:string extract( int start, int end ) const{string timestring = ctime( &stamp );return timestring.substr( start, end );}time_t stamp;};

 

测试程序

#include "Timestamp.h"using namespace std;int main(){time_t now = time(0);Timestamp ts;ts.set(now);cout << ts.getstring() << endl;return 0;}


 

利用库函数瘦包装了一些函数 实现了一个类 可以返回目前的时间(书上的练习 初学c++)

 利用了 time 和 ctime 2个库函数

0 0
原创粉丝点击