//mian函數 // 15.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "MyTime.h" #include <iostream> using namespace std; int main(int argc, char* argv[]) { CMyTime t1,t2; t2=t1.GetCurrentTime(); int day=t2.GetMonth(); cout<<"today is:"<<day<<endl; return 0; }
//定義的類頭文件// MyTime.h: interface for the CMyTime class.////////////////////////////////////////////////////////////////////////#if !defined(AFX_MYTIME_H__D7793E59_8A7A_4BB7_89E3_DC3E6CC7CA8E__INCLUDED_)#define AFX_MYTIME_H__D7793E59_8A7A_4BB7_89E3_DC3E6CC7CA8E__INCLUDED_#if _MSC_VER > 1000#pragma once#endif // _MSC_VER > 1000//#include "stdafx.h"#include "time.h"#include "assert.h"#include "Windows.h"class CMyTime {public:BOOL operator==(CMyTime t1)const; int GetDayOfWeek()const;int GetSec()const;int GetMin()const;int GetHour()const;int GetDay()const;int GetMonth()const;int GetYear()const;time_t GetTime()const;static CMyTime GetCurrentTime();CMyTime();CMyTime(time_t time);CMyTime(int nYear, int nMonth, int nDay, int nHour, int nMin, int nSec,int nDST=-1);virtual ~CMyTime();private:time_t m_time;};#endif // !defined(AFX_MYTIME_H__D7793E59_8A7A_4BB7_89E3_DC3E6CC7CA8E__INCLUDED_)
//類的實現
// MyTime.cpp: implementation of the CMyTime class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "MyTime.h"
#include "time.h"
#include "assert.h"
#include "windef.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CMyTime::CMyTime()
{
}
CMyTime::~CMyTime()
{
}
CMyTime::CMyTime(time_t t)
{
m_time=time(&t);
}
CMyTime::CMyTime(int nYear, int nMonth, int nDay, int nHour, int nMin, int nSec,
int nDST)
{
struct tm atm;
atm.tm_sec = nSec;
atm.tm_min = nMin;
atm.tm_hour = nHour;
assert(nDay >= 1 && nDay <= 31);
atm.tm_mday = nDay;
assert(nMonth >= 1 && nMonth <= 12);
atm.tm_mon = nMonth - 1; // tm_mon is 0 based
assert(nYear >= 1900);
atm.tm_year = nYear - 1900; // tm_year is 1900 based
atm.tm_isdst = nDST;
m_time = mktime(&atm);
assert(m_time != -1); // indicates an illegal input time
}
CMyTime CMyTime::GetCurrentTime()
{
CMyTime t;
t.m_time=time(NULL);
return t;
}
time_t CMyTime::GetTime()const
{
return time(NULL);
}
int CMyTime::GetYear()const
{
tm *p=localtime(&m_time);
return p->tm_year+1900;
}
int CMyTime::GetMonth()const
{
tm *p=localtime(&m_time);
return p->tm_mon+1;
}
int CMyTime::GetDay()const
{
tm *p=localtime(&m_time);
return p->tm_mday;
}
int CMyTime::GetHour()const
{
tm *p=localtime(&m_time);
return p->tm_hour;
}
int CMyTime::GetMin()const
{
tm *p=localtime(&m_time);
return p->tm_min;
}
int CMyTime::GetSec()const
{
tm *p=localtime(&m_time);
return p->tm_sec;
}
int CMyTime::GetDayOfWeek()const
{
tm *p=localtime(&m_time);
return p->tm_wday;
}
BOOL CMyTime::operator==(CMyTime t1)const
{
tm *p1=localtime(&this->m_time);
tm *p2=localtime(&t1.m_time);
if((p1->tm_year==p2->tm_year)&&(p1->tm_mon==p2->tm_mon))
return TRUE;
else
return FALSE;
}