完全是标准的c函数,不过我在vc下写的,。相信移植没问题。
另外,我还用php写了一遍。
/*************************************************
计算阴历日期
范围从2000/02/05开始的20年
代码实现:耙子 2001/08/17
最后修订: 2001/08/18
**************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
typedef unsigned int DWORD;
/**************************************************
sLunar: 返回阴历日期字符串
dwDays: 所求日期距离 2000/02/05的天数差
**************************************************/
void Lunar2Solar(char *sLunar, const DWORD dwDays)
{
const char sTG[][3]= {"甲","乙","丙","丁","戊","己","庚","辛","壬","癸"};
const char sDZ[][3]= {"子","丑","寅","卯","辰","巳","午","未","申","酉","戌","亥"};
const char sSX[][3]= {"鼠","牛","虎","兔","龙","蛇","马","羊","猴","鸡","狗","猪"};
const char sMon[][3]= {"正","二","三","四","五","六","七","八","九","十","冬","腊"};
const char sDate[][5]= {
"初一","初二","初三","初四","初五","初六","初七","初八","初九","初十",
"十一","十二","十三","十四","十五","十六","十七","十八","十九","二十",
"廿一","廿二","廿三","廿四","廿五","廿六","廿七","廿八","廿九","三十"
};
// Start from 2000/2/5, 20 Lunar years' Data
const DWORD dwLunarYears= 20;
const DWORD dwCalendarData[]=
{
0xc96, // 0000 0000 1100 1001 0110
0x41a95, // 0100 0001 1010 1001 0101
0xd4a, // 0000 0000 0100 1101 1010
0xda5,
0x20b55,
0x56a,
0x7155b,
0x25d,
0x92d,
0x5192b,
0xa95,
0xb4a,
0x416aa,
0xad5,
0x90ab5,
0x4ba,
0xa5b,
0x60a57,
0x52b,
0xa93,
0x40e59
};
DWORD i, j;
DWORD yy=0, mm=0;
DWORD t, r, m, p=0, q;
for (i=0; i< dwLunarYears; i++)
{
t= dwCalendarData[i];
r= (t >> 16) & 0x0f;
m= (r==0)?12:13;
for (j=0; j<m; j++)
{
q= ((t >> (m-j-1)) & 1)==0 ? 29 : 30;
if (p+q > dwDays)
break;
p+= q;
}
if (j < m)
break;
}
// Date Overflow
if (i == dwLunarYears)
{
sprintf(sLunar, "%s", "I don't know so much!");
return;
}
sprintf(
sLunar,
"%s%s年(%s) %s%s月%s",
sTG[(i+6)%10], // Heavenly stemas
sDZ[(i+4)%12], // Earthly Branches
sSX[(i+4)%12], // Shu xing
(((r!=0) && (j == r)) ? "闰" : ""), // Leap month
sMon[((r!=0) && (j > r-1)) ? j-1 : j],
sDate[dwDays-p]
);
return;
}
// 测试代码
int main ()
{
time_t t1, t2;
struct tm tt;
DWORD dwDiffTime;
char sLunar[32];
tt.tm_year= 2000-1900;
tt.tm_mon=1;
tt.tm_mday=5;
tt.tm_hour=0;
tt.tm_min=0;
tt.tm_sec=0;
t1= mktime(&tt);
for (int i=0; i< 365; i++ ) {
tt.tm_year= 2030-1900;
tt.tm_mon=0;
tt.tm_mday=i;
tt.tm_hour=0;
tt.tm_min=0;
tt.tm_sec=0;
t2= mktime(&tt);
printf("%s", ctime(&t2));
dwDiffTime= difftime(t2,t1) / (60*60*24);
printf("起第 %d 天 ", dwDiffTime);
Lunar2Solar(sLunar, dwDiffTime);
puts(sLunar);
}
getch();
return 0;
}
正文
农历公历换算(ZT)2005-08-08 11:04:00
【评论】 【打印】 【字体:大 中 小】 本文链接:http://blog.pfan.cn/elva6401/3615.html
阅读(7145) | 评论(0)
版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!
评论