Mgdate.cpp

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <iostream.h>
#include <string.h>

#include "mgdate.h"

static int Days[2][12]= {
    { 31, 28, 31,30, 31, 30, 31,31, 30,31, 30, 31 },
    { 31, 29, 31,30, 31, 30, 31,31, 30,31, 30, 31 }

};

bool MGDate::Set( int M, int D, int Y )
{
	if( M==0 && D == 0 && Y == 0 )
	{
		time_t tNow;
		time(&tNow);
		struct tm* Now = localtime( &tNow);
		m_M = Now->tm_mon+1;
		m_D = Now->tm_mday;
		m_Y = Now->tm_year+1900;
		return( true );
	}

	int Leap = ( (Y%4)==0 && (Y%100)!=0 || (Y%400)==0 )? 1 : 0;
	if( M < 1 || M > 12 || D < 1 || D > Days[Leap][M-1] || Y < 1 || Y > 9999 )
		return( false );
	m_M = M;
	m_D = D;
	m_Y = Y;
	return( true );
}

bool MGDate::Set( const char* Src )
{
	int M, D, Y;

	M = atoi( Src );
	while( *Src && *Src>='0' && *Src<='9' )		Src++;
	while( *Src && !(*Src>='0' && *Src<='9') )	Src++;
	D = atoi( Src );
	while( *Src && *Src>='0' && *Src<='9' )		Src++;
	while( *Src && !(*Src>='0' && *Src<='9') )	Src++;
	Y = atoi( Src );
	while( *Src && *Src>='0' && *Src<='9' )		Src++;
	return( Set( M, D, Y ) );
}

void MGDate::Get( char* Dest )
{
	sprintf( Dest, "%d/%d/%d", m_M, m_D, m_Y );
}

void MGDate::Output()
{
	cout << m_M << "/" << m_D << "/" << m_Y;
}