Board logo

标题: [其他] 日历第三方 [打印本页]

作者: slimay    时间: 2021-6-4 16:42     标题: 日历第三方

仿写的公历cal, 均已源码发行,不提供二进制文件,可跨平台编译
cal 1997 8   //打印指定年月日历
cal 1997      //打印指定全年日历
cal               //打印当前年月日历
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5. #define HELP_INFORMATION "\
  6. The calendar tool, Copyright@2021~2023 Slimay\n\
  7. usage:\n\
  8.     cal [year] [month]\n\
  9. version 1.0\n"
  10. static const char* cal_show[] =
  11. {
  12. "Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa ",
  13. "                   1  2  3  4  5  6  7 ",
  14. " 2  3  4  5  6  7  8  9 10 11 12 13 14 ",
  15. " 9 10 11 12 13 14 15 16 17 18 19 20 21 ",
  16. "16 17 18 19 20 21 22 23 24 25 26 27 28 ",
  17. NULL,
  18. "30 31                                  ",
  19. "23 24 25 26 27 28 29 30 31             ",
  20. "23 24 25 26 27 28 29 30                ",
  21. "23 24 25 26 27 28 29                   ",
  22. "23 24 25 26 27 28                      ",
  23. NULL
  24. };
  25. static const char* month_name[] =
  26. {
  27. "       January                February                March",
  28. "        April                   May                    June",
  29. "        July                   August                September",
  30. "       October                November               December"
  31. };
  32. //设置月份显示矩阵
  33. int initMonthMatrix(int y, int m, int d)
  34. {
  35. cal_show[5] = cal_show[7];
  36. if
  37. (
  38.     (m ==  4) ||
  39.     (m ==  6) ||
  40.     (m ==  9) ||
  41.     (m == 11)
  42. )
  43. {
  44. cal_show[5] = cal_show[8];
  45. }
  46. if(m % 12 == 2)
  47. {
  48. cal_show[5] = cal_show[10];
  49. if
  50. (
  51.     ((m % 4 == 0) && (m % 100 != 0))||
  52.     (m % 400 == 0)
  53. )
  54. {
  55. cal_show[5] = cal_show[9];
  56. }
  57. }
  58. if(m < 3)
  59. {
  60. m += 12;
  61. y--;
  62. }
  63. int w = (y + y/4 + y/400 - y/100 + 2*m + 3*(m+1)/5 + d) % 7;
  64. return w;
  65. }
  66. //获取月份日历
  67. int getCal(int y, int m, int d, int mode)
  68. {
  69. if(mode == 0)
  70. {
  71. int w = initMonthMatrix(y, m, d);
  72. for(int i = 0; i < 7; i ++)
  73. {
  74. printf("%.*s\n", 3*7, (i)?(cal_show[i] + 3*(6-w)):(cal_show[i]));
  75. }
  76. }
  77. else
  78. {
  79. for(int j = 1; j < 12; j += 3)
  80. {
  81. int w1 = initMonthMatrix(y, j + 0, d);
  82. int w2 = initMonthMatrix(y, j + 1, d);
  83. int w3 = initMonthMatrix(y, j + 2, d);
  84. printf("%s\n", month_name[j/3]);
  85. for(int i = 0; i < 7; i ++)
  86. {
  87. if(i == 5){initMonthMatrix(y, j + 0, d);}
  88. printf("%.*s   ", 3*7, (i)?(cal_show[i] + 3*(6-w1)):(cal_show[i]));
  89. if(i == 5){initMonthMatrix(y, j + 1, d);}
  90. printf("%.*s   ", 3*7, (i)?(cal_show[i] + 3*(6-w2)):(cal_show[i]));
  91. if(i == 5){initMonthMatrix(y, j + 2, d);}
  92. printf("%.*s\n", 3*7, (i)?(cal_show[i] + 3*(6-w3)):(cal_show[i]));
  93. }
  94. }
  95. }
  96. return 0;
  97. }
  98. // MAIN主函数
  99. int main(int argc, char** argv)
  100. {
  101. //开关检测
  102. if
  103. (
  104. ( argc >  3 )                       ||
  105. ( stricmp(argv[1], "/?") == 0 )     ||
  106. ( stricmp(argv[1], "/h") == 0 )     ||
  107. ( stricmp(argv[1], "-h") == 0 )     ||
  108. ( stricmp(argv[1], "--h") == 0 )    ||
  109. ( stricmp(argv[1], "--help") == 0 )
  110. )
  111. {
  112. //使用说明
  113. printf(HELP_INFORMATION);
  114. exit(1);
  115. }
  116. if(argc ==1)
  117. {
  118. time_t timep;
  119. time (&timep);
  120. struct tm* p = gmtime(&timep);
  121. int y = 1900 + p->tm_year;
  122. int m = 1 + p->tm_mon;
  123. int d = 1 + p->tm_mday;
  124. printf("     %d-%02d-%02d\n", y, m, d);
  125. getCal(y, m, 1, 0);
  126. }
  127. else if(argc == 2)
  128. {
  129. printf("%d\n", atoi(argv[1]));
  130. getCal(atoi(argv[1]), 1, 1, 1);
  131. }
  132. else if(argc == 3)
  133. {
  134. printf("      %d-%02d\n", atoi(argv[1]), atoi(argv[2]));
  135. getCal(atoi(argv[1]), atoi(argv[2]), 1, 0);
  136. }
  137. return 0;
  138. }
