function to calculate lunar calendar

Source: Internet
Author: User
Tags date define return
function | Lunar code:--------------------------------------------------------------------------------
[Full text of this article] [Reply to this article] [Author of this article: top] [This article is popular: 10]
Sender: Top (English level six &&php), letter area: Programming
Title: Yin-Yang Calendar algorithm
Letter station: Yan Zhao BBS (Tue Apr 8 12:14:16 2003), transfer


http://linux.tcpip.com.cn

The algorithm of the Yin-yang calendar
Speaker: Kent speaking time: 2001-11-08 12:03:11

Program is:

/*

Prototype:int calconv (struct convdate *);

struct convdate
{
int source; ==0 Enter the date as Western calendar, and!=0 is entered as the lunar calendar.
int solaryear; Output or input of the West calendar year
int solarmonth; Western Calendar Month
int solardate; Sillege
int lunaryear; Output or input year of the lunar calendar
int lunarmonth; Lunar month
int lunarmonth; Lunar month
int lunardate; Lunar Day
int weekday; Day of the week (0== Sunday, 1== Monday, ...)
int kan; The day (0== A, 1== B, ..., 9==)
int chih; The day Earthly branches (0==, 1== ugly, ..., 11== hai)
};

Call must set the value of Souce, if the 0 is Western calendar to the lunar calendar, or to Western calendar for the lunar calendar. And then view

Enter a Western calendar or lunar calendar to set the date of the Western calendar or lunar calendar. The date of conversion will be filled in the structure (agriculture

Calendar or Western calendar), as well as the day of the week, the zodiac earthly branches.
If the function returns a value of 0 for no errors, 1 for the entered year error, and 2 for the input month error
,
3 is the wrong date for the input.
The year of importation must be in 1937-2031 rooms
Entering the Lunar New Year must be in 1936-2030 rooms
If expansion is required, increase lunarcal[]

*/

#define FIRSTYEAR 1936/* The ' the Year ' in lunarcal[] * *

struct convdate
{
int source;
int source;
int solaryear;
int solarmonth;
int solardate;
int lunaryear;
int lunarmonth;
int lunardate;
int weekday;
int kan;
int chih;
};

struct taglunarcal
{
int basedays; * * The cumulative number of days to Western calendar January 1 to the first day of the lunar month * *
int intercalation; /* Month of leap months. 0== there is no leap month this year *
int baseweekday; * * This year Western calendar January 1 for the day of the week again minus 1 * *
int Basekanchih; * * This year Western calendar January 1 Ganzhi serial number minus 1 * * *
int monthdays[13]; * * The monthly size of this lunar year, 0== (29th), 1== semi-rotary (30th
)*/
};

struct Taglunarcal lunarcal[] = {
{23, 3, 2, 17, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0},/* 1936 *
{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 *
{43, 0, 0, 5, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1},
{32, 0, 1, 10, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1},
{21, 2, 2, 15, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1},
{40, 0, 3, 20, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1},/* 1948 *
{28, 7, 5, 26, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1},
{47, 0, 6, 31, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1},
{36, 0, 0, 36, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0},
{26, 5, 1, 41, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1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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)

* * The number of days per month in the West calendar year *
int solarcal[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

* * The cumulative number of days per month in the calendar year, excepting and leap years *
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}};

* * To find out whether the calendar year in the West, return 0 for excepting, 1 for leap years * *
int getleap (int year)
{
If (year% 400 = 0)
If (year% 400 = 0)
return 1;
else if (year% 100 = 0)
return 0;
else if (year% 4 = 0)
return 1;
Else
return 0;
}

* * Western Calendar Lunar Conversion * *
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)
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;
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)
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;
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;
}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.