#include "stdafx.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
struct ConvDate
{
 int Source;  // ==0 ����������Ϊ����, !=0 ������Ϊũ��
 int SolarYear; //���������֮������� YYYY
 int SolarMonth; //������ MM
 int SolarDate; //������ DD
 int LunarYear; //���������֮ũ����� YYYY
 int LunarMonth; //ũ���� ������ʾ���� 
 int LunarDate; //ũ����
 int Weekday; //����Ϊ���ڼ� (0������ 1����һ 2���ڶ� 3������ 4������ 5������ 6������)
 int Kan;  //�������(0�� 1�� 2�� 3�� 4�� 5�� 6�� 7�� 8�� 9��)
 int Chih;  //���յ�֧(0�� 1�� 2�� 3î 4�� 5�� 6�� 7δ 8�� 9�� 10�� 11��)
};

struct tagLunarCal
{
 char BaseDays;   //���� 1 �� 1 �յ�ũ�����³�һ���ۻ�����
 char Intercalation;  //�����·�. 0==����û������
 char BaseWeekday;  //�������� 1 �� 1 ��Ϊ���ڼ��ټ� 1
 char BaseKanChih;  //�������� 1 �� 1 �յ���֧��ż� 1
 char MonthDays[13];  //��ũ����ÿ��֮��С, 0==С��(29��), 1==����(30��)
};

struct tagLunarCal LunarCal[] = {
{ 23,  3, 2, 17, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0 },  /* 1936 */
{ 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 },  /* 1940 */
{ 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,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{ 49,  0, 3, 12, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1 },
{ 38,  0, 4, 17, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0 },  /* 2016 */
{ 27,  6, 6, 23, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1 },
{ 46,  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 },  /* 2020 */
{ 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 },  /* 2024 */
{ 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 },  /* 2028 */
{ 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; // The first year in LunarCal[]
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 main(int argc, char*argv[])
{
 struct ConvDate stConvDate;
 printf("0. ����ת��Ϊ����\n1. ����ת��Ϊ����\n");
 scanf("%d",&stConvDate.Source);
 if (stConvDate.Source == 0)
 {
  printf("������ݣ�");
  scanf("%d",&stConvDate.SolarYear);
  printf("\n�����·ݣ�");
  scanf("%d",&stConvDate.SolarMonth);
  printf("\n�������ڣ�");
  scanf("%d",&stConvDate.SolarDate);
 }
 else
 {
  printf("������ݣ�");
  scanf("%d",&stConvDate.LunarYear);
  printf("\n�����·ݣ�");
  scanf("%d",&stConvDate.LunarMonth);
  printf("\n�������ڣ�");
  scanf("%d",&stConvDate.LunarDate);
 }

 switch(CalConv(&stConvDate))
 {
  case INPUT_YEAR_ERROR:
   printf("INPUT_YEAR_ERROR\n");
   break;
  case INPUT_MONTH_ERROR:
   printf("INPUT_MONTH_ERROR\n");
   break;
  case INPUT_DAY_ERROR:
   printf("INPUT_DAY_ERROR\n");
   break;
  default:
   printf("YYMMDD: %d %d %d Week:%d Kan:%d Chih:%d\n",
   stConvDate.LunarYear,stConvDate.LunarMonth,stConvDate.LunarDate,
   stConvDate.Weekday,stConvDate.Kan,stConvDate.Chih);
 }

 return 0;
}*/

// ����������Ƿ�Ϊ����, ���� 0 Ϊƽ��, 1 Ϊ����
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 )  // ==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 ); //�ж��Ƿ����� ����Ϊ1 ƽ��Ϊ0
  
  if ( nSolarMonth == 1 ) // ����ƽ��������� 2������
  {
   nMonthDay = nIsLeap + 28; // ������ 2 �¼�һ��
  }
  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 );
}