123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374 |
- #include "stdafx.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- struct ConvDate
- {
- int Source;
- int SolarYear;
- int SolarMonth;
- int SolarDate;
- int LunarYear;
- int LunarMonth;
- int LunarDate;
- int Weekday;
- int Kan;
- int Chih;
- };
- struct tagLunarCal
- {
- char BaseDays;
- char Intercalation;
- char BaseWeekday;
- char BaseKanChih;
- char MonthDays[13];
- };
- struct tagLunarCal LunarCal[] = {
- { 23, 3, 2, 17, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0 },
- { 41, 0, 4, 23, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1 },
- { 30, 7, 5, 28, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1 },
- { 49, 0, 6, 33, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1 },
- { 38, 0, 0, 38, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1 },
- { 26, 6, 2, 44, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0 },
- { 45, 0, 3, 49, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 },
- { 35, 0, 4, 54, 0, 1, 0, 1, 0, 1, 1,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0, 0, 28, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0 },
- { 35, 0, 1, 33, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0 },
- { 24, 4, 2, 38, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1 },
- { 42, 0, 4, 44, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1 },
- { 31, 0, 5, 49, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0 },
- { 21, 2, 6, 54, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1 },
- { 40, 0, 0, 59, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1 },
- { 28, 6, 2, 5, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0 },
- { 47, 0, 3, 10, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1 },
- { 36, 0, 4, 15, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1 },
- { 25, 5, 5, 20, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0 },
- { 43, 0, 0, 26, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1 },
- { 32, 0, 1, 31, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0 },
- { 22, 3, 2, 36, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0 }};
- const int FIRST_YEAR = 1936;
- const int LAST_YEAR = FIRST_YEAR + sizeof(LunarCal)/sizeof(struct tagLunarCal) - 1;
- int arr_nDays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
- int SolarDays[2][14] = {{0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365, 396 },{0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366, 397 }};
-
- #define NO_ERROR 0
- const char INPUT_YEAR_ERROR = 1;
- const char INPUT_MONTH_ERROR = 2;
- const char INPUT_DAY_ERROR = 3;
- int IsLeap(int _nYear )
- {
- if ( _nYear % 400 == 0 ) return 1;
- else if ( _nYear % 100 == 0 ) return 0;
- else if ( _nYear % 4 == 0 ) return 1;
- else return 0;
- }
- char CalConv( struct ConvDate *_stDate )
- {
- int nIsLeap;
- int nMonthDay;
- int nSolarMonth;
- int nYearOffset;
- int im;
- int l1;
- int l2;
- int nDayOfYear;
- int i;
- int lm;
- int nKanChih;
- if ( _stDate->Source == 0 )
- {
- if ( _stDate->SolarYear <= FIRST_YEAR || _stDate->SolarYear > LAST_YEAR )
- {
- return INPUT_YEAR_ERROR;
- }
-
- nSolarMonth = _stDate->SolarMonth - 1;
- if ( nSolarMonth < 0 || nSolarMonth > 11 )
- {
- return INPUT_MONTH_ERROR;
- }
- nIsLeap = IsLeap( _stDate->SolarYear );
-
- if ( nSolarMonth == 1 )
- {
- nMonthDay = nIsLeap + 28;
- }
- else
- {
- nMonthDay = arr_nDays[nSolarMonth];
- }
-
- if ( _stDate->SolarDate < 1 || _stDate->SolarDate > nMonthDay )
- {
- return INPUT_DAY_ERROR;
- }
-
- nYearOffset = _stDate->SolarYear - FIRST_YEAR;
-
- nDayOfYear = SolarDays[nIsLeap][nSolarMonth] + _stDate->SolarDate;
- _stDate->Weekday = ( nDayOfYear + LunarCal[nYearOffset].BaseWeekday ) % 7;
- nKanChih = nDayOfYear + LunarCal[nYearOffset].BaseKanChih;
- _stDate->Kan = nKanChih % 10;
- _stDate->Chih = nKanChih % 12;
- if ( nDayOfYear < LunarCal[nYearOffset].BaseDays + 1 )
- {
- nYearOffset--;
- _stDate->LunarYear = _stDate->SolarYear - 1;
- nIsLeap = IsLeap( _stDate->LunarYear );
- nSolarMonth += 12;
- nDayOfYear = SolarDays[nIsLeap][nSolarMonth] + _stDate->SolarDate;
- }
- else
- {
- _stDate->LunarYear = _stDate->SolarYear;
- }
- l1 = LunarCal[nYearOffset].BaseDays;
- for ( i=0; i<13; i++ )
- {
- l2 = l1 + LunarCal[nYearOffset].MonthDays[i] + 29;
- if ( nDayOfYear <= l2 )
- break;
- l1 = l2;
- }
- _stDate->LunarMonth = i + 1;
- _stDate->LunarDate = nDayOfYear - l1;
- im = LunarCal[nYearOffset].Intercalation;
- if ( im != 0 && _stDate->LunarMonth > im )
- {
- _stDate->LunarMonth--;
- if ( _stDate->LunarMonth == im )
- _stDate->LunarMonth = -im;
- }
- if ( _stDate->LunarMonth > 12 )
- _stDate->LunarMonth -= 12;
- }
- else
- {
- if ( _stDate->LunarYear < FIRST_YEAR || _stDate->LunarYear >= LAST_YEAR )
- {
- return INPUT_YEAR_ERROR;
- }
- nYearOffset = _stDate->LunarYear - FIRST_YEAR;
- im = LunarCal[nYearOffset].Intercalation;
- lm = _stDate->LunarMonth;
- if ( lm < 0 )
- {
- if ( lm != -im ) return INPUT_MONTH_ERROR;
- }
- else if ( lm < 1 || lm > 12 ) return INPUT_MONTH_ERROR;
- if ( im != 0 )
- {
- if ( lm > im ) lm++;
- else if ( lm == -im ) lm = im + 1;
- }
- lm--;
- if ( _stDate->LunarDate > LunarCal[nYearOffset].MonthDays[lm] + 29 ) return INPUT_DAY_ERROR;
- nDayOfYear = LunarCal[nYearOffset].BaseDays;
- for ( i=0; i<lm; i++ )
- {
- nDayOfYear += LunarCal[nYearOffset].MonthDays[i] + 29;
- }
- nDayOfYear += _stDate->LunarDate;
- nIsLeap = IsLeap( _stDate->LunarYear );
- for ( i=13; i>=0; i-- )
- {
- if ( nDayOfYear > SolarDays[nIsLeap][i] ) break;
- }
- _stDate->SolarDate = nDayOfYear - SolarDays[nIsLeap][i];
- if ( i <= 11 )
- {
- _stDate->SolarYear = _stDate->LunarYear;
- _stDate->SolarMonth = i + 1;
- }
- else
- {
- _stDate->SolarYear = _stDate->LunarYear + 1;
- _stDate->SolarMonth = i - 11;
- }
-
- nIsLeap = IsLeap( _stDate->SolarYear );
- nYearOffset = _stDate->SolarYear - FIRST_YEAR;
- nDayOfYear = SolarDays[nIsLeap][_stDate->SolarMonth-1] + _stDate->SolarDate;
- _stDate->Weekday = ( nDayOfYear + LunarCal[nYearOffset].BaseWeekday ) % 7;
- nKanChih = nDayOfYear + LunarCal[nYearOffset].BaseKanChih;
- _stDate->Kan = nKanChih % 10;
- _stDate->Chih = nKanChih % 12;
- }
- return NO_ERROR;
- }
- void GongToLongOld(CString &date)
- {
- if(date.GetLength()!=10)return;
- struct ConvDate stConvDate;
- stConvDate.Source=0;
- stConvDate.SolarYear=atoi(date.Mid (0,4));
- stConvDate.SolarMonth=atoi(date.Mid (5,2));
- stConvDate.SolarDate=atoi(date.Mid (8,2));
- date="2020-13-33";
- if(0!=CalConv(&stConvDate))return;
- date.Format ("%04d-%02d-%02d", stConvDate.LunarYear,stConvDate.LunarMonth,stConvDate.LunarDate );
- }
- void LongToGong(CString &date)
- {
- if(date.GetLength()!=10)return;
- struct ConvDate stConvDate;
- stConvDate.Source=1;
- stConvDate.LunarYear=atoi(date.Mid (0,4));
- stConvDate.LunarMonth=atoi(date.Mid (5,2));
- stConvDate.LunarDate=atoi(date.Mid (8,2));
- date="2020-13-33";
- if(0!=CalConv(&stConvDate))return;
- date.Format ("%04d-%02d-%02d", stConvDate.SolarYear,stConvDate.SolarMonth,stConvDate.SolarDate );
- }
|