TDateTime::TDateTime(const char* src, TDateTimeFlag flag)
{
	unsigned  int year = 0;
	unsigned  int month = 0;
	unsigned  int day = 0;
	unsigned  int hour = 0;
	unsigned  int min = 0;
	unsigned  int sec = 0;
 
	int result=0;
	Val = 0;

	if(flag == DATE_POS)
	{
		result=DecodeDateString(src,year,month,day);
		if(result == 0 )
		{
			Val = INVALID_DATE_VALUE;
		}
		else
		{
			Val = ComposeDate(year, month, day);
		}
	}
	else if(flag == TIME_POS )
	{
		result=DecodeTimeString(src,hour,min,sec);
		if(result == 0 )
		{
			Val = INVALID_DATE_VALUE;
		}
		else
		{
			Val = ComposeTime(hour, min, sec);
		}
	}
	else if(flag == DATETIME_POS )
	{
		result=DecodeDateTimeString(src,year,month,day,hour,min,sec);
		if(result == 0 )
		{
			Val = INVALID_DATE_VALUE;
		}
		else
		{
			Val = ComposeDate(year, month, day);
			Val += ComposeTime(hour, min, sec);
		}
	}
 

}
TDateTime TDateTime::MonthAdd(const TDateTime & rhs, int Months)
{

	unsigned  int year=0;
	unsigned  int month=0;
	unsigned  int day=0;
	unsigned  int lastday=0;
	int _Months=0;

	TDateTime tmp(rhs);
	tmp.DecodeDate(year, month, day);
	_Months = month + Months;
	if (_Months >= 0) {
		year += _Months / 12;
		month = _Months % 12;
		if (month == 0) {
			year--;
			month = 12;
		}
	} else {
		_Months = -_Months;
		year -= (_Months / 12 + 1);
		month = 12 - _Months % 12;
	}
	lastday = DaysInMonths[IsLeapYear(year)][month];
	if (day > lastday)
		day = lastday;
	tmp = ComposeDate(year, month, day);
	return tmp;
}
 TDateTime::TDateTime(unsigned int date, unsigned int time)
 {
	 unsigned int year, month, day, hour, min, sec;
     year = date/10000;
     month = (date - year*10000)/100;
	 day = date%100;

	 hour = time/10000;
     min = (time - hour*10000)/100;
	 sec = time%100;
     
     if (DateIsValid(year, month, day))
	{
		Val = ComposeDate(year, month, day);
		if (TimeIsValid(hour, min, sec))
		{
			Val += ComposeTime(hour, min, sec);
		}
		else 
		{
			Val = INVALID_DATE_VALUE;
		}
	}
	else 
	{
		Val = INVALID_DATE_VALUE;
	}	
 }
TDateTime::TDateTime(unsigned int   year, unsigned int   month,
                     unsigned int   day)
{
	if (DateIsValid(year, month, day))
	{
		Val = ComposeDate(year, month, day);
	}
	else {
		Val = INVALID_DATE_VALUE;
	}
}
TDateTime::TDateTime(unsigned int   year, unsigned int   month, unsigned int   day,
		     unsigned int   hour, unsigned int   min, unsigned int   sec)
{
	if (DateIsValid(year, month, day))
	{
		Val = ComposeDate(year, month, day);
		if (TimeIsValid(hour, min, sec))
		{
			Val += ComposeTime(hour, min, sec);
		}
		else 
		{
			Val = INVALID_DATE_VALUE;
		}
	}
	else 
	{
		Val = INVALID_DATE_VALUE;
	}	
}
void CLoadDialog::OnOK() 
{
	UpdateData( TRUE );

	m_nFlags = DetermineFlags();

	// Create list of filenames, if possible
	CButton *radioByDate = static_cast< CButton * >( GetDlgItem( IDC_LOADBYDATE ) );
	if ( radioByDate && radioByDate->GetCheck() == 1 )
	{
		m_FileNames.RemoveAll();

		// Use dates to determine filename, otherwise, clicking the "choose files button probably
		//  created a list for us

		char const *starttime = m_strStartDate;
		char const *endtime = m_strEndDate;

		int m1, d1, y1;
		int m2, d2, y2;

		if ( SuckTimeFromString( starttime, &m1, &d1, &y1 ) &&
			SuckTimeFromString( endtime, &m2, &d2, &y2 ) )
		{
			struct tm t1;
			struct tm t2;
			
			if ( ComposeDate( &t1, m1, d1, y1 ) &&
				ComposeDate( &t2, m2, d2, y2 ) )
			{
				time_t start = mktime( &t1 );
				time_t end = mktime( &t2 );
				
				if ( start > end )
				{
					time_t temp = start;
					start = end;
					end = temp;
				}
				
				if ( difftime( end, start ) < FIVE_YEARS )
				{
					// Create file list
					m_FileNames.RemoveAll();
					
					for ( time_t t = start; 
					t <= (time_t)( end + FSECONDS_PER_DAY ) || m_pStatus->IsInSameDay( t, end ) ; 
					t += (int)FSECONDS_PER_DAY )
					{
						char const *fname = m_pStatus->FileNameForTime( t );
						ASSERT( fname );
						int i = m_FileNames.AddToTail();
						ASSERT( m_FileNames.IsValidIndex( i ) );
						FilenameList *entry = &m_FileNames[ i ];
						strncpy( entry->fn, fname, sizeof( entry->fn ) );
						entry->fn[ sizeof( entry->fn ) - 1 ] = 0;
					}
					
					char sz[ 512 ];
					sprintf( sz, "(%i) files in list.", GetNumFiles() );
					
					m_staticNumFiles.SetWindowText( sz );
					
				}
			}
		}
	}

	CDialog::OnOK();
}