原创 将公历转为农历的算法

2008-4-21 22:11 4212 11 11 分类: MCU/ 嵌入式

static const unsigned short wMonthAdd[13]={0,31,59,90,120,151,181,212,243,273,304,334,365};
static const unsigned short wMonthAddy[13]={0,31,60,91,121,152,182,213,244,274,305,335,366};


static const unsigned long wNongliData[99]={/*农历数据*/
0x41A95,0x00D4A,0x00DA5,0x20B55,0x0056A,0x7155B,0x0025D,0x0092D,0x5192B,0x00A95,
0x00B4A,0x416AA,0x00AD5,0x90AB5,0x004BA,0x00A5B,0x60A57,0x0052B,0x00A93,0x40E95,
0x006AA,0x00AD5,0x209B5,0x004B6,0x612AE,0x00A4E,0x00D26,0x51D26,0x00D53,0x005AA,
0x30D6A,0x0096D,0xB095D,0x004AD,0x00A4D,0x61A4B,0x00D25,0x00D52,0x51B54,0x00B5A,
0x0056D,0x2095B,0x0049B,0x71497,0x00A4B,0x00AA5,0x516A5,0x006D2,0x00ADA,0x30AB6,
0x00937,0x8092F,0x00497,0x0064B,0x60D4A,0x00EA5,0x006B2,0x4156C,0x00AAE,0x0092E,
0x3192E,0x00C96,0x71A95,0x00D4A,0x00DA5,0x50B55,0x0056A,0x00A6D,0x40A5D,0x0052D,
0x8152B,0x00A95,0x00B4A,0x616AA,0x00AD5,0x0055A,0x414BA,0x00A5B,0x0052B,0x31527,
0x00693,0x70E53,0x006AA,0x00AD5,0x509B5,0x004B6,0x00A57,0x40A4E,0x00D26,0x81D26,
0x00D52,0x00DAA,0x60D6A,0x0056D,0x004AE,0x4149D,0x00A4D,0x00D15,0x21B25};


typedef struct
{
    int     day;
    int     month;
    int     year;
} CLK_DATE_T;


CLK_DATE_T GetOldDay(CLK_DATE_T pSt)                   /*公历转换成农历*/
{
static int wCurYear,wCurMonth,wCurDay;
static int nIsEnd,m,k,n,i,nBit;
static long nTheDate;
CLK_DATE_T OldDay;
int y;
wCurYear = pSt.year;
wCurMonth = pSt.month;
wCurDay   = pSt.day;
y=wCurYear - 2001;


/*计算该天到2001年正月初一有多少天*/
nTheDate =(y<<8)+(y<<6)+(y<<5)+(y<<3)+(y<<2)+y + (y>>2) + wCurDay + wMonthAdd[wCurMonth - 1] - 23;
if((!(wCurYear & 0x0003)) && (wCurMonth > 2))
nTheDate++;
nIsEnd = 0;
m      = 0;
while(nIsEnd != 1){
     if(wNongliData[m] < 4095) k = 11;
   else k = 12;
    n = k;
     while(n>=0){
           nBit = wNongliData[m];
           for(i=1;i<n+1;i++) nBit = (nBit>>1);
           nBit = (nBit & 0x0001);
           if (nTheDate <= (29 + nBit))
     {
          nIsEnd = 1;
          break;
        }
           nTheDate = nTheDate - 29 - nBit;
          n--;
          }
     if(nIsEnd) break;
     m++;
      }
wCurYear = 2001 + m;
wCurMonth = k - n + 1;
wCurDay   = nTheDate;


y=wNongliData[m] / 65536 + 1;                   /*计算该年闰月的月份+1*/


if (k == 12){


if (wCurMonth == y)
      wCurMonth = 1 - wCurMonth;
else if (wCurMonth > y)
      wCurMonth = wCurMonth - 1;
}
OldDay.year =wCurYear;
OldDay.month=wCurMonth;
OldDay.day =wCurDay;
return OldDay;

PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
EE直播间
更多
我要评论
0
11
关闭 站长推荐上一条 /3 下一条