吕鑫书中自己定义的CMyTime

来源:互联网 发布:一木成林软件 编辑:程序博客网 时间:2024/06/03 13:33
//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 basedassert(nYear >= 1900);atm.tm_year = nYear - 1900;     // tm_year is 1900 basedatm.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;elsereturn FALSE;}

原创粉丝点击