//*****************************************************************************************
void CBCGPDateTimeCtrl::ChangeMonth (UINT uiMonthLetter)
{
	ASSERT (m_monthFormat == 0 || m_monthFormat == 1);

	CBCGPDefaultLocale dl;

	int iDay = m_Date.GetDay ();
	int iMonth = m_Date.GetMonth ();
	int iYear = m_Date.GetYear ();
	int iHour = m_Date.GetHour ();
	int iMin = m_Date.GetMinute ();

	BOOL bLastDayInMonth = (iDay == GetDaysInMonth (iMonth, iYear));

	BOOL bFound = FALSE;
	for (int i = iMonth + 1; i != iMonth; i ++)
	{
		if (i > 12)
		{
			i = 1;
		}

		if (i == iMonth)
		{
			break;
		}

		//--------------------------------------------------
		// Compare manth 'i' first char with the typed char:
		//--------------------------------------------------
		CString strMonth = COleDateTime (iYear, i, 1, 0, 0, 0).
			Format (m_monthFormat == 0 ? _T ("%b") : _T ("%B"));

		if (strMonth.GetLength () > 1 &&
			strMonth.GetAt (0) == (char) uiMonthLetter)
		{
			iMonth = i;
			bFound = TRUE;
			break;
		}
	}

	if (bFound)
	{
		if (bLastDayInMonth ||
			iDay > GetDaysInMonth (iMonth, iYear))
		{
			iDay = GetDaysInMonth (iMonth, iYear);
		}

		COleDateTime date (iYear, iMonth, iDay, iHour, iMin, 0);
		if (IsDateValid (date))
		{
			m_Date = date;
			RedrawWindow (m_rectText);

			OnDateChanged ();
		}
	}
}
//*****************************************************************************************
void CBCGPDateTimeCtrl::ChangeAmPm (UINT uiAmPm)
{
	int iDay = m_Date.GetDay ();
	int iMonth = m_Date.GetMonth ();
	int iYear = m_Date.GetYear ();
	int iHour = m_Date.GetHour ();
	int iMin = m_Date.GetMinute ();

	CString str;
	str += (char) uiAmPm;
	str.MakeUpper ();

	if (str == m_strPM [0] && iHour < 12)
	{
		iHour += 12;
	}

	if (str == m_strAM [0] && iHour >= 12)
	{
		iHour -= 12;
	}

	COleDateTime date (iYear, iMonth, iDay, iHour, iMin, 0);
	if (IsDateValid (date) && m_Date != date)
	{
		m_Date = date;
		RedrawWindow (m_rectText);

		OnDateChanged ();
	}
}
/*--------------------------------------------------------------
 * Loads a date passed as three integers into a tm structure.
 * Returns 0 on success, -1 on error.
 *-------------------------------------------------------------*/
int LoadDateStruct ( struct tm *date, int yy, int mm, int dd )
{
    date->tm_year = yy;
    date->tm_mon  = mm - 1; /* for compatibility w/ ANSI C */
    date->tm_mday = dd;

    if ( IsDateValid ( date ) == NO )
        return ( -1 );
    else
        return ( 0 );
}
Exemplo n.º 4
0
date_t saisiDate(int typeRequest)
{
    date_t date;
    printf("Veuillez choisir une date à venir: \n");
    date.jour = capChar("un jour");
    date.mois = capChar("un mois");
    date.annee = capChar("une année");
    if ((typeRequest == CONS) && (IsDateValid(date.jour, date.mois, date.annee)))
    {
        return (date);
    }
    else if ((typeRequest == RES) && (IsDateValidAndLater(date.jour, date.mois, date.annee)))
    {
        return (date);
    }
    printf("La date n'est pas valide.\n");
    return saisiDate(typeRequest);
}
Exemplo n.º 5
0
DVT_STATUS VALUE_DT_CLASS::Check (UINT32 flags,
                                  BASE_VALUE_CLASS **,
                                  LOG_MESSAGE_CLASS *messages,
                                  SPECIFIC_CHARACTER_SET_CLASS *)

