⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 datetime.cpp

📁 多媒体电话记录程序
💻 CPP
字号:
/////////////////////////////////////////////////////////////////////////////
//  Name: datetime.cpp
//  Copyright: wellgain
//  Author: bet
//  Date: 2003-10-8
//  Description:  the implement for classes CTime and CDateTime
/////////////////////////////////////////////////////////////////////////////


#include <stdio.h>
#include <assert.h>
#include "datetime.h"
/////////////////////////////////////////////////////////////////////////////
//------------------------------ class CTime --------------------------------

CTime CTime::operator-(CTime t)
{
	long secs = to_long() - t.to_long();
	secs = secs>=0 ? secs : -secs;	

	return secs;
}


CTime& CTime::operator+=(CTime t)
{
	*this = *this + t;
	return *this;
}


CTime& CTime::operator-=(CTime t)
{
	*this = *this - t;
	return *this;
}


CTime::CTime()
{
	time_t tt;
	tt = time(NULL);
	struct tm* loctime;
	loctime = localtime(&tt);
	hour = loctime->tm_hour;
	min  = loctime->tm_min;
	sec  = loctime->tm_sec;
}


CTime::~CTime()
{

}

CTime::CTime(int h, int m, int s)
{
	assert(h >= 0);		//允许大于23
	hour = h;
	assert(m >= 0 && m <= 59);
	min  = m;
	assert(s >= 0 && s <= 59);
	sec  = s;
}


CTime::CTime(long secs)
{
	long ss;
	ss = secs>=0 ? secs : -secs;
	hour = (int)(ss/3600);
	ss = ss%3600;
	min  = (int)(ss/60);
	sec  = (int)(ss%60);	
}


string CTime::to_string() const
{
	char ts[30];	
	int w;
	w = sprintf(ts, "%02d:%02d:%02d", GetHour(), GetMinute(), GetSecond());
	ts[w] = '\0';		
	return ts;	
}

CTime& CTime::operator=(const CTime& timeSrc)
{
	if(this == &timeSrc)
		return *this;

	hour = timeSrc.hour;
	min  = timeSrc.min;
	sec  = timeSrc.sec;

	return *this;
}

CTime& CTime::operator=(long t)
{
	CTime ct(t);

	hour = ct.hour;
	min  = ct.min;
	sec  = ct.sec;

	return *this;
}


void CTime::Serialize(CArchive& ar)
{
	if(ar.IsLoading())
		ar>>hour>>min>>sec;
	else
		ar<<hour<<min<<sec;
}

/////////////////////////////////////////////////////////////////////////////
//------------------------------ class CDateTime --------------------------------

CDateTime::CDateTime() : CTime()
{
	time_t tt;
	tt = time(NULL);
	struct tm* loctime;
	loctime = localtime(&tt);
	
	year  = loctime->tm_year+1900;
	month = loctime->tm_mon+1;
	day   = loctime->tm_mday;	
}


CDateTime& CDateTime::operator=(CDateTime& dt)
{
	if(this == &dt)
		return *this;
	*(CTime*)this = (CTime&)dt;
	year  = dt.year;
	month = dt.month;
	day   = dt.day;
	return *this;
}

CDateTime::~CDateTime()
{

}

CDateTime::CDateTime(time_t t)
{
	struct tm* loctime;
	loctime = localtime(&t);
	CDateTime dt(loctime->tm_year+1900, loctime->tm_mon+1, loctime->tm_mday,
				loctime->tm_hour, loctime->tm_min, loctime->tm_sec);	
	(*this) = dt;	
}

CDateTime::CDateTime(int y, int mon, int d,
					 int h, int min, int s)
					 : CTime(h, min, s)
{	
	assert(y>=1969 && y<=2068);
	year  = y;
	assert(mon>=1 && mon<=12);
	month = mon;
	assert(d>=1);
	if(mon==1 || mon==3 || mon==5 ||  mon==7 ||
		 mon==8 ||  mon==10 ||  mon==12)
		 assert(d<=31);
	else{
		if(mon==2){
			if((y%400==0) || ((y%4==0) && (y%100!=0)))
				assert(d<29);
			else
				assert(d<28);
		}
		else
			assert(d<30);
	}
	day   = d;
}

const CDateTime& CDateTime::operator=(const CDateTime& dt)
{
	if(this == &dt)
		return *this;

	*((CTime*)this) = (CTime)dt;
	year  = dt.year;
	month = dt.month;
	day   = dt.day;

	return *this;
}

string CDateTime::date_string() const
{
	char ds[20];	
	int w;
	w = sprintf(ds, "%d-%d-%d", GetYear(), GetMonth(), GetDay());
	ds[w] = '\0';		
	return ds;
}


long CDateTime::to_long() const
{
	time_t tt=0;
	struct tm* mt;
	mt = localtime(&tt);
	mt->tm_year = GetYear()-1900;
	mt->tm_mon  = GetMonth()-1;
	mt->tm_mday = GetDay();
	mt->tm_hour = GetHour();
	mt->tm_min  = GetMinute();
	mt->tm_sec  = GetSecond();	
	return mktime(mt)-3600;	
}

string CDateTime::to_string() const
{
	string s;
	s += date_string();
	s += "  ";
	s += time_string();
	return s;
}


void CDateTime::Serialize(CArchive& ar)
{
	CTime::Serialize(ar);

	if(ar.IsLoading())
		ar>>year>>month>>day;
	else
		ar<<year<<month<<day;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -