📄 calendar.c
字号:
if((constellation = getConstellation(cal)) < 0 ) return NULL; return ConstellationName[constellation];}//计算公历当天对应的节气 0-23,-1表示不是节气int l_GetLunarHolDay(CALENDAR_STRU *cal){ unsigned char Flag; int Day, iYear, iMonth, iDay; iYear = cal->y; if(iYear < START_YEAR || iYear > END_YEAR) return -1; //超出计算范围 iMonth = cal->m; iDay = cal->d; Flag = gLunarHolDay[(iYear - START_YEAR) * 12 + iMonth - 1]; if (iDay < 15) Day = 15 - ((Flag >> 4) & 0x0f); else Day = (Flag & 0x0f) + 15; if (iDay == Day) { if (iDay > 15) return (iMonth - 1) * 2 + 1; else return (iMonth - 1) * 2; } else return -1;}//返回中文形式的月份名char *formatMonth(int iMonth){ char strText[] = "正二三四五六七八九十"; static char strMonth[10]; int i; memset(strMonth, '\0', sizeof(strMonth)); if (iMonth <= 10) { strncpy(strMonth, &strText[(iMonth - 1) * 2], 2); strcat(strMonth, "月"); return strMonth; } i = iMonth % 10; strncpy(strMonth, "十", 2); strncpy(&strMonth[2], &strText[(iMonth - 1) * 2], 2); strcat(strMonth, "月"); return strMonth;}//返回中文形式的农历日期char *formatLunarDay(int iDay){ char strText1[] = "初十廿三"; char strText2[] = "一二三四五六七八九十"; static char strDay[14]; int i; if(iDay < 1 || iDay > 31) return NULL; memset(strDay, '\0', sizeof(strDay)); if ((iDay != 20) && (iDay != 30)) { memcpy(strDay, &strText1[(iDay - 1)/10], 2); memcpy(&strDay[2], &strText2[(iDay - 1) % 10], 2); return strDay; } memcpy(strDay, &strText1[(iDay/10)* 2], 2); strcat(strDay, "十"); return strDay;}char *getLunarHolDay(CALENDAR_STRU *cal){ int i, iYear, iMonth, iDay; iYear = cal->y; if (iYear < START_YEAR || iYear > END_YEAR) return NULL; i = l_GetLunarHolDay(cal); if ((i >= 0) && (i <= 23)) return LunarHolDayName[i];}//返回阴历iLunarYear年的闰月月份,如没有返回0 1901年1月---2050年12月int GetLeapMonth(int iLunarYear){ unsigned char Flag; if ((iLunarYear < START_YEAR) || (iLunarYear > END_YEAR)) return 0; Flag = gLunarMonth[(iLunarYear - START_YEAR) / 2]; if ((iLunarYear - START_YEAR) % 2 == 0) return Flag >> 4; return Flag & 0x0F;}//返回阴历iLunarYer年阴历iLunarMonth月的天数,如果iLunarMonth为闰月,//高字为第二个iLunarMonth月的天数,否则高字为0 1901年1月---2050年12月int LunarMonthDays(int iLunarYear, int iLunarMonth){ int Height, Low, iBit; if ((iLunarYear < START_YEAR) || (iLunarYear > END_YEAR)) return 30; Height = 0; Low = 29; iBit = 16 - iLunarMonth; if ((iLunarMonth > GetLeapMonth(iLunarYear)) && (GetLeapMonth(iLunarYear) > 0)) iBit--; if ((gLunarMonthDay[iLunarYear - START_YEAR] & (1 << iBit)) > 0) Low++; if (iLunarMonth == GetLeapMonth(iLunarYear)) { if ((gLunarMonthDay[iLunarYear - START_YEAR] & (1 << (iBit-1)))>0) Height = 30; else Height = 29; } return Height << 8 | Low;}//返回阴历iLunarYear年的总天数 1901年1月---2050年12月int LunarYearDays(int iLunarYear){ int Days; int tmp, i; if ((iLunarYear < START_YEAR) || (iLunarYear > END_YEAR)) return 0; Days = 0; for (i=1; i <= 12; i++) { tmp = LunarMonthDays(iLunarYear, i); Days += (tmp & 0xff) + ((tmp>>8) & 0xff); } return Days;}//计算公历时间天数century_days(int y, int m, int d){ return 365*y + (m-1)*30 + d + y/4 + (m>8? (m+1)/2 : m/2) - (m>2? 2 : y%4==0);}//日期形式转换int convLunDate(CALENDAR_STRU *cal, int mode){ int i, y, m, d, h, iDay; //按起始年1901年计算,阳历1901年2月19日为阴历1901年正月初一,阳历1901年1月1日到2月19日共有49天 y = cal->y; m = cal->m; d = cal->d; iDay = century_days(y,m,d) - century_days(START_YEAR, 1, 1); if(iDay < 0) return -1; if(iDay < 49) { cal->ly = 36; if(iDay < 19) { cal->lm = 10; cal->ld = 11 + iDay; } else { cal->lm = 11; cal->ld = iDay - 18; } return 0; } //下面从阴历1901年正月初一即1901.2.19算起 iDay = iDay - 49; cal->ly = 37; cal->lm = 0; cal->ld = 0; y = START_YEAR; m = 1; d = 1; //计算年 i = LunarYearDays(y++); while(iDay >= i) { iDay -= i; cal->ly +=1; cal->ly = cal->ly%60; i = LunarYearDays(y++); } //计算月 i = LunarMonthDays(y, m++); //取低位 d = i & 0xff; while (iDay >= d) { iDay -= d; if( (i >> 8) & 0xff ) { //本月闰月 d = (i >>8)& 0xff; //取高位 if (iDay < d) break; iDay -= d; } cal->lm +=1; i = LunarMonthDays(y, m++); //取低位 d = i & 0xff; } //计算日 cal->ld =iDay; cal->ly +=cal->lm/12; cal->lm = cal->lm % 12; cal->ly = cal->ly % 60;}//把iYear年格式化成天干记年法表示的字符串char *formatLunarYear(CALENDAR_STRU *cal) { char Text1[] = "甲乙丙丁戊己庚辛壬癸"; char Text2[] = "子丑寅卯辰巳午未申酉戌亥"; char Text3[] = "鼠牛虎免龙蛇马羊猴鸡狗猪"; static char strYear[10]; int ly; ly = ((unsigned char)cal->ly) % 60; memset(strYear, '\0', sizeof(strYear)); strncpy(strYear, &Text1[(ly % 10)*2], 2); strncpy(&strYear[2], &Text2[(ly % 12)*2], 2); return strYear; }//for testmain(int argc, char **argv){ int rc, i, y, m, d; CALENDAR_STRU cal; extern int optind; extern char *optarg; memset(&cal, '\0', sizeof(CALENDAR_STRU)); while((rc = getopt(argc, argv, "d:"))!=EOF) { switch(rc) { case 'd': sscanf(optarg,"%04d%02d%02d", &y, &m, &d); cal.y = y; cal.m = m; cal.d = d; break; default: return 0; } } convLunDate(&cal, 0); fprintf(stderr, "%s %d %d %d\n",formatLunarYear(&cal), cal.ly, cal.lm, cal.ld); return 0; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -