转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> Delphi程序 >> 正文
中国农历算法(delphi)         ★★★★

中国农历算法(delphi)

作者:闵涛 文章来源:闵涛的学习笔记 点击数:1569 更新时间:2009/4/23 18:30:49

// 节日算法 请参见 《农历与西历对照、万年历》
unit CNYear;

interface

uses sysutils;

type TCNDate = Cardinal;

function DecodeGregToCNDate(dtGreg:TDateTime):TCNDate;
function GetGregDateFromCN(cnYear,cnMonth,cnDay:word;bLeap:Boolean=False):TDateTime;
function GregDateToCNStr(dtGreg:TDateTime):String;
function isCNLeap(cnDate:TCNDate):boolean;

implementation

const cstDateOrg:Integer=32900; //公历1990-01-27的TDateTime表示 对应农历1990-01-01
const cstCNYearOrg=1990;
const cstCNTable:array[cstCNYearOrg..cstCNYearOrg + 60] of WORD=(       //   unsigned 16-bit
24402, 3730, 3366, 13614, 2647, 35542, 858, 1749,           //1997
23401, 1865, 1683, 19099, 1323, 2651, 10926, 1386,           //2005
32213, 2980, 2889, 23891, 2709, 1325, 17757, 2741,           //2013
39850, 1490, 3493, 61098, 3402, 3221, 19102, 1366,           //2021
2773, 10970, 1746, 26469, 1829, 1611, 22103, 3243,           //2029
1370, 13678, 2902, 48978, 2898, 2853, 60715, 2635,           //2037
1195, 21179, 1453, 2922, 11690, 3474, 32421, 3365,           //2045
2645, 55901, 1206, 1461, 14038);                             //2050
//建表方法:
// 0101 111101010010     高四位是闰月位置,后12位表示大小月,大月30天,小月29天,
//闰月一般算小月,但是有三个特例2017/06,2036/06,2047/05
//对于特例则高四位的闰月位置表示法中的最高为设置为1 特殊处理用wLeapNormal变量
// //2017/06 28330->61098 2036/06 27947->60715 2047/05 23133->55901

//如果希望用汇编,这里有一条信息:农历不会滞后公历2个月.
//将公历转换为农历
//返回:12位年份+4位月份+5位日期
function DecodeGregToCNDate(dtGreg:TDateTime):TCNDate;
var
  iDayLeave:Integer;
  wYear,wMonth,wDay:WORD;
  i,j:integer;
  wBigSmallDist,wLeap,wCount,wLeapShift:WORD;
label OK;
begin
  result := 0;
  iDayLeave := Trunc(dtGreg) - cstDateOrg;
  DecodeDate(IncMonth(dtGreg,-1),wYear,wMonth,wDay);
  if (iDayLeave < 0) or (iDayLeave > 22295 )then Exit;
              //Raise Exception.Create(''''目前只能算1990-01-27以后的'''');
              //Raise Exception.Create(''''目前只能算2051-02-11以前的'''');
  for i:=Low(cstCNTable)   to High(cstCNTable) do begin
      wBigSmallDist := cstCNTable[i];
      wLeap := wBigSmallDist shr 12;
      if wLeap > 12 then begin
          wLeap := wLeap and 7;
          wLeapShift := 1;
      end else
          wLeapShift := 0;
      for j:=1 to 12 do begin
          wCount:=(wBigSmallDist and 1) + 29;
          if j=wLeap then wCount := wCount - wLeapShift;
          if iDayLeave < wCount   then begin
              Result := (i shl 9) + (j shl 5) + iDayLeave + 1;
              Exit;
          end;
          iDayLeave := iDayLeave - wCount;
          if j=wLeap then begin
              wCount:=29 + wLeapShift;
              if iDayLeave < wCount   then begin
                  Result := (i shl 9) + (j shl 5) + iDayLeave + 1 + (1 shl 21);
                  Exit;
              end;
              iDayLeave := iDayLeave - wCount;
          end;
          wBigSmallDist := wBigSmallDist shr 1;
      end;
  end;
//返回值:
// 1位闰月标志 + 12位年份+4位月份+5位日期             (共22位)
end;

function isCNLeap(cnDate:TCNDate):boolean;
begin
  result := (cnDate and $200000) <> 0;
end;

function GetGregDateFromCN(cnYear,cnMonth,cnDay:word;bLeap:Boolean=False):TDateTime;
var
  i,j:integer;
  DayCount:integer;
  wBigSmallDist,wLeap,wLeapShift:WORD;
begin
  // 0101 010010101111     高四位是闰月位置,后12位表示大小月,大月30天,小月29天,
  DayCount := 0;
  if (cnYear < 1990) or (cnYear >2050) then begin
      Result := 0;
      Exit;
  end;
  for i:= cstCNYearOrg to cnYear-1 do begin
        wBigSmallDist := cstCNTable[i];
        if (wBIgSmallDist and $F000) <> 0 then DayCount := DayCount + 29;
        DayCount := DayCount + 12 * 29;
        for j:= 1 to 12 do begin
            DayCount := DayCount + wBigSmallDist and 1;
            wBigSmallDist := wBigSmallDist shr 1;
        end;
  end;
  wBigSmallDist := cstCNTable[cnYear];
  wLeap := wBigSmallDist shr 12;
  if wLeap > 12 then begin
      wLeap := wLeap and 7;
      wLeapShift := 1;                       //大月在闰月.
  end else
      wLeapShift := 0;
  for j:= 1 to cnMonth-1 do begin
      DayCount:=DayCount + (wBigSmallDist and 1) + 29;
      if j=wLeap then DayCount := DayCount + 29;
      wBigSmallDist := wBigSmallDist shr 1;
  end;
  if bLeap and (cnMonth = wLeap)   then   //是要闰月的吗?
      DayCount := DayCount + 30 - wLeapShift;
  result := cstDateOrg + DayCount + cnDay - 1;
end;

//将日期显示成农历字符串.
function GregDateToCNStr(dtGreg:TDateTime):String;
const hzNumber:array[0..10] of string=(''''零'''',''''一'''',''''二'''',''''三'''',''''四'''',''''五'''',''''六'''',''''七'''',''''八'''',''''九'''',''''十'''');
function ConvertYMD(Number:Word;YMD:Word):string;
var
  wTmp:word;
begin
  result := '''''''';
  if YMD = 1 then begin //年份
      while Number > 0 do begin
          result := hzNumber[Number Mod 10] + result;
          Number := Number DIV 10;
      end;
      Exit;
  end;
  if Number<=10 then begin   //可只用1位
      if YMD = 2 then //月份
          result := hzNumber[Number]
      else                       //天
          result := ''''初'''' + hzNumber[Number];
      Exit;
  end;
  wTmp := Number Mod 10;   //个位
  if wTmp <> 0 then result := hzNumber[wTmp];
  wTmp := Number Div 10;           //十位
  result:=''''十''''+result;
  if wTmp > 1 then   result := hzNumber[wTmp] + result;
end;
var
  cnYear,cnMonth,cnDay:word;
  cnDate:TCNDate;
  strLeap:string;
begin
  cnDate:= DecodeGregToCNDate(dtGreg);
  if cnDate = 0 then begin
      result := ''''输入越界'''';
      Exit;                
  end;
  cnDay := cnDate and $1F;
  cnMonth := (cnDate shr 5) and $F;
  cnYear := (cnDate shr 9) and $FFF;
  //测试第22位,为1表示闰月
  if i

[1] [2]  下一页


没有相关教程
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台