├── .gitignore ├── LICENSE ├── README.md ├── SConscript ├── inc └── lunar_calendar.h ├── samples └── lunar_sample.c └── src └── lunar_calendar.c /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Object files 5 | *.o 6 | *.ko 7 | *.obj 8 | *.elf 9 | 10 | # Linker output 11 | *.ilk 12 | *.map 13 | *.exp 14 | 15 | # Precompiled Headers 16 | *.gch 17 | *.pch 18 | 19 | # Libraries 20 | *.lib 21 | *.a 22 | *.la 23 | *.lo 24 | 25 | # Shared objects (inc. Windows DLLs) 26 | *.dll 27 | *.so 28 | *.so.* 29 | *.dylib 30 | 31 | # Executables 32 | *.exe 33 | *.out 34 | *.app 35 | *.i*86 36 | *.x86_64 37 | *.hex 38 | 39 | # Debug files 40 | *.dSYM/ 41 | *.su 42 | *.idb 43 | *.pdb 44 | 45 | # Kernel Module Compile Results 46 | *.mod* 47 | *.cmd 48 | .tmp_versions/ 49 | modules.order 50 | Module.symvers 51 | Mkfile.old 52 | dkms.conf 53 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Illusion Lee 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # lunar_calendar 2 | 3 | ## 1、介绍 4 | 5 | **lunar_calendar** 是一个将公历日期转换成阴历的工具软件包。 6 | 7 | ### 1.1 目录结构 8 | 9 | | 名称 | 说明 | 10 | | ---- | ---- | 11 | | examples | 例子目录 | 12 | | inc | 头文件目录 | 13 | | src | 源代码目录 | 14 | 15 | ### 1.2 许可证 16 | 17 | `lunar_calendar` 软件包使用 `MIT` 软件包许可协议,请见 `lunar_calendar/LICENSE` 文件。 18 | 19 | ### 1.3 依赖 20 | 21 | - RT-Thread 3.0+ 22 | 23 | ## 2、如何打开 lunar_calendar 24 | 25 | 使用 lunar_calendar package 需要在 RT-Thread 的包管理器中选择它,具体路径如下: 26 | 27 | ```shell 28 | RT-Thread online packages 29 | tools packages ---> 30 | [*] lunar_calendar: A tool to convert a Gregorian calendar date into a lunar calendar. 31 | ``` 32 | 33 | 然后让 RT-Thread 的包管理器自动更新,或者使用 `pkgs --update` 命令更新包到 BSP 中。 34 | 35 | ## 3、使用 lunar_calendar 36 | 37 | 38 | 在使用 lunar_calendar 软件包时首先要定义一个`char`数组来存储返回的阴历汉字,如: 39 | 40 | ```c 41 | char str[32] = {'\0'}; 42 | ``` 43 | 44 | 调用的函数接口为: 45 | 46 | ```c 47 | void sun2lunar(int year, int month, int day, char *str_lunar); 48 | ``` 49 | 50 | 比如: 51 | 52 | ```c 53 | static showlunar(int argc, char const *argv[]) { 54 | if (argc == 4) { 55 | char str[32] = {'\0'}; 56 | sun2lunar(atoi(argv[1]), atoi(argv[2]), atoi(argv[3]), str); 57 | printf("%s\n", str); 58 | } 59 | return 0; 60 | } 61 | ``` 62 | 63 | ## 4、示例演示 64 | 65 | 在 MSH 中输入命令 `showlunar 2019 12 4`,可以在串口助手上看到输出了对应日期的阴历。 66 | 67 | ``` 68 | msh >showlunar 2019 12 4 69 | 冬月初九 70 | msh > 71 | ``` 72 | 73 | ## 5、注意事项 74 | 75 | - 当前可以生成阴历日期对应的阳历范围为 1900-1-1 到2099-12-31。 76 | 77 | ## 6、联系方式 & 感谢 78 | 79 | * 维护:[illusionlee](https://github.com/illusionlee) 80 | * 主页:https://github.com/illusionlee/lunar_calendar.git 81 | -------------------------------------------------------------------------------- /SConscript: -------------------------------------------------------------------------------- 1 | from building import * 2 | 3 | cwd = GetCurrentDir() 4 | 5 | src = Glob('src/*.c') 6 | 7 | if GetDepend(['PKG_USING_LUNAR_CALENDAR']): 8 | src += Glob('samples/*.c') 9 | 10 | CPPPATH = [cwd + '/inc'] 11 | 12 | group = DefineGroup('lunar_calendar', src, depend = ['PKG_USING_LUNAR_CALENDAR'], CPPPATH = CPPPATH) 13 | 14 | Return('group') 15 | -------------------------------------------------------------------------------- /inc/lunar_calendar.h: -------------------------------------------------------------------------------- 1 | /** 2 | * The MIT License 3 | * 4 | * Copyright (c) 2019 Illusion Lee 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #ifndef __LUNAR_CALENDAR_H_ 26 | #define __LUNAR_CALENDAR_H_ 27 | 28 | #ifdef __cplusplus 29 | extern "C" { 30 | #endif /* __cplusplus */ 31 | extern void sun2lunar(int year, int month, int day, char *str_lunar); 32 | #ifdef __cplusplus 33 | } 34 | #endif /* __cplusplus */ 35 | 36 | #endif /* __LUNAR_CALENDAR_H_ */ 37 | -------------------------------------------------------------------------------- /samples/lunar_sample.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "lunar_calendar.h" 3 | 4 | #if defined(RT_USING_FINSH) && defined(FINSH_USING_MSH) 5 | #include 6 | 7 | static const char *usage = " \n\ 8 | Welcome in showlunar. \n\ 9 | Usage: \n\ 10 | showlunar year month day \n\ 11 | e.g: \n\ 12 | showlunar 2019 12 4 \n\ 13 | \n"; 14 | static showlunar(int argc, char const *argv[]) { 15 | if (argc == 4) { 16 | char str[32] = {'\0'}; 17 | sun2lunar(atoi(argv[1]), atoi(argv[2]), atoi(argv[3]), str); 18 | printf("%s\n", str); 19 | } else { 20 | printf("%s\n", usage); 21 | 22 | } 23 | return 0; 24 | } 25 | 26 | MSH_CMD_EXPORT(showlunar, showlunar generator: showlunar [year] [month] [day]); 27 | #endif /* defined(RT_USING_FINSH) && defined(FINSH_USING_MSH) */ -------------------------------------------------------------------------------- /src/lunar_calendar.c: -------------------------------------------------------------------------------- 1 | /** 2 | * The MIT License 3 | * 4 | * Copyright (c) 2019 Illusion Lee 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include 26 | #include "lunar_calendar.h" 27 | 28 | const char *lunar_day[] = { 29 | "混沌", "初一", "初二", "初三", "初四", "初五", "初六", "初七", "初八", "初九", "初十", 30 | "十一", "十二", "十三", "十四", "十五", "十六", "十七", "十八", "十九", "二十", 31 | "廿一", "廿二", "廿三", "廿四", "廿五", "廿六", "廿七", "廿八", "廿九", "三十" 32 | }; 33 | const char *lunar_month[] = {"虚", "正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "冬", "腊"}; 34 | unsigned int lunar_calendar_table[199] = { 35 | /*1901-1910*/ 36 | 0x04AE53, 0x0A5748, 0x5526BD, 0x0D2650, 0x0D9544, 0x46AAB9, 0x056A4D, 0x09AD42, 0x24AEB6, 0x04AE4A, 37 | /*1911-1920*/ 38 | 0x6A4DBE, 0x0A4D52, 0x0D2546, 0x5D52BA, 0x0B544E, 0x0D6A43, 0x296D37, 0x095B4B, 0x749BC1, 0x049754, 39 | /*1921-1930*/ 40 | 0x0A4B48, 0x5B25BC, 0x06A550, 0x06D445, 0x4ADAB8, 0x02B64D, 0x095742, 0x2497B7, 0x04974A, 0x664B3E, 41 | /*1931-1940*/ 42 | 0x0D4A51, 0x0EA546, 0x56D4BA, 0x05AD4E, 0x02B644, 0x393738, 0x092E4B, 0x7C96BF, 0x0C9553, 0x0D4A48, 43 | /*1941-1950*/ 44 | 0x6DA53B, 0x0B554F, 0x056A45, 0x4AADB9, 0x025D4D, 0x092D42, 0x2C95B6, 0x0A954A, 0x7B4ABD, 0x06CA51, 45 | /*1951-1960*/ 46 | 0x0B5546, 0x555ABB, 0x04DA4E, 0x0A5B43, 0x352BB8, 0x052B4C, 0x8A953F, 0x0E9552, 0x06AA48, 0x6AD53C, 47 | /*1961-1970*/ 48 | 0x0AB54F, 0x04B645, 0x4A5739, 0x0A574D, 0x052642, 0x3E9335, 0x0D9549, 0x75AABE, 0x056A51, 0x096D46, 49 | /*1971-1980*/ 50 | 0x54AEBB, 0x04AD4F, 0x0A4D43, 0x4D26B7, 0x0D254B, 0x8D52BF, 0x0B5452, 0x0B6A47, 0x696D3C, 0x095B50, 51 | /*1981-1990*/ 52 | 0x049B45, 0x4A4BB9, 0x0A4B4D, 0xAB25C2, 0x06A554, 0x06D449, 0x6ADA3D, 0x0AB651, 0x093746, 0x5497BB, 53 | /*1991-2000*/ 54 | 0x04974F, 0x064B44, 0x36A537, 0x0EA54A, 0x86B2BF, 0x05AC53, 0x0AB647, 0x5936BC, 0x092E50, 0x0C9645, 55 | /*2001-2010*/ 56 | 0x4D4AB8, 0x0D4A4C, 0x0DA541, 0x25AAB6, 0x056A49, 0x7AADBD, 0x025D52, 0x092D47, 0x5C95BA, 0x0A954E, 57 | /*2011-2020*/ 58 | 0x0B4A43, 0x4B5537, 0x0AD54A, 0x955ABF, 0x04BA53, 0x0A5B48, 0x652BBC, 0x052B50, 0x0A9345, 0x474AB9, 59 | /*2021-2030*/ 60 | 0x06AA4C, 0x0AD541, 0x24DAB6, 0x04B64A, 0x69573D, 0x0A4E51, 0x0D2646, 0x5E933A, 0x0D534D, 0x05AA43, 61 | /*2031-2040*/ 62 | 0x36B537, 0x096D4B, 0xB4AEBF, 0x04AD53, 0x0A4D48, 0x6D25BC, 0x0D254F, 0x0D5244, 0x5DAA38, 0x0B5A4C, 63 | /*2041-2050*/ 64 | 0x056D41, 0x24ADB6, 0x049B4A, 0x7A4BBE, 0x0A4B51, 0x0AA546, 0x5B52BA, 0x06D24E, 0x0ADA42, 0x355B37, 65 | /*2051-2060*/ 66 | 0x09374B, 0x8497C1, 0x049753, 0x064B48, 0x66A53C, 0x0EA54F, 0x06B244, 0x4AB638, 0x0AAE4C, 0x092E42, 67 | /*2061-2070*/ 68 | 0x3C9735, 0x0C9649, 0x7D4ABD, 0x0D4A51, 0x0DA545, 0x55AABA, 0x056A4E, 0x0A6D43, 0x452EB7, 0x052D4B, 69 | /*2071-2080*/ 70 | 0x8A95BF, 0x0A9553, 0x0B4A47, 0x6B553B, 0x0AD54F, 0x055A45, 0x4A5D38, 0x0A5B4C, 0x052B42, 0x3A93B6, 71 | /*2081-2090*/ 72 | 0x069349, 0x7729BD, 0x06AA51, 0x0AD546, 0x54DABA, 0x04B64E, 0x0A5743, 0x452738, 0x0D264A, 0x8E933E, 73 | /*2091-2099*/ 74 | 0x0D5252, 0x0DAA47, 0x66B53B, 0x056D4F, 0x04AE45, 0x4A4EB9, 0x0A4D4C, 0x0D1541, 0x2D92B5 75 | }; 76 | unsigned int month_sum[12] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}; 77 | 78 | unsigned int lunar_calendar_day = 0; 79 | 80 | int lunar_calendar(int year, int month, int day) { 81 | int lunar_newyear; /* 春节离当年元旦的天数 */ 82 | int sun_newyear; /* 阳历日离当年元旦的天数 */ 83 | int days_of_month; /* 大小月的天数 29或30 */ 84 | int index; /* 从哪个月开始来计算 */ 85 | int flag; /* 对闰月特殊处理 */ 86 | 87 | if ( ((lunar_calendar_table[year - 1901] & 0x0060) >> 5) == 1) 88 | lunar_newyear = (lunar_calendar_table[year - 1901] & 0x001F) - 1; 89 | else 90 | lunar_newyear = (lunar_calendar_table[year - 1901] & 0x001F) - 1 + 31; 91 | sun_newyear = month_sum[month - 1] + day - 1; 92 | if ( (!(year % 4)) && (month > 2)) 93 | sun_newyear++; 94 | /* 判断阳历日在春节前还是春节后 */ 95 | if (sun_newyear >= lunar_newyear) { /* 阳历日在春节后(含春节那天) */ 96 | sun_newyear -= lunar_newyear; 97 | month = 1; 98 | index = 1; 99 | flag = 0; 100 | if ( ( lunar_calendar_table[year - 1901] & (0x80000 >> (index - 1)) ) == 0) 101 | days_of_month = 29; 102 | else 103 | days_of_month = 30; 104 | while (sun_newyear >= days_of_month) { 105 | sun_newyear -= days_of_month; 106 | index++; 107 | if (month == ((lunar_calendar_table[year - 1901] & 0xF00000) >> 20) ) { 108 | flag = ~flag; 109 | if (flag == 0) 110 | month++; 111 | } else { 112 | month++; 113 | } 114 | if ( ( lunar_calendar_table[year - 1901] & (0x80000 >> (index - 1)) ) == 0) 115 | days_of_month = 29; 116 | else 117 | days_of_month = 30; 118 | } 119 | day = sun_newyear + 1; 120 | } else { /* 阳历日在春节前 */ 121 | lunar_newyear -= sun_newyear; 122 | year--; 123 | month = 12; 124 | if ( ((lunar_calendar_table[year - 1901] & 0xF00000) >> 20) == 0) { 125 | index = 12; 126 | } else { 127 | index = 13; 128 | } 129 | flag = 0; 130 | if ( ( lunar_calendar_table[year - 1901] & (0x80000 >> (index - 1)) ) == 0) { 131 | days_of_month = 29; 132 | } else { 133 | days_of_month = 30; 134 | } 135 | while (lunar_newyear > days_of_month) { 136 | lunar_newyear -= days_of_month; 137 | index--; 138 | if (flag == 0) { 139 | month--; 140 | } 141 | if (month == ((lunar_calendar_table[year - 1901] & 0xF00000) >> 20)) { 142 | flag = ~flag; 143 | } 144 | if ( ( lunar_calendar_table[year - 1901] & (0x80000 >> (index - 1)) ) == 0) { 145 | days_of_month = 29; 146 | } else { 147 | days_of_month = 30; 148 | } 149 | } 150 | day = days_of_month - lunar_newyear + 1; 151 | } 152 | lunar_calendar_day = day; 153 | lunar_calendar_day |= (month << 6); 154 | if (month == ((lunar_calendar_table[year - 1901] & 0xF00000) >> 20)) { 155 | return flag; 156 | } else { 157 | return 0; 158 | } 159 | } 160 | 161 | void sun2lunar(int year, int month, int day, char *str_lunar) { 162 | if (lunar_calendar(year, month, day)) { 163 | strcat(str_lunar, "闰"); 164 | } 165 | strcat(str_lunar, lunar_month[(lunar_calendar_day & 0x3C0) >> 6]); 166 | strcat(str_lunar, "月"); 167 | strcat(str_lunar, lunar_day[lunar_calendar_day & 0x3F]); 168 | } 169 | --------------------------------------------------------------------------------