//  DESCRIPTION     : Check DT format.
//  PRECONDITIONS   :
//  POSTCONDITIONS  :
//  EXCEPTIONS      :
//  NOTES           :
//<<===========================================================================
{
    DVT_STATUS      result = MSG_OK;
    int             length = valueM.length();
	int				maxLength;
    int             index;
    int             frac_index = 0;
    unsigned int    offset_index = 0;
    string          syntax = "";
    int             second;
    int             minute;
    int             hour;
    int             day;
    int             month;
    int             year;
    char            message[256];

    if (length == 0)
        return (MSG_OK);

	if (flags & ATTR_FLAG_RANGES_ALLOWED)
    {
		maxLength = DT_QR_LENGTH;
	}
	else 
    {
		maxLength = DT_LENGTH;
	}
	
	if (length > maxLength)
	{
		sprintf (message, "value length %i exceeds maximum length %i - truncated for value(%s)",
				 length, maxLength, valueM.c_str());
		messages->AddMessage (VAL_RULE_D_DT_1, message);
		length = maxLength;
		result= MSG_ERROR;
	}	

    if (length > 0)
    {
        for (index=0 ; index<length ; index++)
        {
            if ((valueM[index] >= '0') && (valueM[index] <= '9'))
            {
                syntax += 'n';
            }
            else
            {
                switch (valueM[index])
                {
                case '.':
					if (!(flags & ATTR_FLAG_RANGES_ALLOWED))
					{
						if (frac_index != 0) // twice
						{
							sprintf (message, "more then one '.' Character found at offset %d",
									index);
							messages->AddMessage (VAL_RULE_D_DT_2, message);
    						return MSG_ERROR;
						}
					}
                    syntax += '.';
                    frac_index = index + 1;
                    break;
                case '+':   // Fall through
                case '-':
					if (!(flags & ATTR_FLAG_RANGES_ALLOWED))
					{
						if (offset_index != 0) // twice
						{
							sprintf (message, "more then one '+' or '-' Character found at offset %d",
									index);
							messages->AddMessage (VAL_RULE_D_DT_2, message);
    						return MSG_ERROR;
						}
					}
                    syntax += 'S';
                    offset_index = index + 1;
                    break;
                case ' ':
                    if ( ((index + 1) % 2 != 0) ||
                         ((valueM[index+1] != NULLCHAR) && 
                          ((index + 1) < maxLength)
                         )
                       )
                    {
                        sprintf (message, "unexpected SPACE character at offset %d",
                               index+1);
                        messages->AddMessage (VAL_RULE_D_DT_2, message);
    				    return MSG_ERROR;
                    }
                    break;
			    case 0x00:
                    sprintf (message, "unexpected character [NUL]=0x00 at offset %d",
                            index+1);
                    messages->AddMessage (VAL_RULE_D_DT_2, message);
				    return MSG_ERROR;
			    case 0x0a: // Fall through
			    case 0x0d:
                    sprintf (message, "unexpected character 0x%02X at offset %d",
                            (int)(valueM[index]), index+1);
                    messages->AddMessage (VAL_RULE_D_DT_2, message);
				    return MSG_ERROR;
                default:
                    sprintf (message, "unexpected character %c=0x%02X at offset %d",
                            valueM[index], valueM[index], index+1);
                    messages->AddMessage (VAL_RULE_D_DT_2, message);
                    return MSG_ERROR;
                }
            }
        }

        // Deal with offset first. Note that an offset can not be at the
        // beginning of a string.
        if (offset_index != 0)
        {
            if (syntax.find("nnnn", offset_index) != offset_index)
            {
                sprintf (message, "invalid optional offset suffix");
                messages->AddMessage (VAL_RULE_D_DT_2, message);
				return MSG_ERROR;
            }

            hour = GetNumeric (&valueM[offset_index], 2);
            minute = GetNumeric (&valueM[offset_index + 2], 2);

            if (!(IsTimeValid (hour, minute, 0)))
            {
                sprintf (message, "invalid time value specified in offset suffix");
                messages->AddMessage (VAL_RULE_D_DT_2, message);
				return MSG_ERROR;
            }

            // chop offset off syntax string
            index = offset_index - 1;
            syntax[index] = NULLCHAR;
            hour = 0;
            minute = 0;
        }

        // Compensate the index counter used in the for-loop.
        index--;

        // Deal with frac.
		if (!(flags & ATTR_FLAG_RANGES_ALLOWED))
		{
			if ((syntax.find ("nnnnnnnnnnnnnn.n") == 0) && (index <= 21))
			{
				if (! IsNumeric (&valueM[frac_index], index-frac_index))
				{
					sprintf (message, "invalid fractional part specified");
					messages->AddMessage (VAL_RULE_D_DT_2, message);
					return MSG_ERROR;
				}
			}
			else
			{
				if (frac_index > 0)
				{
					sprintf (message,
							 "fraction '.' Character not expected at offset %d",
							 frac_index);
					messages->AddMessage (VAL_RULE_D_DT_2, message);
					return MSG_ERROR;
				}
			}
		}

        // Deal with main date/time stem - various formats
        index = 0;
        while (syntax[index] == 'n')
        {
            index++;
        }

        second = 0;
        minute = 0;
        hour = 0;
        day = 1;
        month = 1;
        year = 1900;

        switch (index)
        {
        case 14:
            second = GetNumeric(&valueM[12], 2); // Fall through
        case 12:
            minute = GetNumeric(&valueM[10], 2); // Fall through
        case 10:
            hour =   GetNumeric(&valueM[8], 2);  // Fall through
        case 8:
            day =    GetNumeric(&valueM[6], 2);  // Fall through
        case 6:
            month =  GetNumeric(&valueM[4], 2);  // Fall through
        case 4:
            year =   GetNumeric(valueM, 4);
            break;
        default:
            sprintf (message, "unexpected components - expected YYYY[MM[DD[HH[MM[SS[.FRAC]]]]]][OFFSET]");
            messages->AddMessage (VAL_RULE_D_DT_2, message);
			return MSG_ERROR;
        }

        if (IsDateValid(year, month, day) == false)
        {
            sprintf (message, "invalid date");
            messages->AddMessage (VAL_RULE_D_DT_3, message);
			return MSG_ERROR;
        }
        if (IsTimeValid(hour, minute, second) == false)
        {
            sprintf (message, "invalid time");
            messages->AddMessage (VAL_RULE_D_DT_3, message);
			return MSG_ERROR;
        }
    }

    return (result);
}
//*****************************************************************************************
void CBCGPDateTimeCtrl::PushDigit (int iDigit)
{
	int iDay = m_Date.GetDay ();
	int iMonth = m_Date.GetMonth ();
	int iYear = m_Date.GetYear ();
	int iHour = m_Date.GetHour ();
	int iMin = m_Date.GetMinute ();

	const int iYearDigits = m_type2DigitsInYear ? 2 : 4;

	int iNumber;
	if (m_iPrevDigit == -1)
	{
		iNumber = iDigit;
		m_iYearPos = 0;
	}
	else
	{
		iNumber = m_iPrevDigit * 10 + iDigit;
	}

	switch (m_CurrPartType)
	{
	case NO:
	case CHECK_BOX:
	case AMPM:
		return;

	case DAY:
		iDay = iNumber;
		break;

	case HOUR:
		if (!m_b24HoursFormat)
		{
			BOOL bPM = (iHour >= 12);
			iHour = iNumber;

			if (iHour > 12)
			{
				iHour = iDigit;
			}

			if (bPM)
			{
				iHour += 12;
			}

			if (iHour == 24)
			{
				iHour = 0;
			}

			if (iDigit != 1)	// Only 10, 11 or 12 are allowed!
			{
				iDigit = -1;
			}
		}
		else	// Normal format
		{
			iHour = iNumber;
		}
		break;

	case MIN:
		iMin = iNumber;
		break;

	case MONTH:
		if (m_monthFormat == 2 &&
			iNumber >= 1 && iNumber <= 12)
		{
			iMonth = iNumber;

			if (iDigit > 1 && m_iPrevDigit == -1)
			{
				m_iPrevDigit = 0;	// Stimulate junp to the next part
			}
		}
		break;

	case YEAR:
		if (m_type2DigitsInYear)
		{
			iYear = iYear / 100 * 100 + iNumber;

			if (iYear < m_maxYear2Digits - 99)
			{
				iYear = m_maxYear2Digits / 100 * 100 + iNumber;
			}

			if (iYear > m_maxYear2Digits)
			{
				iYear = (m_maxYear2Digits - 100) / 100 * 100 + iNumber;
			}
		}
		else
		{
			ASSERT (m_iYearPos >= 0);
			ASSERT (m_iYearPos < iYearDigits);

			//------------------------------------------------
			// Replace digit in position m_iYearPos to iDigit:
			//------------------------------------------------
			int iYearNew = 0;

			for (int iPos = 0; iPos < iYearDigits; iPos ++)
			{
				int iTens = 1;
				for (int i = 0; i < iYearDigits - iPos - 1; i ++)
				{
					iTens *= 10;
				}

				iYearNew *= 10;
				if (iPos == m_iYearPos)
				{
					iYearNew += iDigit;
				}
				else
				{
					iYearNew += iYear / iTens;
				}

				iYear %= iTens;
			}

			iYear = iYearNew;
		}

		m_iYearPos ++;
		break;
	}

	COleDateTime date (iYear, iMonth, iDay, iHour, iMin, 0);
	BOOL bValidDate = IsDateValid (date);
	
	if (bValidDate)
	{
		m_Date = date;
		RedrawWindow (m_rectText);

		OnDateChanged ();
	}

	if (m_iPrevDigit == -1)	// First push
	{
		m_iPrevDigit = iDigit;
	}
	else
	{
		if (bValidDate && m_iPartNum < m_iPartsNumber - 1)
		{
			if (m_CurrPartType != YEAR || m_iYearPos == iYearDigits)
			{
				m_iPrevDigit = -1;
				SelectNext ();
			}
		}
	}
}
//*****************************************************************************************
void CBCGPDateTimeCtrl::ScrollCurrPartToLimit (BOOL bTop)
{
	int iDay = m_Date.GetDay ();
	int iMonth = m_Date.GetMonth ();
	int iYear = m_Date.GetYear ();
	int iHour = m_Date.GetHour ();
	int iMin = m_Date.GetMinute ();

	switch (m_CurrPartType)
	{
	case NO:
	case CHECK_BOX:
	case AMPM:
	case YEAR:
		return;

	case DAY:
		if (bTop)
		{
			iDay = 1;
		}
		else
		{
			iDay = GetDaysInMonth (iMonth, iYear);
		}
		break;

	case MONTH:
		if (bTop)
		{
			iMonth = 1;
		}
		else
		{
			iMonth = 12;
		}
		break;

	case HOUR:
		if (bTop)
		{
			iHour = 0;
		}
		else
		{
			iHour = 23;
		}
		break;

	case MIN:
		if (bTop)
		{
			iMin = 0;
		}
		else
		{
			iMin = 59;
		}
		break;
	}

	COleDateTime date (iYear, iMonth, iDay, iHour, iMin, 0);
	if (IsDateValid (date) && m_Date != date)
	{
		m_Date = date;
		RedrawWindow (m_rectText);

		OnDateChanged ();
	}
}
//*****************************************************************************************
void CBCGPDateTimeCtrl::ScrollCurrPart (int iDir)
{
	int iDay = m_Date.GetDay ();
	int iMonth = m_Date.GetMonth ();
	int iYear = m_Date.GetYear ();
	int iHour = m_Date.GetHour ();
	int iMin = m_Date.GetMinute ();
	BOOL bLastDayInMonth = (iDay == GetDaysInMonth (iMonth, iYear));

	switch (m_CurrPartType)
	{
	case NO:
	case CHECK_BOX:
		return;

	case DAY:
		iDay += iDir;
		if (iDay <= 0)
		{
			iDay = GetDaysInMonth (iMonth, iYear);
		}

		if (iDay > GetDaysInMonth (iMonth, iYear))
		{
			iDay = 1;
		}
		break;

	case MONTH:
		iMonth += iDir;
		if (iMonth <= 0)
		{
			iMonth = 12;
		}

		if (iMonth > 12)
		{
			iMonth = 1;
		}
		
		if (bLastDayInMonth ||
			iDay > GetDaysInMonth (iMonth, iYear))
		{
			iDay = GetDaysInMonth (iMonth, iYear);
		}

		break;

	case YEAR:
		iYear += iDir;
		iYear = min(iLastAllowedYear, max(iFirstAllowedYear, iYear));

		if (iDay > GetDaysInMonth (iMonth, iYear))
		{
			iDay = GetDaysInMonth (iMonth, iYear);
		}

		break;

	case HOUR:
		iHour += iDir;
		if (iHour < 0)
		{
			iHour = 23;
		}

		if (iHour > 23)
		{
			iHour = 0;
		}
		break;

	case MIN:
		iMin += iDir;
		if (iMin < 0)
		{
			iMin = 59;
		}

		if (iMin > 59)
		{
			iMin = 0;
		}
		break;

	case AMPM:
		if (iHour < 12)
		{
			iHour += 12;
		}
		else
		{
			iHour -= 12;
		}
	}

	COleDateTime date (iYear, iMonth, iDay, iHour, iMin, 0);
	COleDateTime dateEmpty;

	if (iDir > 0 && date > m_MaxDate && m_MaxDate != dateEmpty)
	{
		date = m_MaxDate;
	}

	if (iDir < 0 && date < m_MinDate)
	{
		date = m_MinDate;
	}

	if (m_Date != date && IsDateValid (date))
	{
		m_Date = date;
		RedrawWindow (m_rectText);

		OnDateChanged ();
	}
}
Exemplo n.º 9
0
/*****************************************************************************
 * Function - IsValid
 * DESCRIPTION:
 * Returns true if all of DAY MONTH YEAR HOURS MINUTES and SECUNDS have been
 * set.
 ****************************************************************************/
