100年日歷帶星期計算
發(fā)布時間:2008年1月5日 1時37分
// CalculateVC.cpp : Defines the entry point for the console application by VC6.0 // Function : simulate singlechip Demo program // Create time: 2006-11-21 // lhx_127@126.com #include "stdafx.h" #include <windows.h> static unsigned char ucCount; unsigned char const pWeek_tab[] = {0,1,4,4,0,2,5,0,3,6,1,4,6}; unsigned char const pMonth_tab[] = {0, 31, //1 28, //2 31, //3 30, //4 31, //5 30, //6 31, //7 31, //8 30, //9 31, //10 30, //11 31 //2 }; typedef struct _SoftTime { unsigned char Seconds; unsigned char Minutes; unsigned char Hours; unsigned char WeekDays; unsigned char Days; unsigned char Months; unsigned char Years; } SoftTime, *pSoftTime ; SoftTime structSoftTime; //=================================================================== //Function Name :CalculateWeekDay // Function : According to date calculation weeks from the first // year to the year 5535, no matter from the system // when the week began and ended //input : Year,Month,Day //OutPut : Week //global : None //=================================================================== unsigned char CalculateWeekDay(unsigned int Year,unsigned char Month,unsigned char Date) { unsigned char err; if((Month<3) && (!(Year&0x03) && (Year%1000) || (!(Year%400)))) { Date--; } err = (Date + Year + Year/4 + Year/400 - Year/100 + pWeek_tab[Month]- 2)%7; return err; } void SoftTime_UpdataData(void) { if( structSoftTime.Days >= pMonth_tab[structSoftTime.Months]) { if( structSoftTime.Months == 2) { if(!(structSoftTime.Years & 0x03 )) { if(structSoftTime.Days >= 29 ) structSoftTime.Days = 1; else{structSoftTime.Days++; structSoftTime.Months--; } } else structSoftTime.Days = 1; } else structSoftTime.Days = 1; if( structSoftTime.Months >= 12) { structSoftTime.Months = 1; structSoftTime.Years++; if(structSoftTime.Years == 100) structSoftTime.Years = 0; }else structSoftTime.Months++; //{if( !(!(structSoftTime.Years & 0x03) && (structSoftTime.Months == 2)) )structSoftTime.Months++; } }else structSoftTime.Days++; } int main(int argc, char* argv[]) { unsigned char temp; unsigned char Week; structSoftTime.Years = 20; structSoftTime.Months = 11; structSoftTime.Days = 21; structSoftTime.Hours = 12; structSoftTime.Minutes = 10; Week = CalculateWeekDay(2006,1,21); printf("this Week is %d \n",Week); while (1) { // Sleep(1); ucCount++; if(ucCount == 2) { ucCount = 0; structSoftTime.Seconds++; } if(structSoftTime.Seconds >= 59 ) { structSoftTime.Seconds = 0; if(structSoftTime.Minutes >= 59) { printf(" data -- %2d-%2d-%2d---%2d-%2d\n",structSoftTime.Years, structSoftTime.Months, structSoftTime.Days,structSoftTime.Hours,structSoftTime.Minutes); structSoftTime.Minutes = 0; if(structSoftTime.Hours >= 23 ) { structSoftTime.Hours = 0; SoftTime_UpdataData(); } else structSoftTime.Hours++; } else structSoftTime.Minutes++; } } return 0; }
|