用Delphi处理公历到农历的转换
(文档类别:Delphi) 2001-9-24
摘 要:公历到农历的转换
关键字:日历,农历,转换
类 别:Object Pascal
用Delphi处理公历到农历的转换
(文档类别:Delphi) 2001-9-24
摘 要:公历到农历的转换
关键字:日历,农历,转换
类 别:Object Pascal
unit uCalfunc;
interface
uses SysUtils,Windows;
const
START_YEAR=1901;
END_YEAR=2050;
//返回iYear年iMonth月的天数 1年1月 --- 65535年12月
function MonthDays(iYear,iMonth:Word):Word;
//返回阴历iLunarYer年阴历iLunarMonth月的天数,如果iLunarMonth为闰月,
//高字为第二个iLunarMonth月的天数,否则高字为01901年1月---2050年12月
function LunarMonthDays(iLunarYear,iLunarMonth:Word):Longword;
//返回阴历iLunarYear年的总天数 1901年1月---2050年12月
function LunarYearDays(iLunarYear:Word):Word;
//返回阴历iLunarYear年的闰月月份,如没有返回01901年1月---2050年12月
function GetLeapMonth(iLunarYear:Word):Word;
//把iYear年格式化成天干记年法表示的字符串
procedure FormatLunarYear(iYear:Word;var pBuffer:string);overload;
function FormatLunarYear(iYear:Word):string;overload;
//把iMonth格式化成中文字符串
procedure FormatMonth(iMonth:Word;var pBuffer:string;bLunar:Boolean=True);overload;
function FormatMonth(iMonth:Word;bLunar:Boolean=True):string;overload;
//把iDay格式化成中文字符串
procedure FormatLunarDay(iDay:Word;var pBuffer:string);overload;
function FormatLunarDay(iDay:Word):string;overload;
//计算公历两个日期间相差的天数1年1月1日 --- 65535年12月31日
function CalcDateDiff(iEndYear,iEndMonth,iEndDay:Word;iStartYear:Word=START_YEAR;iStartMonth:Word=1;iStartDay:Word=1):Longword;overload;
function CalcDateDiff(EndDate,StartDate:TDateTime):Longword;overload;
//计算公历iYear年iMonth月iDay日对应的阴历日期,返回对应的阴历节气 0-24
//1901年1月1日---2050年12月31日
function GetLunarHolDay(InDate:TDateTime):string;overload;
function GetLunarHolDay(iYear,iMonth,iDay:Word):string;overload;
//private function--------------------------------------
//计算从1901年1月1日过iSpanDays天后的阴历日期
procedure l_CalcLunarDate(var iYear,iMonth,iDay:Word;iSpanDays:Longword);
//计算公历iYear年iMonth月iDay日对应的节气 0-24,0表不是节气
function l_GetLunarHolDay(iYear,iMonth,iDay:Word):Word;
implementation
var
//数组gLunarDay存入阴历1901年到2100年每年中的月天数信息,
//阴历每月只能是29或30天,一年用12(或13)个二进制位表示,对应位为1表30天,否则为29天
gLunarMonthDay:array[0..149] of Word=(
//测试数据只有1901.1.1 --2050.12.31
ae0, $a570, 68, $d260, $d950, aa8, a0, ad0, ae8, ae0, //1910
$a4d8, $a4d0, $d250, $d548, $b550, a0, d0, b0, b8, b0, //1920
$a4b0, $b258, a50, d40, $ada8, b60, 70, 78, 70, b0, //1930
$d4a0, $ea50, d48, ad0, b60, 70, e0, $c968, $c950, $d4a0, //1940
$da50, $b550, a0, $aad8, d0, d0, $c958, $a950, $b4a8, ca0, //1950
$b550, a8, da0, $a5b0, b8, b0, $a950, $e950, aa0, $ad50, //1960
$ab50, b60, $a570, $a570, 60, $e930, $d950, aa8, a0, d0, //1970
ae8, ad0, $a4d0, $d268, $d250, $d528, $b540, $b6a0, d0, b0, //1980
b0, $a4b8, $a4b0, $b258, a50, d40, $ada0, $ab60, 70, 78, //1990
70, b0, a50, $ea50, b28, ac0, $ab60, 68, e0, $c960, //2000
$d4a8, $d4a0, $da50, aa8, a0, $aad8, d0, d0, $c958, $a950, //2010
$b4a0, $b550, $b550, a8, ba0, $a5b0, b8, b0, $a930, a8, //2020
aa0, $ad50, da8, b60, 70, $a4e0, $d260, $e930, $d530, aa0, //2030
b50, d0, ae8, ad0, $a4d0, $d258, $d250, $d520, $daa0, $b5a0, //2040
d0, ad8, b0, $a4b8, $a4b0, $aa50, $b528, d20, $ada0, b0); //2050
//数组gLanarMonth存放阴历1901年到2050年闰月的月份,如没有则为0,每字节存两年
gLunarMonth:array[0..74] of Byte=(
, , , , ,//1910
, , , , ,//1920
, , , , ,//1930
, , , , ,//1940
, , , , ,//1950
, , , , ,//1960
, , , , ,//1970
, , , , ,//1980
, a, , , ,//1990
, , , , ,//2000
, , , , ,//2010
, , , , ,//2020
, , , , ,//2030
, $b0, , , ,//2040
, , , , );//2050
//数组gLanarHoliDay存放每年的二十四节气对应的阳历日期
//每年的二十四节气对应的阳历日期几乎固定,平均分布于十二个月中
// 1月2月 3月 4月 5月 6月
//小寒 大寒 立春雨水 惊蛰 春分 清明 谷雨 立夏 小满 芒种 夏至
// 7月8月 9月 10月 11月12月
//小暑 大暑 立秋处暑 白露 秋分 寒露 霜降 立冬 小雪 大雪 冬至
{*********************************************************************************
节气无任何确定规律,所以只好存表,要节省空间,所以....
**********************************************************************************}
//数据格式说明:
//如1901年的节气为
//1月 2月 3月 4月5月 6月 7月8月 9月10月11月 12月
// 6, 21, 4, 19,6, 21, 5, 21, 6,22, 6,22, 8, 23, 8, 24, 8, 24, 8, 24, 8, 23, 8, 22
// 9, 6,11,4, 9, 6,10,6,9,7,9,7,7, 8,7, 9,7,9, 7,9, 7,8, 7, 15
//上面第一行数据为每月节气对应日期,15减去每月第一个节气,每月第二个节气减去15得第二行
// 这样每月两个节气对应数据都小于16,每月用一个字节存放,高位存放第一个节气数据,低位存放
//第二个节气的数据,可得下表
gLunarHolDay:array[0..1799] of Byte=(
, $B4, , $A6, , , , , , , , ,//1901
, $A4, , , , , , , , , , ,//1902
, $A5, , , , , , , , , , ,//1903
, $A5, , $A5, , , , , , , , ,//1904
, $B4, , $A6, , , , , , , , ,//1905
, $A4, , , , , , , , , , ,//1906
, $A5, , , , , , , , , , ,//1907
, $A5, , $A5, , , , , , , , ,//1908
, $B4, , $A6, , , , , , , , ,//1909
, $A4, , , , , , , , , , ,//1910
, $A5, , , , , , , , , , ,//1911
, $A5, , $A5, , , , , , , , ,//1912
, $B4, , $A6, , , , , , , , ,//1913
, $B4, , $A6, , , , , , , , ,//1914
, $A5, , , , , , , , , , ,//1915
, $A5, , $A5, , , , , , , , ,//1916
, $B4, , $A6, , , , , , , , ,//1917
, $B4, , $A6, , , , , , , , ,//1918
, $A5, , , , , , , , , , ,//1919
, $A5, , $A5, , , , , , , , ,//1920
, $B4, , $A5, , , , , , , , ,//1921
, $B4, , $A6, , , , , , , , ,//1922
, $A4, , , , , , , , , , ,//1923
, $A5, , $A5, , , , , , , , ,//1924
, $B4, , $A5, , , , , , , , ,//1925
, $B4, , $A6, , , , , , , , ,//1926
, $A4, , , , , , , , , , ,//1927
, $A5, , $A5, , , , , , , , ,//1928
, $B4, , $A5, , , , , , , , ,//1929
, $B4, , $A6, , , , , , , , ,//1930
, $A4, , , , , , , , , , ,//1931
, $A5, , $A5, , , , , , , , ,//1932
, $B4, , $A5, , , , , , , , ,//1933
, $B4, , $A6, , , , , , , , ,//1934
, $A4, , , , , , , , , , ,//1935
, $A5, , $A5, , , , , , , , ,//1936
, $B4, , $A5, , , , , , , , ,//1937
, $B4, , $A6, , , , , , , , ,//1938
, $A4, , , , , , , , , , ,//1939
, $A5, , $A5, , , , , , , , ,//1940
, $B4, , $A5, , , , , , , , ,//1941
, $B4, , $A6, , , , , , , , ,//1942
, $A4, , , , , , , , , , ,//1943
, $A5, , $A5, $A6, , , , , , , ,//1944
, $B4, , $A5, , , , , , , , ,//1945
, $B4, , $A6, , , , , , , , ,//1946
, $B4, , $A6, , , , , , , , , //1947
, $A5, $A6, $A5, $A6, , , , , , , ,//1948
$A5, $B4, , $A5, , , , , , , , ,//1949
, $B4, , $A5, , , , , , , , ,//1950
, $B4, , $A6, , , , , , , , ,//1951
, $A5, $A6, $A5, $A6, , , , , , , ,//1952
$A5, $B4, , $A5, , , , , , , , ,//1953
, $B4, , $A5, , , , , , , , ,//1954
, $B4, , $A6, , , , , , , , ,//1955
, $A5, $A5, $A5, $A6, , , , , , , ,//1956
$A5, $B4, , $A5, , , , , , , , ,//1957
, $B4, , $A5, , , , , , , , ,//1958
, $B4, , $A6, , , , , , , , ,//1959
, $A4, $A5, $A5, $A6, , , , , , , ,//1960
$A5, $B4, , $A5, , , , , , , , ,//1961
, $B4, , $A5, , , , , , , , ,//1962
, $B4, , $A6, , , , , , , , ,//1963
, $A4, $A5, $A5, $A6, , , , , , , ,//1964
$A5, $B4, , $A5, , , , , , , , ,//1965
, $B4, , $A5, , , , , , , , ,//1966
, $B4, , $A6, , , , , , , , ,//1967
, $A4, $A5, $A5, $A6, $A6, , , , , , ,//1968
$A5, $B4, , $A5, , , , , , , , ,//1969
, $B4, , $A5, , , , , , , , ,//1970
, $B4, , $A6, , , , , , , , ,//1971
, $A4, $A5, $A5, $A6, $A6, , , , , , ,//1972
$A5, $B5, , $A5, $A6, , , , , , , ,//1973
, $B4, , $A5, , , , , , , , ,//1974
, $B4, , $A6, , , , , , , , ,//1975
, $A4, $A5, $B5, $A6, $A6, , , , , , ,//1976
$A5, $B4, , $A5, , , , , , , , ,//1977
, $B4, , $A5, , , , , , , , ,//1978
, $B4, , $A6, , , , , , , , ,//1979
, $A4, $A5, $B5, $A6, $A6, , , , , , ,//1980
$A5, $B4, , $A5, $A6, , , , , , , ,//1981
, $B4, , $A5, , , , , , , , ,//1982
, $B4, , $A5, , , , , , , , ,//1983
, $B4, $A5, $B5, $A6, $A6, , , , , , ,//1984
$A5, $B4, $A6, $A5, $A6, , , , , , , ,//1985
$A5, $B4, , $A5, , , , , , , , ,//1986
, $B4, , $A5, , , , , , , , ,//1987
, $B4, $A5, $B5, $A6, $A6, , , , , , ,//1988
$A5, $B4, $A5, $A5, $A6, , , , , , , ,//1989
$A5, $B4, , $A5, , , , , , , , ,//1990
, $B4, , $A5, , , , , , , , ,//1991
, $B4, $A5, $B5, $A6, $A6, , , , , , ,//1992
$A5, $B3, $A5, $A5, $A6, , , , , , , ,//1993
$A5, $B4, , $A5, , , , , , , , ,//1994
, $B4, , $A5, , , , , , , , ,//1995
, $B4, $A5, $B5, $A6, $A6, , , , , , ,//1996
$A5, $B3, $A5, $A5, $A6, $A6, , , , , , ,//1997
$A5, $B4, , $A5, , , , , , , , ,//1998
, $B4, , $A5, , , , , , , , ,//1999
, $B4, $A5, $B5, $A6, $A6, , , , , , ,//2000
$A5, $B3, $A5, $A5, $A6, $A6, , , , , , ,//2001
$A5, $B4, , $A5, , , , , , , , ,//2002
, $B4, , $A5, , , , , , , , ,//2003
, $B4, $A5, $B5, $A6, $A6, , , , , , ,//2004
$A5, $B3, $A5, $A5, $A6, $A6, , , , , , ,//2005
$A5, $B4, , $A5, $A6, , , , , , , ,//2006
, $B4, , $A5, , , , , , , , ,//2007
, $B4, $A5, $B5, $A6, $A6, , , , , , ,//2008
$A5, $B3, $A5, $B5, $A6, $A6, , , , , , ,//2009
$A5, $B4, , $A5, $A6, , , , , , , ,//2010
, $B4, , $A5, , , , , , , , ,//2011
, $B4, $A5, $B5, $A5, $A6, , , , , , ,//2012
$A5, $B3, $A5, $B5, $A6, $A6, , , , , , ,//2013
$A5, $B4, , $A5, $A6, , , , , , , ,//2014
, $B4, , $A5, , , , , , , , ,//2015
, $B4, $A5, $B4, $A5, $A6, , , , , , ,//2016
$A5, $C3, $A5, $B5, $A6, $A6, , , , , , ,//2017
$A5, $B4, $A6, $A5, $A6, , , , , , , ,//2018
$A5, $B4, , $A5, , , , , , , , ,//2019
, $B4, $A5, $B4, $A5, $A6, , , , , , ,//2020
$A5, $C3, $A5, $B5, $A6, $A6, , , , , , ,//2021
$A5, $B4, $A5, $A5, $A6, , , , , , , ,//2022
$A5, $B4, , $A5, , , , , , , , ,//2023
, $B4, $A5, $B4, $A5, $A6, , , , , , ,//2024
$A5, $C3, $A5, $B5, $A6, $A6, , , , , , ,//2025
$A5, $B3, $A5, $A5, $A6, $A6, , , , , , ,//2026
$A5, $B4, , $A5, , , , , , , , ,//2027
, $B4, $A5, $B4, $A5, $A6, , , , , , ,//2028
$A5, $C3, $A5, $B5, $A6, $A6, , , , , , ,//2029
$A5, $B3, $A5, $A5, $A6, $A6, , , , , , ,//2030
$A5, $B4, , $A5, , , , , , , , ,//2031
, $B4, $A5, $B4, $A5, $A6, , , , , , ,//2032
$A5, $C3, $A5, $B5, $A6, $A6, , , , , , ,//2033
$A5, $B3, $A5, $A5, $A6, $A6, , , , , , ,//2034
$A5, $B4, , $A5, $A6, , , , , , , ,//2035
, $B4, $A5, $B4, $A5, $A6, , , , , , ,//2036
$A5, $C3, $A5, $B5, $A6, $A6, , , , , , ,//2037
$A5, $B3, $A5, $A5, $A6, $A6, , , , , , ,//2038
$A5, $B4, , $A5, $A6, , , , , , , ,//2039
, $B4, $A5, $B4, $A5, $A6, , , , , , ,//2040
$A5, $C3, $A5, $B5, $A5, $A6, , , , , , ,//2041
$A5, $B3, $A5, $B5, $A6, $A6, , , , , , ,//2042
$A5, $B4, , $A5, $A6, , , , , , , ,//2043
, $B4, $A5, $B4, $A5, $A6, , , , , , ,//2044
$A5, $C3, $A5, $B4, $A5, $A6, , , , , , ,//2045
$A5, $B3, $A5, $B5, $A6, $A6, , , , , , ,//2046
$A5, $B4, , $A5, $A6, , , , , , , ,//2047
, $B4, $A5, $B4, $A5, $A5, , , , , , ,//2048
$A4, $C3, $A5, $A5, $A5, $A6, , , , , , ,//2049
$A5, $C3, $A5, $B5, $A6, $A6, , , , , , );//2050
function MonthDays(iYear,iMonth:Word):Word;
begin
case iMonth of
1,3,5,7,8,10,12: Result:=31;
4,6,9,11: Result:=30;
2://如果是闰年
if IsLeapYear(iYear) then
Result:=29
else
Result:=28
else
Result:=0;
end;
end;
function GetLeapMonth(iLunarYear:Word):Word;
var
Flag:Byte;
begin
Flag:=gLunarMonth[(iLunarYear-START_YEAR) div 2];
if (iLunarYear-START_YEAR) mod 2=0 then
Result:=Flag shr 4
else
Result:=Flag and F;
end;
function LunarMonthDays(iLunarYear,iLunarMonth:Word):Longword;
var
Height,Low:Word;
iBit:Integer;
begin
if iLunarYear<START_YEAR then
begin
Result:=30;
Exit;
end;
Height:=0;
Low:=29;
iBit:=16-iLunarMonth;
if (iLunarMonth>GetLeapMonth(iLunarYear)) and (GetLeapMonth(iLunarYear)>0) then
Dec(iBit);
if (gLunarMonthDay[iLunarYear-START_YEAR] and (1 shl iBit))>0 then
Inc(Low);
if iLunarMonth=GetLeapMonth(iLunarYear) then
if (gLunarMonthDay[iLunarYear-START_YEAR] and (1 shl (iBit-1)))>0 then
Height:=30
else
Height:=29;
Result:=MakeLong(Low,Height);
end;
function LunarYearDays(iLunarYear:Word):Word;
var
Days,i:Word;
tmp:Longword;
begin
Days:=0;
for i:=1 to 12 do
begin
tmp:=LunarMonthDays(iLunarYear,i);
Days:=Days+HiWord(tmp);
Days:=Days+LoWord(tmp);
end;
Result:=Days;
end;
procedure FormatLunarYear(iYear:Word;var pBuffer:string);
var
szText1,szText2,szText3:string;
begin
szText1:='甲乙丙丁戊己庚辛壬癸';
szText2:='子丑寅卯辰巳午未申酉戌亥';
szText3:='鼠牛虎免龙蛇马羊猴鸡狗猪';
pBuffer:=Copy(szText1,((iYear-4) mod 10)*2+1,2);
pBuffer:=pBuffer+Copy(szText2,((iYear-4) mod 12)*2+1,2);
pBuffer:=pBuffer+' ';
pBuffer:=pBuffer+Copy(szText3,((iYear-4) mod 12)*2+1,2);
pBuffer:=pBuffer+'年';
end;
function FormatLunarYear(iYear:Word):string;
var
pBuffer:string;
begin
FormatLunarYear(iYear,pBuffer);
Result:=pBuffer;
end;
procedure FormatMonth(iMonth:Word;var pBuffer:string;bLunar:Boolean);
var
szText:string;
begin
if (not bLunar) and (iMonth=1) then
begin
pBuffer:='一月';
Exit;
end;
szText:='正二三四五六七八九十';
if iMonth<=10 then
begin
pBuffer:='';
pBuffer:=pBuffer+Copy(szText,(iMonth-1)*2+1,2);
pBuffer:=pBuffer+'月';
Exit;
end;
if iMonth=11 then
pBuffer:='十一'
else
pBuffer:='十二';
pBuffer:=pBuffer+'月';
end;
function FormatMonth(iMonth:Word;bLunar:Boolean):string;
var
pBuffer:string;
begin
FormatMonth(iMonth,pBuffer,bLunar);
Result:=pBuffer;
end;
procedure FormatLunarDay(iDay:Word;var pBuffer:string);
var
szText1,szText2:string;
begin
szText1:='初十廿三';
szText2:='一二三四五六七八九十';
if (iDay<>20) and (iDay<>30) then
begin
pBuffer:=Copy(szText1,((iDay-1) div 10)*2+1,2);
pBuffer:=pBuffer+Copy(szText2,((iDay-1) mod 10)*2+1,2);
end
else
begin
pBuffer:=Copy(szText1,(iDay div 10)*2+1,2);
pBuffer:=pBuffer+'十';
end;
end;
function FormatLunarDay(iDay:Word):string;
var
pBuffer:string;
begin
FormatLunarDay(iDay,pBuffer);
Result:=pBuffer;
end;
function CalcDateDiff(iEndYear,iEndMonth,iEndDay:Word;iStartYear:Word;iStartMonth:Word;iStartDay:Word):Longword;
begin
Result:=Trunc(EncodeDate(iEndYear,iEndMonth,iEndDay)-EncodeDate(iStartYear,iStartMonth,iStartDay));
end;
function CalcDateDiff(EndDate,StartDate:TDateTime):Longword;
begin
Result:=Trunc(EndDate-StartDate);
end;
procedure l_CalcLunarDate(var iYear,iMonth,iDay:Word;iSpanDays:Longword);
var
tmp:Longword;
begin
//阳历1901年2月19日为阴历1901年正月初一
//阳历1901年1月1日到2月19日共有49天
if iSpanDays<49 then
begin
iYear:=START_YEAR-1;
if iSpanDays<19 then
begin
iMonth:=11;
iDay:=11+Word(iSpanDays);
end
else
begin
iMonth:=12;
iDay:=Word(iSpanDays)-18;
end;
Exit;
end;
//下面从阴历1901年正月初一算起
iSpanDays:=iSpanDays-49;
iYear:=START_YEAR;
iMonth:=1;
iDay:=1;
//计算年
tmp:=LunarYearDays(iYear);
while iSpanDays>=tmp do
begin
iSpanDays:=iSpanDays-tmp;
Inc(iYear);
tmp:=LunarYearDays(iYear);
end;
//计算月
tmp:=LoWord(LunarMonthDays(iYear,iMonth));
while iSpanDays>=tmp do
begin
iSpanDays:=iSpanDays-tmp;
if iMonth=GetLeapMonth(iYear) then
begin
tmp:=HiWord(LunarMonthDays(iYear,iMonth));
if iSpanDays<tmp then Break;
iSpanDays:=iSpanDays-tmp;
end;
Inc(iMonth);
tmp:=LoWord(LunarMonthDays(iYear,iMonth));
end;
//计算日
iDay:=iDay+Word(iSpanDays);
end;
function l_GetLunarHolDay(iYear,iMonth,iDay:Word):Word;
var
Flag:Byte;
Day:Word;
begin
Flag:=gLunarHolDay[(iYear-START_YEAR)*12+iMonth-1];
if iDay<15 then
Day:=15-((Flag shr 4) and f)
else
Day:=(Flag and f)+15;
if iDay=Day then
if iDay>15 then
Result:=(iMonth-1)*2+2
else
Result:=(iMonth-1)*2+1
else
Result:= 0;
end;
function GetLunarHolDay(InDate:TDateTime):string;
var
i,iYear,iMonth,iDay:Word;
begin
DecodeDate(InDate,iYear,iMonth,iDay);
i:=l_GetLunarHolDay(iYear,iMonth,iDay);
case i of
1:Result:='小 寒';
2:Result:='大 寒';
3:Result:='立 春';
4:Result:='雨 水';
5:Result:='惊 蛰';
6:Result:='春 分';
7:Result:='清 明';
8:Result:='谷 雨';
9:Result:='立 夏';
10:Result:='小 满';
11:Result:='芒 种';
12:Result:='夏 至';
13:Result:='小 暑';
14:Result:='大 暑';
15:Result:='立 秋';
16:Result:='处 暑';
17:Result:='白 露';
18:Result:='秋 分';
19:Result:='寒 露';
20:Result:='霜 降';
21:Result:='立 冬';
22:Result:='小 雪';
23:Result:='大 雪';
24:Result:='冬 至';
else
l_CalcLunarDate(iYear,iMonth,iDay,CalcDateDiff(InDate,EncodeDate(START_YEAR,1,1)));
Result := trim(FormatMonth(iMonth) + FormatLunarDay(iDay));
end;
end;
function GetLunarHolDay(iYear,iMonth,iDay:Word):string;
begin
Result:=GetLunarHolDay(EncodeDate(iYear,iMonth,iDay));
end;
end.
—— 用Delphi处理公历到农历的转换