64,682
社区成员
发帖
与我相关
我的任务
分享
#include <time.h>
#include <iostream>
using namespace std;
int main()
{
tm pt = {0};
pt.tm_year = 101;//2001年
pt.tm_mon = 0;//1月
pt.tm_mday = 1;//1日
time_t start = mktime(&pt);
cout<<"please enter a day between 2001/1/1 and 2009/1/1, format: 'XXXX XX XX'"<<endl;
int year=0, month=0, day=0;
cin>>year>>month>>day;
pt.tm_year = year-1900;
pt.tm_mon = month-1;
pt.tm_mday = day;
time_t end = mktime(&pt);
cout<<"seconds between 2001/1/1 and "<<pt.tm_year+1900<<"/"<<pt.tm_mon+1<<"/"<<pt.tm_mday<<" is: "<<end-start<<endl;
return 0;
}
time.h
#ifndef __LX_TIME
#define __LX_TIME
#include <dos.h>
#include <iostream>
using namespace std;
typedef struct time sTime;
class myTime
{
private:
sTime _t1,_t2;
public:
unsigned int _hour,_min,_sec,_hund;
myTime(sTime t);
myTime();
~myTime();
myTime GetTime();
myTime Start();
myTime End();
};
#endif
time.cpp
#include "time.h"
myTime operator -(myTime &T1,myTime &T2)
{
int hr,m,s,hd;
myTime ret;
hr=T1._hour;
hd=T1._hund;
s=T1._sec ;
m=T1._min ;
hd-=T2._hund;
if(hd<0)
{
hd+=100;
s-=1;
}
s-=T2._sec;
if(s<0)
{
s+=60;
m-=1;
}
m-=T2._min;
if(m<0)
{
m+=60;
hr-=1;
}
hr-=T2._hour;
ret._hund=hd;
ret._min=m;
ret._sec=s;
ret._hour=hr;
return ret;
}
myTime::~myTime()
{
// cout<<"Destructor"<<endl;
}
myTime::myTime()
{
// cout<<"Constructor without param"<<endl;
}
myTime::myTime(sTime t)
{
_hour=t.ti_hour;
_min=t.ti_min;
_sec=t.ti_sec;
_hund=t.ti_hund;
// cout<<"Constructor with param"<<endl;
}
myTime myTime::Start()
{
// cout<<"Begin Start"<<endl;
gettime(&_t1);
myTime x(_t1);
// cout<<"Finish Start"<<endl;
return x;
}
myTime myTime::End()
{
// cout<<"Begin End"<<endl;
gettime(&_t2);
myTime tmp1(_t1),tmp2(_t2);
// cout<<"Finish End"<<endl;
return (tmp2-tmp1);
}
myTime myTime::GetTime()
{
sTime t;
gettime(&t);
_hour=t.ti_hour;
_min=t.ti_min;
_sec=t.ti_sec;
_hund=t.ti_hund;
return *this;
}
ostream & operator << (ostream &os ,myTime const T)
{
os<<(int)T._hour<<":"<<(int)T._min<<":"<<(int)T._sec<<"."<<(int)T._hund;
return os;
}