iOS get lunar calendar with leap month

Source: Internet
Author: User
Tags month name

/*
* @DO The date of the lunar calendar for the specified date
* @param date [date specified]
* @return (NSString) [Lunar string for specified date]
*/
+ (NSString *) Getchinesecalendarwithdate: (NSDate *) Date
{
/* Zodiac Name */
const char *ctiangan[] = {"A", "B", "C", "Ding", "e", "own", "Geng", "Xin", "Ren", "decyl"};
/* Support Name */
const char *cdizhi[] = {"Child", "ugly", "Yin", "Mao", "Chen", "WU", "noon",
"Not", "Shen", "unitary", "Xu", "Hai"};
/* Zodiac Name */
const char *cshuxiang[] = {"Rat", "ox", "Tiger", "Rabbit", "dragon", "Snake",
"Horse", "sheep", "monkey", "Chicken", "dog", "Pig"};
/* Lunar Date name */
const char *cdayname[] = {"*", "Junior", "Second day", "Third Day", "four", "Duanwu",
"Arrest", "When初七because", "Holidays", "Penggushan", "decade",
"11", "12", "13", "14", "XV",
"16", "17", "18", "19", "20",
"21", "22", "23", "24", "25",
"26", "27", "28", "29", "30"};
/* Lunar month name */
const char *cmonname[] = {"*", "positive", "two", "three", "four", "five", "six",
"Seven", "eight", "Nine", "Ten", "11", "Wax"};

/* Calendar days in front of month */
const int WMONTHADD[12] = {0,31,59,90,120,151,181,212,243,273,304,334};
/* Lunar Data */
const int WNONGLIDATA[100] =
{2635,333387,1701,1748,267701,694,2391,133423,1175,396438
, 3402,3749,331177,1453,694,201326,2350,465197,3221,3402
, 400202,2901,1386,267611,605,2349,137515,2709,464533,1738
, 2901,330421,1242,2651,199255,1323,529706,3733,1706,398762
, 2741,1206,267438,2647,1318,204070,3477,461653,1386,2413
, 330077,1197,2637,268877,3365,531109,2900,2922,398042,2395
, 1179,267415,2635,661067,1701,1748,398772,2742,2391,330031
, 1175,1611,200010,3749,527717,1452,2742,332397,2350,3222
, 268949,3402,3493,133973,1386,464219,605,2349,334123,2709
, 2890,267946,2773,592565,1210,2651,395863,1323,2707,265877};

static int wcuryear,wcurmonth,wcurday;
static int nthedate,nisend,m,k,n,i,nbit;

Char sznongli[30], sznongliday[10],szshuxiang[10];
NSString * Sznongli = @ "";
NSString * Sznonglimonth = @ "";
NSString * Szshuxiang = @ "";
/*---Take the current calendar year, month, Day---*/

Nscalendar * cal = [Nscalendar Currentcalendar];
Nsuinteger unitflags = nsdaycalendarunit| Nsmonthcalendarunit| Nsyearcalendarunit;
nsdatecomponents * conponent = [cal Components:unitflags Fromdate:date];

Wcuryear = [Conponent year];
Wcurmonth = [conponent month];
Wcurday = [Conponent day];

/*---Calculated to the initial time February 8, 1921 days: 1921-2-8 (first day of the first month)---*/
Nthedate = (wCurYear-1921) * 365 + (wCurYear-1921)/4 + Wcurday + Wmonthadd
[WCurMonth-1]-38;
if ((!) ( Wcuryear% 4) && (Wcurmonth > 2))
Nthedate = nthedate + 1;
/*--calculates lunar zodiac, earthly branches, months, days---* *
nisend = 0;
m = 0;
while (nisend! = 1)
{
if (Wnonglidata[m] < 4095)
K = 11;
Else
K = 12;
n = k;
while (n>=0)
{
Gets the value of nth bits of Wnonglidata (m)
NBit = Wnonglidata[m];
for (i=1;i<n+1;i++)
NBit = NBIT/2;
NBit = nBit% 2;
if (nthedate <= (+ nBit))
{
Nisend = 1;
Break
}
Nthedate = Nthedate-29-nbit;
n = n-1;
}
if (nisend)
Break
m = m + 1;
}

Wcuryear = 1921 + M;
Wcurmonth = k-n + 1;
Wcurday = nthedate;

if (k = = 12)
{
if (Wcurmonth = = Wnonglidata[m]/65536 + 1)
Wcurmonth = 1-wcurmonth;
else if (Wcurmonth > Wnonglidata[m]/65536 + 1)
Wcurmonth = wCurMonth-1;
}
/*--generates lunar zodiac, earthly branches, Zodiac ==> wnongli--*/

Szshuxiang = [NSString stringwithcstring:cshuxiang[((wCurYear-4)%)%] encoding:nsutf8stringencoding];

NSString * Tempyear = [NSString stringwithcstring:ctiangan[((wCurYear-4)%)%] encoding:nsutf8stringencoding];
NSString * Tempdizhi = [NSString stringwithcstring:cdizhi[((wCurYear-4)%)%] encoding:nsutf8stringencoding];
Sznongli = [NSString stringwithformat:@ "%@ (%@%@) year", Szshuxiang,tempyear,tempdizhi];
/*--generation lunar month, day ==> wnongliday--*/
if (Wcurmonth < 1) {
NSString * Tempmonth = [nsstring stringwithcstring:cmonname[-1 * wcurmonth] encoding:nsutf8stringencoding];
Sznonglimonth = [Sznonglimonth stringbyappendingformat:@ "Leap%@ Month", Tempmonth];
} else{
NSString * Tempmonth = [NSString stringwithcstring:cmonname[wcurmonth] encoding:nsutf8stringencoding];
Sznonglimonth = [Sznonglimonth stringbyappendingformat:@ "%@ month", Tempmonth];
}

NSString * Tempdayname = [NSString stringwithcstring:cdayname[wcurday] encoding:nsutf8stringencoding];
Sznongli = [Sznongli stringbyappendingformat:@ "%@%@", Sznonglimonth,tempdayname];

return Sznongli;

}

iOS get lunar calendar with leap month

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.