标题: [其他] 日历第三方 [打印本页]
作者: slimay 时间: 2021-6-4 16:42 标题: 日历第三方
仿写的公历cal, 均已源码发行,不提供二进制文件,可跨平台编译
cal 1997 8 //打印指定年月日历
cal 1997 //打印指定全年日历
cal //打印当前年月日历- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
-
- #define HELP_INFORMATION "\
- The calendar tool, Copyright@2021~2023 Slimay\n\
- usage:\n\
- cal [year] [month]\n\
- version 1.0\n"
-
- static const char* cal_show[] =
- {
- "Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa ",
- " 1 2 3 4 5 6 7 ",
- " 2 3 4 5 6 7 8 9 10 11 12 13 14 ",
- " 9 10 11 12 13 14 15 16 17 18 19 20 21 ",
- "16 17 18 19 20 21 22 23 24 25 26 27 28 ",
- NULL,
- "30 31 ",
- "23 24 25 26 27 28 29 30 31 ",
- "23 24 25 26 27 28 29 30 ",
- "23 24 25 26 27 28 29 ",
- "23 24 25 26 27 28 ",
- NULL
- };
-
- static const char* month_name[] =
- {
- " January February March",
- " April May June",
- " July August September",
- " October November December"
- };
-
- //设置月份显示矩阵
- int initMonthMatrix(int y, int m, int d)
- {
- cal_show[5] = cal_show[7];
-
- if
- (
- (m == 4) ||
- (m == 6) ||
- (m == 9) ||
- (m == 11)
- )
- {
- cal_show[5] = cal_show[8];
- }
-
- if(m % 12 == 2)
- {
- cal_show[5] = cal_show[10];
- if
- (
- ((m % 4 == 0) && (m % 100 != 0))||
- (m % 400 == 0)
- )
- {
- cal_show[5] = cal_show[9];
- }
- }
-
- if(m < 3)
- {
- m += 12;
- y--;
- }
- int w = (y + y/4 + y/400 - y/100 + 2*m + 3*(m+1)/5 + d) % 7;
-
- return w;
- }
-
- //获取月份日历
- int getCal(int y, int m, int d, int mode)
- {
- if(mode == 0)
- {
- int w = initMonthMatrix(y, m, d);
- for(int i = 0; i < 7; i ++)
- {
- printf("%.*s\n", 3*7, (i)?(cal_show[i] + 3*(6-w)):(cal_show[i]));
- }
- }
- else
- {
- for(int j = 1; j < 12; j += 3)
- {
- int w1 = initMonthMatrix(y, j + 0, d);
- int w2 = initMonthMatrix(y, j + 1, d);
- int w3 = initMonthMatrix(y, j + 2, d);
-
- printf("%s\n", month_name[j/3]);
- for(int i = 0; i < 7; i ++)
- {
- if(i == 5){initMonthMatrix(y, j + 0, d);}
- printf("%.*s ", 3*7, (i)?(cal_show[i] + 3*(6-w1)):(cal_show[i]));
-
- if(i == 5){initMonthMatrix(y, j + 1, d);}
- printf("%.*s ", 3*7, (i)?(cal_show[i] + 3*(6-w2)):(cal_show[i]));
-
- if(i == 5){initMonthMatrix(y, j + 2, d);}
- printf("%.*s\n", 3*7, (i)?(cal_show[i] + 3*(6-w3)):(cal_show[i]));
- }
- }
- }
-
- return 0;
- }
-
- // MAIN主函数
- int main(int argc, char** argv)
- {
- //开关检测
- if
- (
- ( argc > 3 ) ||
- ( stricmp(argv[1], "/?") == 0 ) ||
- ( stricmp(argv[1], "/h") == 0 ) ||
- ( stricmp(argv[1], "-h") == 0 ) ||
- ( stricmp(argv[1], "--h") == 0 ) ||
- ( stricmp(argv[1], "--help") == 0 )
- )
- {
- //使用说明
- printf(HELP_INFORMATION);
- exit(1);
- }
-
- if(argc ==1)
- {
- time_t timep;
- time (&timep);
- struct tm* p = gmtime(&timep);
- int y = 1900 + p->tm_year;
- int m = 1 + p->tm_mon;
- int d = 1 + p->tm_mday;
- printf(" %d-%02d-%02d\n", y, m, d);
- getCal(y, m, 1, 0);
- }
- else if(argc == 2)
- {
- printf("%d\n", atoi(argv[1]));
- getCal(atoi(argv[1]), 1, 1, 1);
- }
- else if(argc == 3)
- {
- printf(" %d-%02d\n", atoi(argv[1]), atoi(argv[2]));
- getCal(atoi(argv[1]), atoi(argv[2]), 1, 0);
- }
-
- return 0;
- }
复制代码
作者: slimay 时间: 2021-6-4 16:44
本帖最后由 slimay 于 2021-6-4 16:48 编辑
公历转农历- #include<stdio.h>
- #include<time.h>
-
- //天干
- const char* tian_gan[] = {"甲","乙","丙","丁","戊","己","庚","辛","壬","癸"};
- //地支
- const char* di_zhi[] = {"子","丑","寅","卯","辰","巳","午","未","申","酉","戌","亥"};
- //属相
- const char* shu_xiang[] = {"鼠","牛","虎","兔","龙","蛇","马","羊","猴","鸡","狗","猪"};
- //农历月名
- const char* lunar_month[] = {"正","二","三","四","五","六","七","八","九","十","冬","腊"};
- //朔望
- const char* lunar_day[] = {"初一","初二","初三","初四","初五","初六","初七","初八","初九","初十","十一","十二","十三","十四","十五","十六","十七","十八","十九","二十","廿一","廿二","廿三","廿四","廿五","廿六","廿七","廿八","廿九","三十"};
-
- //公历月累积天数
- unsigned int month_accumulate[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365, 396};
-
- //农历数据 ~ 自主研发的格式
- /*
- 存储农历闰月、春节日序数、农历月份大小 占用32位空间
- +-----------------------------------------------------+
- | 第25-32位 | 第17-24位 | 第1-16位 |
- |-----------|--------------|--------------------------|
- | 闰月 | 春节日序数 | 一个比特对应一个月份大小 |
- +-----------------------------------------------------+
- ((leapMonth << 24)|(springDate << 16)|(bitArryOrg))
- */
- unsigned int lunar_data[] =
- {
- 0x002D0BA5, 0x00220B49, 0x02171A93, 0x002A0A95, 0x091F152D, 0x00310556, 0x00260AB5, 0x061C15AA, 0x002F05D2, 0x00230DA5, /*1600-1609*/
- 0x03191D4A, 0x002C0D4A, 0x0B210A95, 0x00320A97, 0x00280556, 0x081D0AB5, 0x00300AD9, 0x002506D2, 0x041A0EA5, 0x002D0F25, /*1610-1619*/
- 0x0023064A, 0x02160C97, 0x00290AAB, 0x0A1F155A, 0x0032056A, 0x00260B69, 0x061C1752, 0x002F0792, 0x00240B25, 0x0418164B, /*1620-1629*/
- 0x002B0A4B, 0x0B2014AB, 0x003302AD, 0x002705AD, 0x081D0BA9, 0x00300DA9, 0x00260D92, 0x041A1D25, 0x002D0D25, 0x00220A55, /*1630-1639*/
- 0x011714AD, 0x002902B6, 0x0B1E15B5, 0x003206D4, 0x00270EC9, 0x061C1E92, 0x002F0E92, 0x00240D26, 0x04190A56, 0x002A0A5B, /*1640-1649*/
- 0x002004D6, 0x021506D5, 0x002806D5, 0x061D16C9, 0x00300749, 0x00250693, 0x051A152B, 0x002C052B, 0x00210A5B, 0x0317155A, /*1650-1659*/
- 0x002A056A, 0x071E0B65, 0x00310BA5, 0x00270B49, 0x061C1A95, 0x002E0A95, 0x0023052D, 0x04180AAD, 0x002B0AB5, 0x002005AA, /*1660-1669*/
- 0x02150BA5, 0x00280DA5, 0x071E1D4A, 0x00300D4A, 0x00250C96, 0x051A152E, 0x002D0556, 0x00210AB5, 0x031715B2, 0x002A06D2, /*1670-1679*/
- 0x081F0EA5, 0x00310725, 0x0026064B, 0x061B0C97, 0x002E04AB, 0x0022055B, 0x04180AD6, 0x002B0B6A, 0x00210752, 0x03151725, /*1680-1689*/
- 0x00280B25, 0x071D1A4B, 0x00300A4D, 0x002404AB, 0x0519056B, 0x002C05AD, 0x00220BAA, 0x03171B52, 0x002A0D92, 0x071F1D25, /*1690-1699*/
- 0x00320D25, 0x00270A55, 0x061C14AD, 0x002F04B6, 0x002405B5, 0x04190DAA, 0x002C0EC9, 0x00220E92, 0x03171D26, 0x00290D26, /*1700-1709*/
- 0x071E0A56, 0x00300A5B, 0x0026055A, 0x051A06D5, 0x002D0755, 0x00230749, 0x03180E93, 0x002A0693, 0x081F152B, 0x0032052B, /*1710-1719*/
- 0x00270AAB, 0x061C155A, 0x002F056A, 0x00240B65, 0x041A174A, 0x002C0B4A, 0x00210A95, 0x0316152B, 0x0029052D, 0x071D0AAD, /*1720-1729*/
- 0x00300AB5, 0x002605AA, 0x051B0BA5, 0x002D0DA5, 0x00230D4A, 0x04181D15, 0x002B0C96, 0x091F1956, 0x00320556, 0x00270AD5, /*1730-1739*/
- 0x061D15B2, 0x002F06D2, 0x00240EA5, 0x041A0E8A, 0x002C068B, 0x00200C97, 0x03160956, 0x0028055B, 0x071E0ADA, 0x00300B6A, /*1740-1749*/
- 0x00260752, 0x051B1725, 0x002E0B45, 0x00220A8B, 0x041714AB, 0x002A04AD, 0x091F096B, 0x003105B5, 0x00270BAA, 0x061D1B54, /*1750-1759*/
- 0x00300DA2, 0x00240D45, 0x05191A8D, 0x002C0A95, 0x002104AD, 0x021509AD, 0x002806B5, 0x071E0DAA, 0x00310ECA, 0x00260EA2, /*1760-1769*/
- 0x051B1D46, 0x002E0D4A, 0x00230A96, 0x03171536, 0x002A055A, 0x0A1F0AD5, 0x00320B65, 0x00270752, 0x061C0EA5, 0x002F06A5, /*1770-1779*/
- 0x0024054B, 0x05180A97, 0x002B0AAB, 0x0021055A, 0x03160AD5, 0x00280B65, 0x071E1752, 0x00310D52, 0x00260B15, 0x051A154B, /*1780-1789*/
- 0x002D054D, 0x00220AAD, 0x0418156A, 0x002A05B2, 0x001F0BA9, 0x02151D52, 0x00280D92, 0x061C1D15, 0x002F0D26, 0x00240956, /*1790-1799*/
- 0x04190AAD, 0x002C0AD6, 0x002205D4, 0x02170DA9, 0x002A0EC9, 0x061F0E8A, 0x0031068B, 0x00260D27, 0x051C0956, 0x002D095B, /*1800-1809*/
- 0x00230ADA, 0x031916D4, 0x002C0754, 0x00200745, 0x0215168B, 0x00280A93, 0x061D152B, 0x002F04AD, 0x0024096D, 0x041A0B6A, /*1810-1819*/
- 0x002D0BAA, 0x00220BA4, 0x03171B45, 0x002A0D45, 0x071F1A95, 0x00310A95, 0x0026052D, 0x051B0AAD, 0x002E0AB5, 0x00230DAA, /*1820-1829*/
- 0x04191DA4, 0x002C0EA4, 0x09211D4A, 0x00330D4A, 0x00280A96, 0x061D1536, 0x0030055A, 0x00240AD5, 0x041A16CA, 0x002D0752, /*1830-1839*/
- 0x00220EA5, 0x0317154A, 0x0029054B, 0x071E0A97, 0x00310AAB, 0x0026055A, 0x051B0B55, 0x002E0BA9, 0x00240752, 0x04181AA5, /*1840-1849*/
- 0x002B0B25, 0x08201A4B, 0x0033094D, 0x00270AAD, 0x071D156A, 0x003005B4, 0x00250BA9, 0x051A1D52, 0x002D0D92, 0x00220D25, /*1850-1859*/
- 0x03171A4D, 0x00290956, 0x081E12B5, 0x00310AD6, 0x002706D4, 0x051B0DA9, 0x002E0EC9, 0x00240E92, 0x04190D26, 0x002A0527, /*1860-1869*/
- 0x0A1F0A57, 0x0032095B, 0x00280B5A, 0x061D16D4, 0x00300754, 0x00250749, 0x051A1693, 0x002C0A93, 0x0021052B, 0x03160A5B, /*1870-1879*/
- 0x0029096D, 0x071E0B6A, 0x00310DAA, 0x00270BA4, 0x051C1B49, 0x002E0D49, 0x00230A95, 0x0418152B, 0x002B052D, 0x001F0AAD, /*1880-1889*/
- 0x0215156A, 0x00280DAA, 0x061E1DA4, 0x00300EA4, 0x00250D4A, 0x051A0A95, 0x002C0A97, 0x00210556, 0x03160AB5, 0x00290AD5, /*1890-1899*/
- 0x081F16D2, 0x00320752, 0x00270EA5, 0x051D164A, 0x002F064B, 0x00230A9B, 0x04191556, 0x002C056A, 0x00210B59, 0x02161752, /*1900-1909*/
- 0x00290752, 0x061E1B25, 0x00310B25, 0x00250A4B, 0x051A14AB, 0x002D02AD, 0x0022056B, 0x02170B69, 0x002A0DA9, 0x07201D92, /*1910-1919*/
- 0x00330E92, 0x00270D25, 0x051C1A4D, 0x002F0A56, 0x002402B6, 0x041815B5, 0x002C06D4, 0x00210EA9, 0x02171E92, 0x00290E92, /*1920-1929*/
- 0x061E0D26, 0x0030052B, 0x00250A57, 0x051A12B6, 0x002D0B5A, 0x002306D4, 0x03180EC9, 0x002A0749, 0x071F1693, 0x00320A93, /*1930-1939*/
- 0x0027052B, 0x061B0A5B, 0x002E0AAD, 0x0024056A, 0x04191B55, 0x002C0BA4, 0x00210B49, 0x02161A93, 0x00290A95, 0x071D152D, /*1940-1949*/
- 0x00300536, 0x00250AAD, 0x051B15AA, 0x002D05B2, 0x00220DA5, 0x03181D4A, 0x002B0D4A, 0x081F0A95, 0x00310A97, 0x00270556, /*1950-1959*/
- 0x061C0AB5, 0x002E0AD5, 0x002406D2, 0x04190EA5, 0x002C0EA5, 0x0021064A, 0x03150C97, 0x00280A9B, 0x071E155A, 0x0030056A, /*1960-1969*/
- 0x00250B69, 0x051B1752, 0x002E0B52, 0x00220B25, 0x0417164B, 0x002A0A4B, 0x081F14AB, 0x003102AD, 0x0026056D, 0x061C0B69, /*1970-1979*/
- 0x002F0DA9, 0x00240D92, 0x04191D25, 0x002C0D25, 0x0A211A4D, 0x00330A56, 0x002802B6, 0x061D05B5, 0x003006D5, 0x00250EA9, /*1980-1989*/
- 0x051B1E92, 0x002E0E92, 0x00230D26, 0x03170A56, 0x00290A57, 0x081F14D6, 0x0032035A, 0x002606D5, 0x051C16C9, 0x002F0749, /*1990-1999*/
- 0x00240693, 0x0418152B, 0x002B052B, 0x00200A5B, 0x0216155A, 0x0028056A, 0x071D1B55, 0x00310BA4, 0x00260B49, 0x051A1A93, /*2000-2009*/
- 0x002D0A95, 0x0022052D, 0x04170AAD, 0x00290AB5, 0x091F15AA, 0x003205D2, 0x00270DA5, 0x061C1D4A, 0x002F0D4A, 0x00240C95, /*2010-2019*/
- 0x0419152E, 0x002B0556, 0x00200AB5, 0x021615B2, 0x002906D2, 0x061D0EA5, 0x00300725, 0x0025064B, 0x051A0C97, 0x002C0CAB, /*2020-2029*/
- 0x0022055A, 0x03170AD6, 0x002A0B69, 0x0B1F1752, 0x00320B52, 0x00270B25, 0x061C1A4B, 0x002E0A4B, 0x002304AB, 0x0518055B, /*2030-2039*/
- 0x002B05AD, 0x00200B6A, 0x02161B52, 0x00290D92, 0x071E1D25, 0x00300D25, 0x00250A55, 0x051A14AD, 0x002D04B6, 0x002105B5, /*2040-2049*/
- 0x03170DAA, 0x002A0EC9, 0x08201E92, 0x00320E92, 0x00270D26, 0x061C0A56, 0x002E0A57, 0x00230556, 0x041806D5, 0x002B0755, /*2050-2059*/
- 0x00210749, 0x03150E93, 0x00280693, 0x071D152B, 0x0030052B, 0x00240A5B, 0x051A155A, 0x002D056A, 0x00220B65, 0x0417174A, /*2060-2069*/
- };
-
- //获取农历函数
- int getLunar(int year, int month, int day)
- {
- //获取给的年份 year 对应的农历数据
- unsigned int yLundarData = lunar_data[year - 1600];
-
- //获取 year 对应的农历闰月
- unsigned int leapMonth = yLundarData >> 24;
-
- //获取给定年 春节 在该年的日序数(就是该年的 第几天)
- unsigned int springIndex = (yLundarData >> 16)&0xFF;
-
- //获取给定 查询日期 在该年的日序数(就是该年的 第几天)
- unsigned int dayIndex = month_accumulate[month - 1] + day;
- //阳历闰月二月加一天
- if(month > 2)
- {
- if
- (
- ((year % 4 == 0) && (year % 100 != 0))||
- (year % 400 == 0)
- )
- {
- dayIndex += 1;
- }
- }
-
- // 查询日期在春节前,需要调取上一年的农历数据,因此递归 year - 1, month + 12
- if(dayIndex < springIndex)
- {
- return getLunar(year - 1, month + 12, day);
- }
-
- //查询日期在春节后
- unsigned int bitArry = yLundarData;
- int s = springIndex;
- int i = 1;
- while(i <= 13)
- {
- s += (bitArry & 1) ?30 :29;
- i ++;
- if(s > dayIndex)
- {
- i -= 1;
- s -= (bitArry & 1) ?30 :29;
- if((0 < leapMonth) && (leapMonth < i))
- {
- i -= 1;
- if(i == leapMonth)
- {
- // 农历 (dayIndex + 1 - s) 中的+1 表示农历是从初一开始的,
- return -(i*100) - (dayIndex + 1 - s);
- }
- }
-
- return (i*100) + (dayIndex + 1 - s);
- }
- bitArry >>= 1;
- }
-
- return 0;
- }
-
- //MAIN主函数
- int main()
- {
- //获取当前日期
- time_t timep;
- time (&timep);
- struct tm* p = gmtime(&timep);
- int year = 1900 + p->tm_year;
- int month = 1 + p->tm_mon;
- int day = 1 + p->tm_mday ;
-
- year = 1900;
- month = 9;
- day = 25;
- printf("%d %d %d\n", year, month, day);
-
- //计算天干地支属相
- int yTianGan = (year + 6) % 10;
- int yDiZhi = (year + 8) % 12;
- int yShuXiang = (year + 8) % 12;
-
- int lunVal = getLunar(year, month, day);
-
- if(lunVal != 0)
- {
- int lun = (lunVal >= 0) ?(lunVal) :(-lunVal);
- int lunMonth = lun / 100;
- int lunDay = lun % 100;
- printf("%s%s%s年 %s%s月%s\n", tian_gan[yTianGan], di_zhi[yDiZhi], shu_xiang[yShuXiang], (lunVal > 0)?"":"闰", lunar_month[lunMonth - 1], lunar_day[lunDay -1]);
- }
-
- return 0;
- }
复制代码
作者: locoman 时间: 2021-6-25 14:47
回复 2# slimay
请问:2楼的可以这样实现吗?
——
cal 2021 //得到年的农历
cal 2021.08 //得到年月的农历
cal 2021.08.27 //得到年月日农历
或者 cal 2021.08.27 //得到农历的年/月/日/
结果里面都是农历,分别用/进行了分隔
作者: slimay 时间: 2021-6-28 17:50
回复 3# locoman
可以的,核心都写好了,你直接套用核心函数自己做就行。
作者: locoman 时间: 2021-6-30 14:35
回复 locoman
可以的,核心都写好了,你直接套用核心函数自己做就行。
slimay 发表于 2021-6-28 17:50
您可以编译这样一个支持命令行的CMD程序出来,大家体验一下,可以吗??
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |