// TODO 17-1.01
struct tm RTC_TimeStruct;
const char *monthArray[] =
	{"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
const char *weekArray[] =
	{"SUN", "MON", "TUE", "WED", "THR", "FRI", "SAT"};
int Year, Month, Date, Week;
int Hour, Minute, Second;

void RTC_CalculateWeekday( struct tm *pTimeStruct )
{
    // Zeller's congruence to use Gregorian calendar
    int J = (pTimeStruct->tm_year+1900) / 100;
    int K = (pTimeStruct->tm_year+1900) % 100;
    int m = (pTimeStruct->tm_mon<2?pTimeStruct->tm_mon+13:pTimeStruct->tm_mon+1);
    int q =  pTimeStruct->tm_mday;
    int h = (q + (int)((13*((float)m+1))/5) + K + (int)(K/4) + (int)(J/4) - 2*J) % 7;

    pTimeStruct->tm_wday = (h+7-1)%7; // Convert to Time structure format
}

void RTC_SetCompilerTime( struct tm *pTimeStruct )
{
    char monthStr[3];
    int year, month, day, hour, min, sec;

    sscanf(__DATE__, "%s %d %d", monthStr, &day, &year );
    sscanf(__TIME__, "%d:%d:%d", &hour, &min, &sec );

    for( month=0 ; month<12 && strcmp(monthStr, monthArray[month]) ; month++ );

    pTimeStruct->tm_year = year-1900;
    pTimeStruct->tm_mon  = month;
    pTimeStruct->tm_mday = day;
    pTimeStruct->tm_hour = hour;
    pTimeStruct->tm_min  = min;
    pTimeStruct->tm_sec  = sec;

    // Set compiler time as initial time
    RTC_RTCCTimeSet( pTimeStruct );	
    // Calculate Week Day
    RTC_CalculateWeekday( pTimeStruct );
}


// TODO 17-1.02
    RTC_SetCompilerTime( &RTC_TimeStruct );


// TODO 17-1.03
            RTC_RTCCTimeGet( &RTC_TimeStruct );
            RTC_CalculateWeekday( &RTC_TimeStruct );
            Year   = RTC_TimeStruct.tm_year+1900;
            Month  = RTC_TimeStruct.tm_mon;
            Date   = RTC_TimeStruct.tm_mday;
            Week   = RTC_TimeStruct.tm_wday;
            Hour   = RTC_TimeStruct.tm_hour;
            Minute = RTC_TimeStruct.tm_min;
            Second = RTC_TimeStruct.tm_sec;
            sprintf( (char *)USARTRTxBuffer, "%04d-%s-%02d(%s)", Year, monthArray[Month], Date, weekArray[Week] );
            LCM_DrawString( 0, 2, (char *)USARTRTxBuffer );
            myprintf( "\033[1;1H%s ", USARTRTxBuffer );
            sprintf( (char *)USARTRTxBuffer, "%02d:%02d:%02d", Hour, Minute, Second );
            LCM_DrawString( 0, 3, (char *)USARTRTxBuffer );
            myprintf( "%s", USARTRTxBuffer );
