接着上一篇liblunar的日历bug问题,这里写了一个查询月份的工具,代码比较简陋
/* * author:pallover#gmail.com * website: http://pallove.is-programmer.com/ */ #include <lunar/lunar.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> int str2int(const char *str) { int n = 0; while(*str >= '0' && *str <= '9') { n = n * 10 + (*str - '0'); str++; } return n; } int get_weekday(int year, int month, int day) { int m = month; if(m == 1) { m = 13; year -= 1; } else if(m == 2) { m = 14; year -= 1; } int c = year / 100; int y = year % 100; int d = day; return (((c >> 2) - (c << 1) + y + (y >> 2) + (13 * (m + 1) / 5) + d - 1) % 7 + 7) % 7; } int get_mdays(int year, int month) { switch(month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: return 31; case 4: case 6: case 9: case 11: return 30; case 2: if(!(year % 4) && year % 100 || !(year % 400)) return 29; else return 28; } } int main(int argc, char *argv[]) { struct tm *tm_ptr; time_t the_time; (void) time(&the_time); tm_ptr = localtime(&the_time); int nyear = 0, nmonth = 1, nday = 1; if(argc > 1) { nyear = str2int(argv[1]); if(nyear <= 1900) nyear = tm_ptr->tm_year + 1900; if(argc > 2) { nmonth = str2int(argv[2]); if(nmonth < 1) nmonth = 1; if(argc > 3) { nday = str2int(argv[3]); if(nday < 1) nday = 1; } } } else { nyear = tm_ptr->tm_year + 1900; nmonth = tm_ptr->tm_mon + 1; nday = tm_ptr->tm_mday; printf("%d, %d, %d\n", nyear, nmonth, nday); } lunar_init(&argc, &argv); LunarDate *lunar_date = lunar_date_new(); lunar_date_set_solar_date(lunar_date, nyear, nmonth, nday, 1, NULL); gchar *str = lunar_date_strftime(lunar_date, "%(NIAN)年%(YUE)月%(RI) 生肖:%(shengxiao) \n节日:%(jieri)"); printf("\n%*s%s%d年%d月%d日%s\n\n", 20, "", "\033[47m\033[31m", nyear, nmonth, nday, "\033[0m"); printf("农历:%s\n\n", str); char *weekday = "日一二三四五六"; char ch[4]; while(*weekday){ strncpy(ch, weekday, 3); ch[3] = 0; weekday += 3; printf("%*s%2s", 6, "", ch); } int i = 1, line, nlen, n, firstwday, endline = 0; int nmdays = get_mdays(nyear, nmonth); firstwday = get_weekday(nyear, nmonth, 1); printf("\n"); line = 7 - firstwday; while(nmdays > 0) { if(line % 7 && !endline) printf("%*s", firstwday << 3, ""); for(nlen = 0; nlen < line; nlen++, i++) { n = get_weekday(nyear, nmonth, i); printf("%*s%s%2d%s", 6, "", ((i == nday) ? "\033[36m\033[1m" : ""), i, ((i == nday) ? "\033[0m" : "")); } i -= line; if(n == 6 || endline) { printf("\n"); } if(line % 7 && !endline) printf("%*s", firstwday << 3, ""); for(nlen = 0; nlen < line; nlen++, i++) { lunar_date_set_solar_date(lunar_date, nyear, nmonth, i, 0, NULL); printf("%*s%s%4s%s", 4, "", ((i == nday) ? "\033[36m\033[1m" : ""), lunar_date_strftime(lunar_date, "%(RI)"), ((i == nday) ? "\033[0m" : "")); } if(n == 6 || endline) { printf("\n\n"); } nmdays -= line; line = (nmdays >= 7) ? 7 : nmdays % 7, endline = 1; } lunar_date_free(lunar_date); exit(0); }
可以传入0-3个自己想查询有效参数(年,月,日),如不输入,则默认为本地日期。
2011年1月23日 11:08
不错,不过可否用 is-programmer 提供的 Syntax Higligher 插件整理一下?
2011年1月23日 21:41
改了下,还是不太好,syntax higlighter配置选项也只有颜色方案。