bool MpcTime::IsValid(void)
{
   return IsDateValid() && IsTimeValid();
}
/*--------------------------------------------------------------
 * Returns a long which contains the number of days since
 * Jan 1, AD 1; returns -1L on error. See text for observations
 * on the use of this function.
 *-------------------------------------------------------------*/
long DateToDayNumber ( struct tm * date )
{
    long day_num;
    int  mm, yy;

    if ( IsDateValid ( date ) == NO )
        return ( -1L );

    yy = date->tm_year;
    mm = date->tm_mon + 1;

    /* get all the regular days in preceding years */

    day_num =  ( yy - 1 ) * 365L;

    /* now get the leap years */

    day_num += ( yy - 1 ) / 4L;

    /*
     * now back out the century years that are not leap:
     * this would be all century years that are
     * not evenly divisible by 400: 1700, 1800, 1900, 2100...
     */

    day_num -= ( yy - 1 ) / 100L;
    day_num += ( yy - 1 ) / 400L;

    /*
     * before 1582 all century years were leap, so adjust for
     * this. If year is > 1582, then just add 12 days for years
     * 100, 200, 300, 500, 600, 700, 900, 1000, 1100, 1300, 1400
     * and 1500. Otherwise, calculate it.
     */

    if ( yy - 1 > 1582L )
        day_num += 12L;
    else
    {
        day_num += ( yy - 1 ) / 100L;
        day_num -= ( yy - 1 ) / 400L;
    }

    /* now, add the days elapsed in the year so far */

    if ( IsYearLeap ( date->tm_year ) == NO )
        while ( --mm )
            day_num += Days_in_month[ mm ];
    else
        while ( --mm )
            day_num += Days_in_month_leap[ mm ];

    /* add days in current month for the year being evaluated */

    day_num += date->tm_mday;

    /*
     * now adjust for the 10 days cut out of the calendar when
     * the change was made to the Gregorian calendar. This change
     * reflects the jump from October 4 to October 15, 1582, a
     * deletion of 10 days.
     */

    if ( day_num > 577737L )
        day_num -= 10L;

    return ( day_num );
}