陽曆到陰曆的轉換 (轉)
陽曆到陰曆的轉換 (轉)[@more@]在CSDN上見到不少有人問關於陰曆的問題,我查了一下,文件中心還沒有相關的文章,碰巧手頭有一份資料,哪來的忘了,現在將全文貼出來,希望對大家有幫助。
- 90.語言, C (90:90/1237) ------------------------------------------- 90-C -
Msg : #404 [407]
From : Sunshine Lin 90:2010/633 Thu 29 Aug 96 22:05
To : Steven Shui Sun 01 Sep 96 05:03
Subj : 國曆轉農曆
--------------------------------------------------------------------------------
.MSGID: 90:2010/633.0 32264c56
.PID: BWRA 3.02 [Eval]
.TID: GE 1.11+
回覆如下...
SS> 請問有誰知道 "國曆轉農曆" 的演演算法?
SS> 望知道的前輩不佞賜教.
農曆的演演算法相當複雜, 多半都是由查表方式完成,
以下程式是在 Inte 找到的。
另外我還找到另一支程式是大陸人寫的, 可以反堆,
還可算八字。有興趣的話寫信給我, 我再 post 出來好了!!
--------------------------------------------------------------------
發信人: smhwang@phoenix (全自動之狼), 信區: programming
標 題: --- 西曆與農曆轉換函式 ---
發信站: 交大資工鳳凰城資訊站 (Wed Jul 26 01:51:17 1995)
轉信站: phoenix
Origin: kscg-ts5.kscg.gov.tw
本函式歡迎各站各版轉載, 無須經本人同意. 若對曆法有興趣, 歡迎討論.
/*
西曆農曆轉換程式 黃曉鳴 1995,7,25
: int CalConv( struct ConvDate * );
struct ConvDate
{
int ; ==0 則輸入日期為西曆, !=0 則輸入為農曆
int SolarYear; 輸出或輸入之西曆年份
int SolarMonth; 西曆月
int SolarDate; 西曆日
int LunarYear; 輸出或輸入之農曆年份
int LunarMonth; 農曆月
int LunarDate; 農曆日
int Weekday; 該日為星期幾 ( 0==星期日, 1==星期一, ... )
int Kan; 該日天干 ( 0==甲, 1==乙, ..., 9==癸 )
int Chih; 該日地支 ( 0==子, 1==醜, ..., 11==亥 )
};
呼叫時須設定 Souce 的值, 若為 0 則為西曆轉農曆, 否則為農曆轉西曆. 然後視
輸入為西曆或農曆來設定西曆或農曆的年月日. 轉換後的年月日會填入結構中( 農
歷或西曆 ), 以及該日為星期幾, 天干地支.
若函式的返回值為 0 表示沒有錯誤, 1 為輸入之年份錯誤, 2 為輸入之月份錯誤,
3 為輸入之日期錯誤.
輸入之西曆年須在 1937 - 2031 間
輸入之農曆年須在 1936 - 2030 間
若須擴充, 則增加 LunarCal[]
*/
#define FIRSTYEAR 1936 /* The first year in LunarCal[] */
struct ConvDate
{
int Source;
int SolarYear;
int SolarMonth;
int SolarDate;
int LunarYear;
int LunarMonth;
int LunarDate;
int Weekday;
int Kan;
int Chih;
};
struct tagLunarCal
{
int BaseDays; /* 到西曆 1 月 1 日到農曆正月初一的累積日數 */
int Intercalation; /* 閏月月份. 0==此年沒有閏月 */
int BaseWeekday; /* 此年西曆 1 月 1 日為星期幾再減 1 */
int BaseKanChih; /* 此年西曆 1 月 1 日之干支序號減 1 */
int MonthDays[13]; /* 此農曆年每月之大小, 0==小月(29日), 1==大月(30日) */
};
struct tagLunarCal LunarCal[] = {
{ 23, 3, 2, 17, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0 }, /* 1936 */
{ 41, 0, 4, 23, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1 },
{ 30, 7, 5, 28, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1 },
{ 49, 0, 6, 33, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1 },
{ 38, 0, 0, 38, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1 }, /* 1940 */
{ 26, 6, 2, 44, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0 },
{ 45, 0, 3, 49, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 },
{ 35, 0, 4, 54, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1 },
{ 24, 4, 5, 59, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1 }, /* 1944 */
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/* 2016 */
{ 27, 6, 6, 23, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1 },
{ 46, 0, 0, 28, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0 },
{ 35, 0, 1, 33, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0 },
{ 24, 4, 2, 38, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1 }, /* 2020 */
{ 42, 0, 4, 44, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1 },
{ 31, 0, 5, 49, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0 },
{ 21, 2, 6, 54, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1 },
{ 40, 0, 0, 59, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1 }, /* 2024 */
{ 28, 6, 2, 5, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0 },
{ 47, 0, 3, 10, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1 },
{ 36, 0, 4, 15, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1 },
{ 25, 5, 5, 20, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0 }, /* 2028 */
{ 43, 0, 0, 26, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1 },
{ 32, 0, 1, 31, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0 },
{ 22, 3, 2, 36, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0 } };
#define LASTYEAR (FIRSTYEAR+sizeof(LunarCal)/sizeof(struct tagLunarCal)-1)
/* 西曆年每月之日數 */
int SolarCal[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
/* 西曆年每月之累積日數, 平年與閏年 */
int SolarDays[2][14] = {
{ 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365, 396 },
{ 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366, 397 } };
/* 求此西曆年是否為閏年, 返回 0 為平年, 1 為閏年 */
int GetLeap( int year )
{
if ( year % 400 == 0 )
return 1;
else if ( year % 100 == 0 )
return 0;
else if ( year % 4 == 0 )
return 1;
else
return 0;
}
/* 西曆農曆轉換 */
int CalConv( struct ConvDate *cd )
{
int leap, d, sm, y, im, l1, l2, acc, i, lm, kc;
if ( cd->Source == 0 ) /* Solar */
{
if ( cd->SolarYear <= FIRSTYEAR ¦¦ cd->SolarYear > LASTYEAR )
return 1;
sm = cd->SolarMonth - 1;
if ( sm < 0 ¦¦ sm > 11 )
return 2;
leap = GetLeap( cd->SolarYear );
if ( sm == 1 )
d = leap + 28;
else
d = SolarCal[sm];
if ( cd->SolarDate < 1 ¦¦ cd->SolarDate > d )
return 3;
y = cd->SolarYear - FIRSTYEAR;
acc = SolarDays[leap][sm] + cd->SolarDate;
cd->Weekday = ( acc + LunarCal[y].BaseWeekday ) % 7;
kc = acc + LunarCal[y].BaseKanChih;
cd->Kan = kc % 10;
cd->Chih = kc % 12;
if ( acc <= LunarCal[y].BaseDays )
{
y--;
cd->LunarYear = cd->SolarYear - 1;
leap = GetLeap( cd->LunarYear );
sm += 12;
acc = SolarDays[leap][sm] + cd->SolarDate;
}
else
cd->LunarYear = cd->SolarYear;
l1 = LunarCal[y].BaseDays;
for ( i=0; i<13; i++ )
{
l2 = l1 + LunarCal[y].MonthDays[i] + 29;
if ( acc <= l2 )
break;
l1 = l2;
}
cd->LunarMonth = i + 1;
cd->LunarDate = acc - l1;
im = LunarCal[y].Intercalation;
if ( im != 0 && cd->LunarMonth > im )
{
cd->LunarMonth--;
if ( cd->LunarMonth == im )
cd->LunarMonth = -im;
}
if ( cd->LunarMonth > 12 )
cd->LunarMonth -= 12;
}
else /* Lunar */
{
if ( cd->LunarYear < FIRSTYEAR ¦¦ cd->LunarYear >= LASTYEAR )
return 1;
y = cd->LunarYear - FIRSTYEAR;
im = LunarCal[y].Intercalation;
lm = cd->LunarMonth;
if ( lm < 0 )
{
if ( lm != -im )
return 2;
}
else if ( lm < 1 ¦¦ lm > 12 )
return 2;
if ( im != 0 )
{
if ( lm > im )
lm++;
else if ( lm == -im )
lm = im + 1;
}
lm--;
if ( cd->LunarDate > LunarCal[y].MonthDays[lm] + 29 )
return 3;
acc = LunarCal[y].BaseDays;
for ( i=0; i acc += LunarCal[y].MonthDays[i] + 29;
acc += cd->LunarDate;
leap = GetLeap( cd->LunarYear );
for ( i=13; i>=0; i-- )
if ( acc > SolarDays[leap][i] )
break;
cd->SolarDate = acc - SolarDays[leap][i];
if ( i <= 11 )
{
cd->SolarYear = cd->LunarYear;
cd->SolarMonth = i + 1;
}
else
{
cd->SolarYear = cd->LunarYear + 1;
cd->SolarMonth = i - 11;
}
leap = GetLeap( cd->SolarYear );
y = cd->SolarYear - FIRSTYEAR;
acc = SolarDays[leap][cd->SolarMonth-1] + cd->SolarDate;
cd->Weekday = ( acc + LunarCal[y].BaseWeekday ) % 7;
kc = acc + LunarCal[y].BaseKanChih;
cd->Kan = kc % 10;
cd->Chih = kc % 12;
}
return 0;
}
- 90.語言, C (90:90/1237) ------------------------------------------- 90-C -
Msg : #404 [407]
From : Sunshine Lin 90:2010/633 Thu 29 Aug 96 22:05
To : Steven Shui Sun 01 Sep 96 05:03
Subj : 國曆轉農曆
--------------------------------------------------------------------------------
.MSGID: 90:2010/633.0 32264c56
.PID: BWRA 3.02 [Eval]
.TID: GE 1.11+
回覆如下...
SS> 請問有誰知道 "國曆轉農曆" 的演演算法?
SS> 望知道的前輩不佞賜教.
農曆的演演算法相當複雜, 多半都是由查表方式完成,
以下程式是在 Inte 找到的。
另外我還找到另一支程式是大陸人寫的, 可以反堆,
還可算八字。有興趣的話寫信給我, 我再 post 出來好了!!
--------------------------------------------------------------------
發信人: smhwang@phoenix (全自動之狼), 信區: programming
標 題: --- 西曆與農曆轉換函式 ---
發信站: 交大資工鳳凰城資訊站 (Wed Jul 26 01:51:17 1995)
轉信站: phoenix
Origin: kscg-ts5.kscg.gov.tw
本函式歡迎各站各版轉載, 無須經本人同意. 若對曆法有興趣, 歡迎討論.
/*
西曆農曆轉換程式 黃曉鳴 1995,7,25
: int CalConv( struct ConvDate * );
struct ConvDate
{
int ; ==0 則輸入日期為西曆, !=0 則輸入為農曆
int SolarYear; 輸出或輸入之西曆年份
int SolarMonth; 西曆月
int SolarDate; 西曆日
int LunarYear; 輸出或輸入之農曆年份
int LunarMonth; 農曆月
int LunarDate; 農曆日
int Weekday; 該日為星期幾 ( 0==星期日, 1==星期一, ... )
int Kan; 該日天干 ( 0==甲, 1==乙, ..., 9==癸 )
int Chih; 該日地支 ( 0==子, 1==醜, ..., 11==亥 )
};
呼叫時須設定 Souce 的值, 若為 0 則為西曆轉農曆, 否則為農曆轉西曆. 然後視
輸入為西曆或農曆來設定西曆或農曆的年月日. 轉換後的年月日會填入結構中( 農
歷或西曆 ), 以及該日為星期幾, 天干地支.
若函式的返回值為 0 表示沒有錯誤, 1 為輸入之年份錯誤, 2 為輸入之月份錯誤,
3 為輸入之日期錯誤.
輸入之西曆年須在 1937 - 2031 間
輸入之農曆年須在 1936 - 2030 間
若須擴充, 則增加 LunarCal[]
*/
#define FIRSTYEAR 1936 /* The first year in LunarCal[] */
struct ConvDate
{
int Source;
int SolarYear;
int SolarMonth;
int SolarDate;
int LunarYear;
int LunarMonth;
int LunarDate;
int Weekday;
int Kan;
int Chih;
};
struct tagLunarCal
{
int BaseDays; /* 到西曆 1 月 1 日到農曆正月初一的累積日數 */
int Intercalation; /* 閏月月份. 0==此年沒有閏月 */
int BaseWeekday; /* 此年西曆 1 月 1 日為星期幾再減 1 */
int BaseKanChih; /* 此年西曆 1 月 1 日之干支序號減 1 */
int MonthDays[13]; /* 此農曆年每月之大小, 0==小月(29日), 1==大月(30日) */
};
struct tagLunarCal LunarCal[] = {
{ 23, 3, 2, 17, 1,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47, 0, 3, 10, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1 },
{ 36, 0, 4, 15, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1 },
{ 25, 5, 5, 20, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0 }, /* 2028 */
{ 43, 0, 0, 26, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1 },
{ 32, 0, 1, 31, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0 },
{ 22, 3, 2, 36, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0 } };
#define LASTYEAR (FIRSTYEAR+sizeof(LunarCal)/sizeof(struct tagLunarCal)-1)
/* 西曆年每月之日數 */
int SolarCal[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
/* 西曆年每月之累積日數, 平年與閏年 */
int SolarDays[2][14] = {
{ 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365, 396 },
{ 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366, 397 } };
/* 求此西曆年是否為閏年, 返回 0 為平年, 1 為閏年 */
int GetLeap( int year )
{
if ( year % 400 == 0 )
return 1;
else if ( year % 100 == 0 )
return 0;
else if ( year % 4 == 0 )
return 1;
else
return 0;
}
/* 西曆農曆轉換 */
int CalConv( struct ConvDate *cd )
{
int leap, d, sm, y, im, l1, l2, acc, i, lm, kc;
if ( cd->Source == 0 ) /* Solar */
{
if ( cd->SolarYear <= FIRSTYEAR ¦¦ cd->SolarYear > LASTYEAR )
return 1;
sm = cd->SolarMonth - 1;
if ( sm < 0 ¦¦ sm > 11 )
return 2;
leap = GetLeap( cd->SolarYear );
if ( sm == 1 )
d = leap + 28;
else
d = SolarCal[sm];
if ( cd->SolarDate < 1 ¦¦ cd->SolarDate > d )
return 3;
y = cd->SolarYear - FIRSTYEAR;
acc = SolarDays[leap][sm] + cd->SolarDate;
cd->Weekday = ( acc + LunarCal[y].BaseWeekday ) % 7;
kc = acc + LunarCal[y].BaseKanChih;
cd->Kan = kc % 10;
cd->Chih = kc % 12;
if ( acc <= LunarCal[y].BaseDays )
{
y--;
cd->LunarYear = cd->SolarYear - 1;
leap = GetLeap( cd->LunarYear );
sm += 12;
acc = SolarDays[leap][sm] + cd->SolarDate;
}
else
cd->LunarYear = cd->SolarYear;
l1 = LunarCal[y].BaseDays;
for ( i=0; i<13; i++ )
{
l2 = l1 + LunarCal[y].MonthDays[i] + 29;
if ( acc <= l2 )
break;
l1 = l2;
}
cd->LunarMonth = i + 1;
cd->LunarDate = acc - l1;
im = LunarCal[y].Intercalation;
if ( im != 0 && cd->LunarMonth > im )
{
cd->LunarMonth--;
if ( cd->LunarMonth == im )
cd->LunarMonth = -im;
}
if ( cd->LunarMonth > 12 )
cd->LunarMonth -= 12;
}
else /* Lunar */
{
if ( cd->LunarYear < FIRSTYEAR ¦¦ cd->LunarYear >= LASTYEAR )
return 1;
y = cd->LunarYear - FIRSTYEAR;
im = LunarCal[y].Intercalation;
lm = cd->LunarMonth;
if ( lm < 0 )
{
if ( lm != -im )
return 2;
}
else if ( lm < 1 ¦¦ lm > 12 )
return 2;
if ( im != 0 )
{
if ( lm > im )
lm++;
else if ( lm == -im )
lm = im + 1;
}
lm--;
if ( cd->LunarDate > LunarCal[y].MonthDays[lm] + 29 )
return 3;
acc = LunarCal[y].BaseDays;
for ( i=0; i
acc += cd->LunarDate;
leap = GetLeap( cd->LunarYear );
for ( i=13; i>=0; i-- )
if ( acc > SolarDays[leap][i] )
break;
cd->SolarDate = acc - SolarDays[leap][i];
if ( i <= 11 )
{
cd->SolarYear = cd->LunarYear;
cd->SolarMonth = i + 1;
}
else
{
cd->SolarYear = cd->LunarYear + 1;
cd->SolarMonth = i - 11;
}
leap = GetLeap( cd->SolarYear );
y = cd->SolarYear - FIRSTYEAR;
acc = SolarDays[leap][cd->SolarMonth-1] + cd->SolarDate;
cd->Weekday = ( acc + LunarCal[y].BaseWeekday ) % 7;
kc = acc + LunarCal[y].BaseKanChih;
cd->Kan = kc % 10;
cd->Chih = kc % 12;
}
return 0;
}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752043/viewspace-987928/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- PHP獲取農曆、陽曆轉陰曆PHP
- 陽曆和陰曆相互轉化的工具類 golang版本Golang
- 用Delphi處理公曆到農曆的轉換 (轉)
- angular 日期(陽曆)Angular
- Oracle和JDE日曆轉換Oracle
- 公曆日期轉農曆日期
- 實現陽曆和農曆顯示日期的javascript程式碼JavaScript
- Android自定義預定日曆,並且顯示陰曆Android
- 農曆的演算法 (轉)演算法
- 陰陽曆演算法 (轉)演算法
- 日曆函式單元 (轉)函式
- 專案經理成長曆程(轉)
- Java 周曆日曆Java
- 小程式-日曆簽到
- 在access中增加農曆支援模組. (轉)
- JAVA例項 陰陽曆演算法(轉)Java演算法
- POJ-1008 Maya Calendar-兩種曆法轉換
- JS中1900-2100區間內的公曆、農曆互轉的工具類JS
- 相當於delphi的日曆控制元件 (轉)控制元件
- JavaScript 實現日曆式日期選擇 (轉)JavaScript
- JS編寫日曆控制元件(支援單日曆 雙日曆 甚至多日曆等)JS控制元件
- 根據公曆計算農曆
- [轉載] Python日曆模組| 使用示例的weekday()方法Python
- [轉載]一個計算機高手的成長曆程計算機
- 微信小程式實現簽到的日曆微信小程式
- 縱享絲滑滑動切換的周月日曆,水滴效果,豐富自定義日曆樣式,仿小米日曆(ViewDragHelper實現)View
- HTML元件(HTMLCOMPONENTS)之四編寫日曆(1)(轉)HTML元件
- HTML元件(HTMLCOMPONENTS)之四編寫日曆(2)(轉)HTML元件
- HTML元件(HTMLCOMPONENTS)之四編寫日曆(3)(轉)HTML元件
- win10 日曆怎麼顯示農曆_win10日曆不顯示農曆怎麼辦Win10
- 前端 javascript 練習題 -簡易年曆及tab切換簡易年曆【千鋒】前端JavaScript
- win10電腦日曆怎麼顯示農曆_win10系統日曆顯示農曆的設定方法Win10
- 【SQL】日曆SQL
- Win10日曆應用怎麼更換主題_Win10日曆應用如何更換背景Win10
- vue之實現日曆----顯示農曆,滾動日曆監聽年月改變Vue
- 演算法系列之十七:日曆生成演算法-中國公曆(格里曆)(下)演算法
- 日曆2021年日曆表|2021年日曆表列印版 Excel版Excel
- 如何在Mac中使用佛教日曆,波斯日曆等Mac