复制代码

作者: slimay    时间: 2021-6-4 16:44

本帖最后由 slimay 于 2021-6-4 16:48 编辑

公历转农历
  1. #include<stdio.h>
  2. #include<time.h>
  3. //天干
  4. const char*    tian_gan[] = {"甲","乙","丙","丁","戊","己","庚","辛","壬","癸"};
  5. //地支
  6. const char*      di_zhi[] = {"子","丑","寅","卯","辰","巳","午","未","申","酉","戌","亥"};
  7. //属相
  8. const char*   shu_xiang[] = {"鼠","牛","虎","兔","龙","蛇","马","羊","猴","鸡","狗","猪"};
  9. //农历月名
  10. const char* lunar_month[] = {"正","二","三","四","五","六","七","八","九","十","冬","腊"};
  11. //朔望
  12. const char*   lunar_day[] = {"初一","初二","初三","初四","初五","初六","初七","初八","初九","初十","十一","十二","十三","十四","十五","十六","十七","十八","十九","二十","廿一","廿二","廿三","廿四","廿五","廿六","廿七","廿八","廿九","三十"};
  13. //公历月累积天数
  14. unsigned int month_accumulate[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365, 396};
  15. //农历数据 ~ 自主研发的格式
  16. /*
  17. 存储农历闰月、春节日序数、农历月份大小   占用32位空间
  18. +-----------------------------------------------------+
  19. | 第25-32位 |  第17-24位   |         第1-16位         |
  20. |-----------|--------------|--------------------------|
  21. |    闰月   |  春节日序数  | 一个比特对应一个月份大小 |
  22. +-----------------------------------------------------+
  23. ((leapMonth << 24)|(springDate << 16)|(bitArryOrg))
  24. */
  25. unsigned int lunar_data[] =
  26. {
  27. 0x002D0BA5, 0x00220B49, 0x02171A93, 0x002A0A95, 0x091F152D, 0x00310556, 0x00260AB5, 0x061C15AA, 0x002F05D2, 0x00230DA5, /*1600-1609*/
  28. 0x03191D4A, 0x002C0D4A, 0x0B210A95, 0x00320A97, 0x00280556, 0x081D0AB5, 0x00300AD9, 0x002506D2, 0x041A0EA5, 0x002D0F25, /*1610-1619*/
  29. 0x0023064A, 0x02160C97, 0x00290AAB, 0x0A1F155A, 0x0032056A, 0x00260B69, 0x061C1752, 0x002F0792, 0x00240B25, 0x0418164B, /*1620-1629*/
  30. 0x002B0A4B, 0x0B2014AB, 0x003302AD, 0x002705AD, 0x081D0BA9, 0x00300DA9, 0x00260D92, 0x041A1D25, 0x002D0D25, 0x00220A55, /*1630-1639*/
  31. 0x011714AD, 0x002902B6, 0x0B1E15B5, 0x003206D4, 0x00270EC9, 0x061C1E92, 0x002F0E92, 0x00240D26, 0x04190A56, 0x002A0A5B, /*1640-1649*/
  32. 0x002004D6, 0x021506D5, 0x002806D5, 0x061D16C9, 0x00300749, 0x00250693, 0x051A152B, 0x002C052B, 0x00210A5B, 0x0317155A, /*1650-1659*/
  33. 0x002A056A, 0x071E0B65, 0x00310BA5, 0x00270B49, 0x061C1A95, 0x002E0A95, 0x0023052D, 0x04180AAD, 0x002B0AB5, 0x002005AA, /*1660-1669*/
  34. 0x02150BA5, 0x00280DA5, 0x071E1D4A, 0x00300D4A, 0x00250C96, 0x051A152E, 0x002D0556, 0x00210AB5, 0x031715B2, 0x002A06D2, /*1670-1679*/
  35. 0x081F0EA5, 0x00310725, 0x0026064B, 0x061B0C97, 0x002E04AB, 0x0022055B, 0x04180AD6, 0x002B0B6A, 0x00210752, 0x03151725, /*1680-1689*/
  36. 0x00280B25, 0x071D1A4B, 0x00300A4D, 0x002404AB, 0x0519056B, 0x002C05AD, 0x00220BAA, 0x03171B52, 0x002A0D92, 0x071F1D25, /*1690-1699*/
  37. 0x00320D25, 0x00270A55, 0x061C14AD, 0x002F04B6, 0x002405B5, 0x04190DAA, 0x002C0EC9, 0x00220E92, 0x03171D26, 0x00290D26, /*1700-1709*/
  38. 0x071E0A56, 0x00300A5B, 0x0026055A, 0x051A06D5, 0x002D0755, 0x00230749, 0x03180E93, 0x002A0693, 0x081F152B, 0x0032052B, /*1710-1719*/
  39. 0x00270AAB, 0x061C155A, 0x002F056A, 0x00240B65, 0x041A174A, 0x002C0B4A, 0x00210A95, 0x0316152B, 0x0029052D, 0x071D0AAD, /*1720-1729*/
  40. 0x00300AB5, 0x002605AA, 0x051B0BA5, 0x002D0DA5, 0x00230D4A, 0x04181D15, 0x002B0C96, 0x091F1956, 0x00320556, 0x00270AD5, /*1730-1739*/
  41. 0x061D15B2, 0x002F06D2, 0x00240EA5, 0x041A0E8A, 0x002C068B, 0x00200C97, 0x03160956, 0x0028055B, 0x071E0ADA, 0x00300B6A, /*1740-1749*/
  42. 0x00260752, 0x051B1725, 0x002E0B45, 0x00220A8B, 0x041714AB, 0x002A04AD, 0x091F096B, 0x003105B5, 0x00270BAA, 0x061D1B54, /*1750-1759*/
  43. 0x00300DA2, 0x00240D45, 0x05191A8D, 0x002C0A95, 0x002104AD, 0x021509AD, 0x002806B5, 0x071E0DAA, 0x00310ECA, 0x00260EA2, /*1760-1769*/
  44. 0x051B1D46, 0x002E0D4A, 0x00230A96, 0x03171536, 0x002A055A, 0x0A1F0AD5, 0x00320B65, 0x00270752, 0x061C0EA5, 0x002F06A5, /*1770-1779*/
  45. 0x0024054B, 0x05180A97, 0x002B0AAB, 0x0021055A, 0x03160AD5, 0x00280B65, 0x071E1752, 0x00310D52, 0x00260B15, 0x051A154B, /*1780-1789*/
  46. 0x002D054D, 0x00220AAD, 0x0418156A, 0x002A05B2, 0x001F0BA9, 0x02151D52, 0x00280D92, 0x061C1D15, 0x002F0D26, 0x00240956, /*1790-1799*/
  47. 0x04190AAD, 0x002C0AD6, 0x002205D4, 0x02170DA9, 0x002A0EC9, 0x061F0E8A, 0x0031068B, 0x00260D27, 0x051C0956, 0x002D095B, /*1800-1809*/
  48. 0x00230ADA, 0x031916D4, 0x002C0754, 0x00200745, 0x0215168B, 0x00280A93, 0x061D152B, 0x002F04AD, 0x0024096D, 0x041A0B6A, /*1810-1819*/
  49. 0x002D0BAA, 0x00220BA4, 0x03171B45, 0x002A0D45, 0x071F1A95, 0x00310A95, 0x0026052D, 0x051B0AAD, 0x002E0AB5, 0x00230DAA, /*1820-1829*/
  50. 0x04191DA4, 0x002C0EA4, 0x09211D4A, 0x00330D4A, 0x00280A96, 0x061D1536, 0x0030055A, 0x00240AD5, 0x041A16CA, 0x002D0752, /*1830-1839*/
  51. 0x00220EA5, 0x0317154A, 0x0029054B, 0x071E0A97, 0x00310AAB, 0x0026055A, 0x051B0B55, 0x002E0BA9, 0x00240752, 0x04181AA5, /*1840-1849*/
  52. 0x002B0B25, 0x08201A4B, 0x0033094D, 0x00270AAD, 0x071D156A, 0x003005B4, 0x00250BA9, 0x051A1D52, 0x002D0D92, 0x00220D25, /*1850-1859*/
  53. 0x03171A4D, 0x00290956, 0x081E12B5, 0x00310AD6, 0x002706D4, 0x051B0DA9, 0x002E0EC9, 0x00240E92, 0x04190D26, 0x002A0527, /*1860-1869*/
  54. 0x0A1F0A57, 0x0032095B, 0x00280B5A, 0x061D16D4, 0x00300754, 0x00250749, 0x051A1693, 0x002C0A93, 0x0021052B, 0x03160A5B, /*1870-1879*/
  55. 0x0029096D, 0x071E0B6A, 0x00310DAA, 0x00270BA4, 0x051C1B49, 0x002E0D49, 0x00230A95, 0x0418152B, 0x002B052D, 0x001F0AAD, /*1880-1889*/
  56. 0x0215156A, 0x00280DAA, 0x061E1DA4, 0x00300EA4, 0x00250D4A, 0x051A0A95, 0x002C0A97, 0x00210556, 0x03160AB5, 0x00290AD5, /*1890-1899*/
  57. 0x081F16D2, 0x00320752, 0x00270EA5, 0x051D164A, 0x002F064B, 0x00230A9B, 0x04191556, 0x002C056A, 0x00210B59, 0x02161752, /*1900-1909*/
  58. 0x00290752, 0x061E1B25, 0x00310B25, 0x00250A4B, 0x051A14AB, 0x002D02AD, 0x0022056B, 0x02170B69, 0x002A0DA9, 0x07201D92, /*1910-1919*/
  59. 0x00330E92, 0x00270D25, 0x051C1A4D, 0x002F0A56, 0x002402B6, 0x041815B5, 0x002C06D4, 0x00210EA9, 0x02171E92, 0x00290E92, /*1920-1929*/
  60. 0x061E0D26, 0x0030052B, 0x00250A57, 0x051A12B6, 0x002D0B5A, 0x002306D4, 0x03180EC9, 0x002A0749, 0x071F1693, 0x00320A93, /*1930-1939*/
  61. 0x0027052B, 0x061B0A5B, 0x002E0AAD, 0x0024056A, 0x04191B55, 0x002C0BA4, 0x00210B49, 0x02161A93, 0x00290A95, 0x071D152D, /*1940-1949*/
  62. 0x00300536, 0x00250AAD, 0x051B15AA, 0x002D05B2, 0x00220DA5, 0x03181D4A, 0x002B0D4A, 0x081F0A95, 0x00310A97, 0x00270556, /*1950-1959*/
  63. 0x061C0AB5, 0x002E0AD5, 0x002406D2, 0x04190EA5, 0x002C0EA5, 0x0021064A, 0x03150C97, 0x00280A9B, 0x071E155A, 0x0030056A, /*1960-1969*/
  64. 0x00250B69, 0x051B1752, 0x002E0B52, 0x00220B25, 0x0417164B, 0x002A0A4B, 0x081F14AB, 0x003102AD, 0x0026056D, 0x061C0B69, /*1970-1979*/
  65. 0x002F0DA9, 0x00240D92, 0x04191D25, 0x002C0D25, 0x0A211A4D, 0x00330A56, 0x002802B6, 0x061D05B5, 0x003006D5, 0x00250EA9, /*1980-1989*/
  66. 0x051B1E92, 0x002E0E92, 0x00230D26, 0x03170A56, 0x00290A57, 0x081F14D6, 0x0032035A, 0x002606D5, 0x051C16C9, 0x002F0749, /*1990-1999*/
  67. 0x00240693, 0x0418152B, 0x002B052B, 0x00200A5B, 0x0216155A, 0x0028056A, 0x071D1B55, 0x00310BA4, 0x00260B49, 0x051A1A93, /*2000-2009*/
  68. 0x002D0A95, 0x0022052D, 0x04170AAD, 0x00290AB5, 0x091F15AA, 0x003205D2, 0x00270DA5, 0x061C1D4A, 0x002F0D4A, 0x00240C95, /*2010-2019*/
  69. 0x0419152E, 0x002B0556, 0x00200AB5, 0x021615B2, 0x002906D2, 0x061D0EA5, 0x00300725, 0x0025064B, 0x051A0C97, 0x002C0CAB, /*2020-2029*/
  70. 0x0022055A, 0x03170AD6, 0x002A0B69, 0x0B1F1752, 0x00320B52, 0x00270B25, 0x061C1A4B, 0x002E0A4B, 0x002304AB, 0x0518055B, /*2030-2039*/
  71. 0x002B05AD, 0x00200B6A, 0x02161B52, 0x00290D92, 0x071E1D25, 0x00300D25, 0x00250A55, 0x051A14AD, 0x002D04B6, 0x002105B5, /*2040-2049*/
  72. 0x03170DAA, 0x002A0EC9, 0x08201E92, 0x00320E92, 0x00270D26, 0x061C0A56, 0x002E0A57, 0x00230556, 0x041806D5, 0x002B0755, /*2050-2059*/
  73. 0x00210749, 0x03150E93, 0x00280693, 0x071D152B, 0x0030052B, 0x00240A5B, 0x051A155A, 0x002D056A, 0x00220B65, 0x0417174A, /*2060-2069*/
  74. };
  75. //获取农历函数
  76. int getLunar(int year, int month, int day)
  77. {
  78. //获取给的年份 year 对应的农历数据
  79. unsigned int yLundarData = lunar_data[year - 1600];
  80. //获取 year 对应的农历闰月
  81. unsigned int   leapMonth = yLundarData >> 24;
  82. //获取给定年 春节 在该年的日序数(就是该年的 第几天)
  83. unsigned int springIndex = (yLundarData >> 16)&0xFF;
  84. //获取给定 查询日期 在该年的日序数(就是该年的 第几天)
  85. unsigned int dayIndex = month_accumulate[month - 1] + day;
  86. //阳历闰月二月加一天
  87. if(month > 2)
  88. {
  89. if
  90. (
  91.     ((year % 4 == 0) && (year % 100 != 0))||
  92.     (year % 400 == 0)
  93. )
  94. {
  95. dayIndex += 1;
  96. }
  97. }
  98. // 查询日期在春节前,需要调取上一年的农历数据,因此递归 year - 1, month + 12
  99. if(dayIndex < springIndex)
  100. {
  101. return getLunar(year - 1, month + 12, day);
  102. }
  103. //查询日期在春节后
  104. unsigned int bitArry = yLundarData;
  105. int s = springIndex;
  106. int i = 1;
  107. while(i <= 13)
  108. {
  109. s += (bitArry & 1) ?30 :29;
  110. i ++;
  111. if(s > dayIndex)
  112. {
  113. i -= 1;
  114. s -= (bitArry & 1) ?30 :29;
  115. if((0 < leapMonth) && (leapMonth < i))
  116. {
  117. i -= 1;
  118. if(i == leapMonth)
  119. {
  120. // 农历 (dayIndex + 1 - s) 中的+1 表示农历是从初一开始的,
  121. return -(i*100) - (dayIndex + 1 - s);
  122. }
  123. }
  124. return (i*100) + (dayIndex + 1 - s);
  125. }
  126. bitArry >>= 1;
  127. }
  128. return 0;
  129. }
  130. //MAIN主函数
  131. int main()
  132. {
  133. //获取当前日期
  134. time_t timep;
  135. time (&timep);
  136. struct tm* p = gmtime(&timep);
  137. int year = 1900 + p->tm_year;
  138. int month = 1 + p->tm_mon;
  139. int day = 1 + p->tm_mday ;
  140. year = 1900;
  141. month = 9;
  142. day   = 25;
  143. printf("%d %d %d\n", year, month, day);
  144. //计算天干地支属相
  145. int yTianGan = (year + 6) % 10;
  146. int yDiZhi = (year + 8) % 12;
  147. int yShuXiang = (year + 8) % 12;
  148. int lunVal = getLunar(year, month, day);
  149. if(lunVal != 0)
  150. {
  151. int lun = (lunVal >= 0) ?(lunVal) :(-lunVal);
  152. int lunMonth = lun / 100;
  153. int lunDay = lun % 100;
  154. 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]);
  155. }
  156. return 0;
  157. }
复制代码

作者: 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