コード例 #1
0
ファイル: lcd.cpp プロジェクト: rpheuts/artillery
string LCD::GetDate()
{
	char buf[20];
	string tmp = GetDayOfTheWeek((int) _timed->day, (int) _timed->month, (int) _timed->year);
	sprintf(buf, "%02d.%02d.%02d",_timed->day,_timed->month, _timed->year);
	tmp.append(buf);
	return tmp;
}
コード例 #2
0
/*!
\brief Get the year, month, and day in month from a week in year
\details An ISO week-numbering year has 52 or 53 full weeks (364 or 371 days)
\param year [in, out] Gregorian year
\param weekInYear [in] Gregorian week in a year
\param month [out] Gregorian month
\param dayInMonth [out] Gregorian day in month
\returns \c True if success or \c False if error
*/
bool GetDayAndMonthFromWeekInYear(int *year, int weekInYear, int *month, 
    int *dayInMonth) 
{
    int daysInMonth;
     int weeksInYear;
    *month = ISO_FIRST_MONTH_IN_YEAR;
    bool hasChanged;

    do
    {
        hasChanged = false;

        weeksInYear = GetNumberOfWeeksInYear(*year);
        if (weekInYear > weeksInYear)
        {
            weekInYear -= weeksInYear;
            (*year)++;
            hasChanged = true;
        }

    } while(hasChanged);

    int dayInYear = (weekInYear - 1)  * ISO_DAYS_A_WEEK + 1;

    // Since the first day of week 1 in a year in the Gregorian calendar is not usually January 1st we need to handle the offset
    static int t[] = {0, 0, -1, -2, -3, 3, 2, 1};
    int jan1DayOfWeek = GetDayOfTheWeek(*year, 1, 1); 
    dayInYear += t[jan1DayOfWeek];

    if (dayInYear <= 0)
    {
        // dayInYear is in the previous year
        (*year)--;
        dayInYear += GetDaysInYear(*year);
    }
    else
    {
        int daysInYear = GetDaysInYear(*year);
        if (dayInYear > daysInYear)
        {
            // dayInYear is in the next year
            (*year)++;
            dayInYear -= daysInYear;
        }
    }

    if (!GetDayAndMonthFromDayInYear(*year, dayInYear, month, dayInMonth))
        return false;

    return true;
}
コード例 #3
0
/*!
\brief Get the number of weeks in a year
\par Algorithm
There are 52 weeks in most years however, years that begin on a Thursday and leap years that begin on a Wednesday have 53 weeks.                
*/
int GetNumberOfWeeksInYear(int year) 
{
    int jan1DayOfWeek = GetDayOfTheWeek(year, 1, 1);                    
    return jan1DayOfWeek == 4 || (jan1DayOfWeek == 3 && IsALeapYear(year)) ? 53 : 52;
}