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

2008-4-21 22:11 3401 0 分类: 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;

广告

文章评论 0条评论)

登录后参与讨论
相关推荐阅读
qingsha1314 2009-03-23 22:46
对某网友的不理解解释一下(12864坐标问题)
你可以看一下12864的资料,因为在每次写入地址并写好数据后,地址是自动加一的,也就是X坐标每次自动加一,所以,大循环里每次改变Y坐标,X坐标是在小循环里自动加的。因此只要每次把X的初始坐标一给即可。...
qingsha1314 2008-10-26 21:01
找工作好难,而某些公司的做法更让人可气
        今天又像以往跑出去找工作了,到了一个比较牛的大学去了,经过一个多小时的宣讲会,开始投简历,然后完了是等两个小时看通知有没有笔试机会,在操场上闲聊了两个小时 ,然后跑去看结果,我靠,居然...
qingsha1314 2008-10-09 23:55
AT24C04调试成功
经过一天的努力,终于把AT24C02 的驱动程序给搞定了。真是不容易,这个东西不是很好弄,而且时序的把握要非常好,稍有差迟,就不行了,现在把原程序传上来。89S52驱动+LCD1602显示。解压密码:...
qingsha1314 2008-09-25 11:49
SPCE061A的12864驱动程序
#include "spce061a.h"const unsigned char bmp3[]={0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,      0x05,...
qingsha1314 2008-09-16 14:44
VHDL写的鼠标驱动器+VGA显示
这是我们假期参加ALTERA电子竞赛时用的鼠标驱动器的VHDL代码.已经过测试成功.以下是源代码.无需修改就可以使用.library IEEE;use IEEE.STD_LOGIC_1164.ALL;...
qingsha1314 2008-07-11 11:28
大三的生活结束了
今天是考完试的最后一天,这也就意味着我的大三生活结束了.回顾这一学期的所做所为,感觉没有虚度,先后制作了好多小电子玩艺,也学到了一些东西.马上就要开始ALTERA的SOPC竞赛了,打算好好准备,争取拿...
广告
我要评论
0
0
广告
关闭 热点推荐上一条 /2 下一条