#include "StdAfx.h" #include "CalendarEx.h" #include //或者 #include #include namespace CalendarEx { ////////////////////////////////////////////////////////////////////////// // 全局定义; // const static int BEGIN_YEAR = 1901; // const static int NUMBER_YEAR = 199; #define BEGIN_YEAR 1901 #define NUMBER_YEAR 199 // 非闰年的月份天数; const static int nNormalMonth[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; // 闰年的月份天数; const static int nLeapMonth[12] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; // 为了提高效率,记录每月一日是一年中的第几天; // 平年的情况; const static int NORMAL_YDAY[13] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 }; // 闰年的情况; const static int LEAP_YDAY[13] = { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }; // 使用比特位记录每年的情况 // 0~4 共5bit 春节日份 // 5~6 共2bit 春节月份 // 7~19 共13bit 13个月的大小月情况(如果无闰月,最后位无效),大月为1(30天),小月为0(29天) // 20~23 共4bit 记录闰月的月份,如果没有闰月为0 ////////////////////////////////////////////////////////////////////////// // 示例 //0x955ABF 2014年, 农9月有闰月, 公历1月31号为春节; // 1001 0101010110101 01 11111 // 20~23bit.闰月月份 7-19.13个月 5~6bit.春月 0~4bit.春节日期; ////1001 0101010110101 01 11111 //0X252ABF //1001010010101010111111 const static DWORD LUNAR_YEARS[199] = { 0x04AE53, 0x0A5748, 0x5526BD, 0x0D2650, 0x0D9544, 0x46AAB9, 0x056A4D, 0x09AD42, 0x24AEB6, 0x04AE4A, //1901-1910 0x6A4DBE, 0x0A4D52, 0x0D2546, 0x5D52BA, 0x0B544E, 0x0D6A43, 0x296D37, 0x095B4B, 0x749BC1, 0x049754, //1911-1920 0x0A4B48, 0x5B25BC, 0x06A550, 0x06D445, 0x4ADAB8, 0x02B64D, 0x095742, 0x2497B7, 0x04974A, 0x664B3E, //1921-1930 0x0D4A51, 0x0EA546, 0x56D4BA, 0x05AD4E, 0x02B644, 0x393738, 0x092E4B, 0x7C96BF, 0x0C9553, 0x0D4A48, //1931-1940 0x6DA53B, 0x0B554F, 0x056A45, 0x4AADB9, 0x025D4D, 0x092D42, 0x2C95B6, 0x0A954A, 0x7B4ABD, 0x06CA51, //1941-1950 0x0B5546, 0x555ABB, 0x04DA4E, 0x0A5B43, 0x352BB8, 0x052B4C, 0x8A953F, 0x0E9552, 0x06AA48, 0x7AD53C, //1951-1960 0x0AB54F, 0x04B645, 0x4A5739, 0x0A574D, 0x052642, 0x3E9335, 0x0D9549, 0x75AABE, 0x056A51, 0x096D46, //1961-1970 0x54AEBB, 0x04AD4F, 0x0A4D43, 0x4D26B7, 0x0D254B, 0x8D52BF, 0x0B5452, 0x0B6A47, 0x696D3C, 0x095B50, //1971-1980 0x049B45, 0x4A4BB9, 0x0A4B4D, 0xAB25C2, 0x06A554, 0x06D449, 0x6ADA3D, 0x0AB651, 0x093746, 0x5497BB, //1981-1990 0x04974F, 0x064B44, 0x36A537, 0x0EA54A, 0x86B2BF, 0x05AC53, 0x0AB647, 0x5936BC, 0x092E50, 0x0C9645, //1991-2000 0x4D4AB8, 0x0D4A4C, 0x0DA541, 0x25AAB6, 0x056A49, 0x7AADBD, 0x025D52, 0x092D47, 0x5C95BA, 0x0A954E, //2001-2010 0x0B4A43, 0x4B5537, 0x0AD54A, 0x955ABF, 0x04BA53, 0x0A5B48, 0x652BBC, 0x052B50, 0x0A9345, 0x474AB9, //2011-2020 0x06AA4C, 0x0AD541, 0x24DAB6, 0x04B64A, 0x69573D, 0x0A4E51, 0x0D2646, 0x5E933A, 0x0D534D, 0x05AA43, //2021-2030 0x36B537, 0x096D4B, 0xB4AEBF, 0x04AD53, 0x0A4D48, 0x6D25BC, 0x0D254F, 0x0D5244, 0x5DAA38, 0x0B5A4C, //2031-2040 0x056D41, 0x24ADB6, 0x049B4A, 0x7A4BBE, 0x0A4B51, 0x0AA546, 0x5B52BA, 0x06D24E, 0x0ADA42, 0x355B37, //2041-2050 0x09374B, 0x8497C1, 0x049753, 0x064B48, 0x66A53C, 0x0EA54F, 0x06B244, 0x4AB638, 0x0AAE4C, 0x092E42, //2051-2060 0x3C9735, 0x0C9649, 0x7D4ABD, 0x0D4A51, 0x0DA545, 0x55AABA, 0x056A4E, 0x0A6D43, 0x452EB7, 0x052D4B, //2061-2070 0x8A95BF, 0x0A9553, 0x0B4A47, 0x6B553B, 0x0AD54F, 0x055A45, 0x4A5D38, 0x0A5B4C, 0x052B42, 0x3A93B6, //2071-2080 0x069349, 0x7729BD, 0x06AA51, 0x0AD546, 0x54DABA, 0x04B64E, 0x0A5743, 0x452738, 0x0D264A, 0x8E933E, //2081-2090 0x0D5252, 0x0DAA47, 0x66B53B, 0x056D4F, 0x04AE45, 0x4A4EB9, 0x0A4D4C, 0x0D1541, 0x2D92B5 //2091-2099 }; /************************************************************************/ /* 函数:IsValidLunanrDate[2/23/2017 Jeff]; /* 描述:农历日期有效性; /* 参数:; /* [IN] lpLunarDate:输入的农历日期; /* 返回:有效的农历日期返回true,否则返回false; /* 注意:lpLunarDate的日期格式必须是:yyyy/mm/dd 或 yyyy-mm-dd; /* 示例:; /* /* 修改:; /* 日期:; /* 内容:; /************************************************************************/ bool IsValidLunanrDate(IN const TCHAR* lpLunarDate) { if (lpLunarDate == NULL || _tcslen(lpLunarDate) != 10 ) return false; CString strLunarDate = lpLunarDate; if (strLunarDate.IsEmpty()) return false; static int nLunarYear, nLunarMonth, nLunarDay; nLunarYear = nLunarMonth = nLunarDay = 0; nLunarYear = _ttoi(strLunarDate.Left(4)); nLunarMonth = _ttoi(strLunarDate.Mid(5, 2)); nLunarDay = _ttoi(strLunarDate.Mid(8, 2)); //越界检查,如果越界,返回无效日期; if (nLunarYear <= BEGIN_YEAR || nLunarYear > BEGIN_YEAR + NUMBER_YEAR - 1) return false; if (nLunarMonth < 1 || nLunarMonth > 12) return false; // 1.农历年是否有闰月; int nYearIndex = nLunarYear - BEGIN_YEAR; int nLeapMonth = (LUNAR_YEARS[nYearIndex] >> 20) & 0xF; // 2.月份大小月天数; int nMonthDays = 0; if ((LUNAR_YEARS[nYearIndex] >> (7 + 13 - nLunarMonth)) & 0x1) nMonthDays = 30; else nMonthDays = 29; if (nLunarDay < 0 || nLunarDay > nMonthDays) return false; return true; } /************************************************************************/ /* 函数:IsValidLunanrDate[2/23/2017 Jeff]; /* 描述:农历日期有效性; /* 参数:; /* [IN] nLunarYear:输入的农历年; /* [IN] nLunarMonth:输入的农历月; /* [IN] nLunarDay:输入的农历日; /* 返回:有效的农历日期返回true,否则返回false; /* 注意:; /* 示例:; /* /* 修改:; /* 日期:; /* 内容:; /************************************************************************/ bool IsValidLunanrDate(IN const int &nLunarYear, IN const int &nLunarMonth, IN const int &nLunarDay) { //越界检查,如果越界,返回无效日期; if (nLunarYear <= BEGIN_YEAR || nLunarYear > BEGIN_YEAR + NUMBER_YEAR - 1) return false; if (nLunarMonth < 1 || nLunarMonth > 12) return false; // 1.农历年是否有闰月; int nYearIndex = nLunarYear - BEGIN_YEAR; int nLeapMonth = (LUNAR_YEARS[nYearIndex] >> 20) & 0xF; // 2.月份大小月天数; int nMonthDays = 0; if ((LUNAR_YEARS[nYearIndex] >> (7 + 13 - nLunarMonth)) & 0x1) nMonthDays = 30; else nMonthDays = 29; if (nLunarDay < 0 || nLunarDay > nMonthDays) return false; return true; } /************************************************************************/ /* 函数:IsValidSolarDate[2/23/2017 Jeff]; /* 描述:判断公历日期是否有效; /* 参数:; /* [IN] lpSolarDate:输入的公历日期; /* 返回:有效的公历日期返回true,否则返回false; /* 注意:lpSolarDate的日期格式必须是:yyyy/mm/dd 或 yyyy-mm-dd; /* 示例:; /* /* 修改:; /* 日期:; /* 内容:; /************************************************************************/ bool IsValidSolarDate(IN const TCHAR* lpSolarDate) { if (lpSolarDate == NULL || _tcslen(lpSolarDate) != 10) return false; CString strSolarDate = lpSolarDate; // 判断日期合法性; static int nSolarYear = 0; static int nSolarMonth = 0; static int nSolarDay = 0; nSolarYear = nSolarMonth = nSolarDay = 0; nSolarYear = _ttoi(strSolarDate.Left(4)); nSolarMonth = _ttoi(strSolarDate.Mid(5, 2)); nSolarDay = _ttoi(strSolarDate.Mid(8, 2)); if ((nSolarDay <= 0) || (nSolarDay > 31) || (nSolarMonth <= 0) || (nSolarMonth > 12)) return false; if ((nSolarYear % 400 == 0) || (nSolarYear % 4 == 0 && nSolarYear % 100 != 0)) { if (nSolarDay > nLeapMonth[nSolarMonth - 1]) return false; } else { if (nSolarDay > nNormalMonth[nSolarMonth - 1]) return false; } return true; } /************************************************************************/ /* 函数:IsValidSolarDate[2/23/2017 Jeff]; /* 描述:判断公历日期是否有效; /* 参数:; /* [IN] nSolarYear:输入的公历年; /* [IN] nSolarMonth:输入的公历月; /* [IN] nSolarDay:输入的公历日; /* 返回:若公历日期有效返回true,否则返回false; /* 注意:; /* 示例:; /* /* 修改:; /* 日期:; /* 内容:; /************************************************************************/ bool IsValidSolarDate(IN const int &nSolarYear, IN const int &nSolarMonth, IN const int &nSolarDay) { if (nSolarYear < 0 ) return false; // 判断日期合法性; if ((nSolarDay <= 0) || (nSolarDay > 31) || (nSolarMonth <= 0) || (nSolarMonth > 12)) return false; if ((nSolarYear % 400 == 0) || (nSolarYear % 4 == 0 && nSolarYear % 100 != 0)) { if (nSolarDay > nLeapMonth[nSolarMonth - 1]) return false; } else { if (nSolarDay > nNormalMonth[nSolarMonth - 1]) return false; } return true; } /************************************************************************/ /* 函数:DayOfSolarYear[2/23/2017 Jeff]; /* 描述:计算这个公历日期是这一年中的第几天; /* 参数:; /* [IN] nSolarYear:输入的公历年; /* [IN] nSolarMonth:输入的公历月; /* [IN] nSolarDay:输入的公历日; /* 返回:如果日期正确,返回公历日期在这一年中的第几天; 否则返回-1; /* 注意:; /* 示例:; /* /* 修改:; /* 日期:; /* 内容:; /************************************************************************/ int DayOfSolarYear(IN const int &nSolarYear,IN const int &nSolarMonth, IN const int &nSolarDay) { if( !IsValidSolarDate(nSolarYear,nSolarMonth,nSolarDay) ) return -1; const int *nDaysYear = NORMAL_YDAY; //判断是否是公历闰年; if (nSolarYear % 4 == 0) { if (nSolarYear % 100 != 0) nDaysYear = LEAP_YDAY; if (nSolarYear % 400 == 0) nDaysYear = LEAP_YDAY; } return nDaysYear[nSolarMonth - 1] + nSolarDay; } /************************************************************************/ /* 函数:SolarToLuanrDate[2/23/2017 Jeff]; /* 描述:将公历日期转为对应的农历日期; /* 参数:; /* [IN] nSolarYear:输入的公历年; /* [IN] nSolarMonth:输入的公历月; /* [IN] nSolarDay:输入的公历日; /* [OUT] lnaDate:公历日期有效转换的农历日期; /* 返回:日期有效且转换成功返回true; /* 注意:; /* 示例:; /* /* 修改:; /* 日期:; /* 内容:; /************************************************************************/ bool SolarToLuanrDate(IN const int &nSolarYear,IN const int &nSolarMonth, IN const int &nSolarDay, OUT LunarDate &lnaDate ) { if( !IsValidSolarDate(nSolarYear,nSolarMonth,nSolarDay) ) return false; lnaDate.nYear = nSolarYear; // 越界检查,如果越界,返回无效日期; if (nSolarYear <= BEGIN_YEAR || nSolarYear > BEGIN_YEAR + NUMBER_YEAR - 1) return false; int nIndexYear = nSolarYear - BEGIN_YEAR; ////////////////////////////////////////////////////////////////////////// // 计算春节的公历日期; // 春节:Spring Festival 或者 Chinese new Year; // 春节的公历月份; int nSpringMonth = (LUNAR_YEARS[nIndexYear] & 0x60) >> 5; // 春节的公历日号; int nSpringDay = (LUNAR_YEARS[nIndexYear] & 0x1F); // 计算今天是公历年的第几天; int nTodayInSolarYear = DayOfSolarYear(nSolarYear, nSolarMonth, nSolarDay); if( nTodayInSolarYear == -1 ) return false; // 计算春节是公历年的第几天; int nSpringDayInSolarYear = DayOfSolarYear(nSolarYear, nSpringMonth, nSpringDay); if( nSpringDayInSolarYear == -1 ) return false; // 计算今天是农历年的第几天 = 农历今天在的公历第几天 - 今年农历春节在公历的第几天; int nTodayInLunarYear = nTodayInSolarYear - nSpringDayInSolarYear + 1; // 如果今天在春节的前面,重新计算today_luanr_yd; if ( nTodayInLunarYear <= 0 ) { // 农历年比当前公历年小1; nIndexYear--; lnaDate.nYear--; // 越界,返回无效日期; if (nIndexYear < 0) return false; // 去年农历春节月; nSpringMonth = (LUNAR_YEARS[nIndexYear] & 0x60) >> 5; // 去年农历春节日; nSpringDay = (LUNAR_YEARS[nIndexYear] & 0x1F); // 去年公历年共多少天; int nDaysSolarYear = DayOfSolarYear(lnaDate.nYear, 12, 31); if( nDaysSolarYear == -1) return false; nSpringDayInSolarYear = DayOfSolarYear(nSolarYear, nSpringMonth, nSpringDay); if(nSpringDayInSolarYear == -1) return false; // 计算今天是农历年的第几天; nTodayInLunarYear = nTodayInSolarYear + nDaysSolarYear - nSpringDayInSolarYear + 1; } int nLunarMonth = 1; // 计算月份和日期; for (; nLunarMonth <= 13; nLunarMonth++) { int nMonthDays = 29; if ((LUNAR_YEARS[nIndexYear] >> (7 + 13 - nLunarMonth)) & 0x1) nMonthDays = 30; if (nTodayInLunarYear <= nMonthDays) break; else nTodayInLunarYear -= nMonthDays; } lnaDate.nDay = nTodayInLunarYear; // 处理闰月; int nLeapMonth = (LUNAR_YEARS[nIndexYear] >> 20) & 0xf; if (nLeapMonth > 0 && nLeapMonth < nLunarMonth) { nLunarMonth--; // 如果当前月为闰月,设置闰月标志; if (nLunarMonth == nLeapMonth) lnaDate.bIsLeapMonth = true; } if ( nLeapMonth > 12 ) return false; lnaDate.nMonth = nLunarMonth; return true; } /************************************************************************/ /* 函数:SolarToLuanrDate[2/23/2017 Jeff]; /* 描述:将公历日期转为对应的农历日期; /* 参数:; /* [IN] nSolarYear:输入的公历年; /* [IN] nSolarMonth:输入的公历月; /* [IN] nSolarDay:输入的公历日; /* [OUT] lpLunarDate:公历日期有效转换的农历日期; /* 返回:日期有效且转换成功返回true; /* 注意:; /* 示例:; /* /* 修改:; /* 日期:; /* 内容:; /************************************************************************/ bool SolarToLuanrDate(IN const int &nSolarYear, IN const int &nSolarMonth, IN const int &nSolarDay,OUT TCHAR* lpLunarDate) { if( !IsValidSolarDate(nSolarYear,nSolarMonth,nSolarDay) ) return false; LunarDate lnaDate; lnaDate.nYear = nSolarYear; lnaDate.nMonth = 0; lnaDate.nDay = 0; lnaDate.bIsLeapMonth = false; // 越界检查,如果越界,返回无效日期; if ( nSolarYear <= BEGIN_YEAR || nSolarYear > BEGIN_YEAR + NUMBER_YEAR - 1 ) return false; int nIndexYear = nSolarYear - BEGIN_YEAR; ////////////////////////////////////////////////////////////////////////// // 计算春节的公历日期; // 春节:Spring Festival 或者 Chinese new Year; // 春节的公历月份; int nSpringMonth = (LUNAR_YEARS[nIndexYear] & 0x60) >> 5; // 春节的公历日号; int nSpringDay = (LUNAR_YEARS[nIndexYear] & 0x1F); // 计算今天是公历年的第几天; int nTodayInSolarYear = DayOfSolarYear(nSolarYear, nSolarMonth, nSolarDay); if ( nTodayInSolarYear == -1 ) return false; // 计算春节是公历年的第几天; int nSpringDayInSolarYear = DayOfSolarYear(nSolarYear, nSpringMonth, nSpringDay); if ( nSpringDayInSolarYear == -1 ) return false; // 计算今天是农历年的第几天 = 农历今天在的公历第几天 - 今年农历春节在公历的第几天; int nTodayInLunarYear = nTodayInSolarYear - nSpringDayInSolarYear + 1; // 如果今天在春节的前面,重新计算today_luanr_yd; if (nTodayInLunarYear <= 0) { // 农历年比当前公历年小1; nIndexYear--; lnaDate.nYear--; // 越界,返回无效日期; if (nIndexYear < 0) return false; // 去年农历春节月; nSpringMonth = (LUNAR_YEARS[nIndexYear] & 0x60) >> 5; // 去年农历春节日; nSpringDay = (LUNAR_YEARS[nIndexYear] & 0x1F); // 去年公历年共多少天; int nDaysSolarYear = DayOfSolarYear(lnaDate.nYear, 12, 31); if (nDaysSolarYear == -1) return false; nSpringDayInSolarYear = DayOfSolarYear(nSolarYear, nSpringMonth, nSpringDay); if (nSpringDayInSolarYear == -1) return false; // 计算今天是农历年的第几天; nTodayInLunarYear = nTodayInSolarYear + nDaysSolarYear - nSpringDayInSolarYear + 1; } int nLunarMonth = 1; //计算月份和日期; for (; nLunarMonth <= 13; nLunarMonth++) { int nMonthDays = 29; if ((LUNAR_YEARS[nIndexYear] >> (7 + 13 - nLunarMonth)) & 0x1) nMonthDays = 30; if (nTodayInLunarYear <= nMonthDays) break; else nTodayInLunarYear -= nMonthDays; } lnaDate.nDay = nTodayInLunarYear; //处理闰月; int nLeapMonth = (LUNAR_YEARS[nIndexYear] >> 20) & 0xf; if (nLeapMonth > 0 && nLeapMonth < nLunarMonth) { nLunarMonth--; //如果当前月为闰月,设置闰月标志; if (nLunarMonth == nLeapMonth) lnaDate.bIsLeapMonth = true; } if ( nLeapMonth > 12) return false; lnaDate.nMonth = nLunarMonth; _stprintf_s(lpLunarDate, 11, _T("%04d-%02d-%02d"),lnaDate.nYear,lnaDate.nMonth,lnaDate.nDay); return true; } /************************************************************************/ /* 函数:LuanrToSolarDate[2/23/2017 Jeff]; /* 描述:将农历日期转为对应的公历日期; /* 参数:; /* [IN] nLunarYear:输入的农历年; /* [IN] nLunarMonth:输入的农历月; /* [IN] nLunarDay:输入的农历日; /* [OUT] lpSolarDate:农历日期有效转换的公历日期; /* [IN] bIsLeapMonth:若true,若该年公历有闰月且正好是输入月份,返回闰月对应的公历; /* 返回:转换公历成功返回true; /* 注意:2016-12-20转公历时出错,未纠正;2018-02-29转公历同样出错; /* 示例:; /* /* 修改:; /* 日期:; /* 内容:; /************************************************************************/ bool LuanrToSolarDate(IN const int &nLunarYear, IN const int &nLunarMonth, IN const int &nLunarDay, OUT TCHAR* lpSolarDate, IN bool bIsLeapMonth/*=false*/) { // 越界检查,如果越界,返回无效日期; if (nLunarYear <= BEGIN_YEAR || nLunarYear > BEGIN_YEAR + NUMBER_YEAR - 1) return false; // 1.判断该农历年是否有闰月; int nIndexYear = nLunarYear - BEGIN_YEAR; int nLeapMonth = (LUNAR_YEARS[nIndexYear] >> 20) & 0xF; int nDays = 0; long nMonths = (LUNAR_YEARS[nIndexYear] & 0x0FFF80) >> 7; std::bitset<13> btMonth(nMonths); for ( int i = 0; i < nLunarMonth-1; i++ ) { nDays += btMonth[i] ? 30 : 29; } if ( bIsLeapMonth && nLeapMonth != 0 ) { nDays += btMonth[nLunarMonth-1] ? 30 : 29; } nDays += nLunarDay; // 公历年是否是闰年; bool isLeapYear = false; if ((nLunarYear % 400 == 0) || (nLunarYear % 4 == 0 && nLunarYear % 100 != 0)) { isLeapYear = true; } else { isLeapYear = false; } // 正月的公历月份; int SolarNewYearMonth = (LUNAR_YEARS[nIndexYear] & 0x000060) >> 5; // 正月初一的公历日期; int SolarNewYearDay = LUNAR_YEARS[nIndexYear] & 0x00001F; int nSolarDay = 0; int nSolarDays = 0; int nSolarMonth = -1; int nSolarYear = nLunarYear; if ( isLeapYear ) { nSolarDays = LEAP_YDAY[SolarNewYearMonth-1] + SolarNewYearDay + nDays; for ( int i = SolarNewYearMonth; i < 12; i++) { if ( nSolarDays >= LEAP_YDAY[i] && nSolarDays < LEAP_YDAY[i+1]) { nSolarMonth = i+1; nSolarDay = nSolarDays - LEAP_YDAY[i] -1; } } if ( nSolarMonth == -1 ) nSolarDays -= LEAP_YDAY[12]; } else { nSolarDays = NORMAL_YDAY[SolarNewYearMonth-1]+ SolarNewYearDay + nDays; for ( int i = SolarNewYearMonth; i < 12; i++) { if ( nSolarDays >= NORMAL_YDAY[i] && nSolarDays < NORMAL_YDAY[i+1]) { nSolarMonth = i+1; nSolarDay = nSolarDays - NORMAL_YDAY[i] -1; } } if ( nSolarMonth == -1 ) nSolarDays -= NORMAL_YDAY[12]; } if ( nSolarMonth == -1 ) {// 超出本年; nSolarYear = nLunarYear+1 ; if ((nSolarYear % 400 == 0) || (nSolarYear % 4 == 0 && nSolarYear % 100 != 0)) { isLeapYear = true; } else { isLeapYear = false; } if ( isLeapYear ) { for ( int i = 0; i < 12; i++) { if ( nSolarDays >= LEAP_YDAY[i] && nSolarDays < LEAP_YDAY[i+1]) { nSolarMonth = i+1; nSolarDay = nSolarDays - LEAP_YDAY[i] -1; } } } else { for ( int i = 0; i < 12; i++) { if ( nSolarDays >= NORMAL_YDAY[i] && nSolarDays < NORMAL_YDAY[i+1]) { nSolarMonth = i+1; nSolarDay = nSolarDays - NORMAL_YDAY[i] -1; } } } } _stprintf_s(lpSolarDate, 11, _T("%4d-%02d-%02d"), nSolarYear, nSolarMonth, nSolarDay); return true; } bool LuanrToSolarDate(IN const int &nLunarYear, IN const int &nLunarMonth, IN const int &nLunarDay, OUT LunarDate &lnaDate, IN bool bIsLeapMonth/*=false*/) { // 越界检查,如果越界,返回无效日期; if (nLunarYear <= BEGIN_YEAR || nLunarYear > BEGIN_YEAR + NUMBER_YEAR - 1) return false; // 1.判断该农历年是否有闰月; int nIndexYear = nLunarYear - BEGIN_YEAR; int nLeapMonth = (LUNAR_YEARS[nIndexYear] >> 20) & 0xF; int nDays = 0; long nMonths = (LUNAR_YEARS[nIndexYear] & 0x0FFF80) >> 7; std::bitset<13> btMonth(nMonths); for ( int i = 0; i < nLunarMonth-1; i++ ) { nDays += btMonth[i] ? 30 : 29; } if ( bIsLeapMonth && nLeapMonth != 0 ) { nDays += btMonth[nLunarMonth-1] ? 30 : 29; } nDays += nLunarDay; // 公历年是否是闰年; bool isLeapYear = false; if ((nLunarYear % 400 == 0) || (nLunarYear % 4 == 0 && nLunarYear % 100 != 0)) { isLeapYear = true; } else { isLeapYear = false; } // 正月的公历月份; int SolarNewYearMonth = (LUNAR_YEARS[nIndexYear] & 0x000060) >> 5; // 正月初一的公历日期; int SolarNewYearDay = LUNAR_YEARS[nIndexYear] & 0x00001F; int nSolarDay = 0; int nSolarDays = 0; int nSolarYear = nLunarYear; int nSolarMonth = -1; if ( isLeapYear ) { nSolarDays = LEAP_YDAY[SolarNewYearMonth-1] + SolarNewYearDay + nDays; for ( int i = SolarNewYearMonth; i < 12; i++) { if ( nSolarDays >= LEAP_YDAY[i] && nSolarDays < LEAP_YDAY[i+1]) { nSolarMonth = i+1; nSolarDay = nSolarDays - LEAP_YDAY[i] -1; } } if ( nSolarMonth == -1 ) nSolarDays -= LEAP_YDAY[12]; } else { nSolarDays = NORMAL_YDAY[SolarNewYearMonth-1]+ SolarNewYearDay + nDays; for ( int i = SolarNewYearMonth; i < 12; i++) { if ( nSolarDays >= NORMAL_YDAY[i] && nSolarDays < NORMAL_YDAY[i+1]) { nSolarMonth = i+1; nSolarDay = nSolarDays - NORMAL_YDAY[i] -1; } } if ( nSolarMonth == -1 ) nSolarDays -= NORMAL_YDAY[12]; } if ( nSolarMonth == -1 ) {// 超出本年; nSolarYear = nLunarYear+1 ; if ((nSolarYear % 400 == 0) || (nSolarYear % 4 == 0 && nSolarYear % 100 != 0)) { isLeapYear = true; } else { isLeapYear = false; } if ( isLeapYear ) { for ( int i = 0; i < 12; i++) { if ( nSolarDays >= LEAP_YDAY[i] && nSolarDays < LEAP_YDAY[i+1]) { nSolarMonth = i+1; nSolarDay = nSolarDays - LEAP_YDAY[i] -1; } } } else { for ( int i = 0; i < 12; i++) { if ( nSolarDays >= NORMAL_YDAY[i] && nSolarDays < NORMAL_YDAY[i+1]) { nSolarMonth = i+1; nSolarDay = nSolarDays - NORMAL_YDAY[i] -1; } } } } lnaDate.nYear = nSolarYear; lnaDate.nMonth = nSolarMonth; lnaDate.nDay = nSolarDay; return true; } /************************************************************************/ /* 函数:[1/17/2018 Jeff]; /* 描述:给农历增加天数; /* 参数:; /* [IN] :; /* [OUT] :; /* [IN/OUT] :; /* 返回:void; /* 注意:; /* 示例:; /* /* 修改:; /* 日期:; /* 内容:; /************************************************************************/ void SolarDateAddDay(struct tm &tmSolar, int nDays = 1) { // 参数校验; if ((tmSolar.tm_mday <= 0) || (tmSolar.tm_mday > 31) || (tmSolar.tm_mon < 0) || (tmSolar.tm_mon > 11)) return ; if ((tmSolar.tm_year % 400 == 0) || (tmSolar.tm_year % 4 == 0 && tmSolar.tm_year % 100 != 0)) {// 如果是闰年; int nDValue = nLeapMonth[tmSolar.tm_mon - 1] - tmSolar.tm_mday - nDays; if (nDValue < 0) { // 月份加1; if ( tmSolar.tm_mon != 11 ) { ++tmSolar.tm_mon; tmSolar.tm_mday -= nDValue; } else {// 年月加1,月份和天数变成1; ++tmSolar.tm_year; tmSolar.tm_mon = 1; tmSolar.tm_mday = 1; } } } else { if (tmSolar.tm_mday > nNormalMonth[tmSolar.tm_mon - 1]) return; } } /************************************************************************/ /* 函数:[2/23/2017 Jeff]; /* 描述:判断输入的农历日期的月份是否有闰月; /* 参数:; /* [IN] nLunarYear:农历年; /* [OUT] :; /* [IN/OUT] :; /* 返回:若有闰月返回闰月值,否则返回0; /* 注意:; /* 示例:; /* /* 修改:; /* 日期:; /* 内容:; /************************************************************************/ int IsLeapMonth(IN const int &nLunarYear) { // 越界检查,如果越界,返回无效日期; if (nLunarYear <= BEGIN_YEAR || nLunarYear > BEGIN_YEAR + NUMBER_YEAR - 1) return -1; // 1.判断该农历年是否有闰月; return (LUNAR_YEARS[nLunarYear - BEGIN_YEAR] >> 20) & 0xF; } /************************************************************************/ /* 函数:获取年龄[2/23/2017 Jeff]; /* 描述:; /* 参数:; /* [IN] lpLunarBirthDay:公历生日; /* [OUT] :; /* [IN/OUT] :; /* 返回:void; /* 注意:2014-11-22农历生日, 在2018-; /* 示例:; /* /* 修改:; /* 日期:; /* 内容:; /************************************************************************/ int GetAge(IN const TCHAR* lpLunarBirthDay, IN bool bIsLeapMonth /*= false*/) { if ( !IsValidLunanrDate(lpLunarBirthDay) ) return -1; // 1.将农历转成公历; #ifdef USE_MFC CString strLunarDate = lpLunarBirthDay; int nLunarYear = _ttoi(strLunarDate.Left(4)); int nLunarMonth = _ttoi(strLunarDate.Mid(5, 2)); int nLunarDay = _ttoi(strLunarDate.Mid(8, 2)); #else #ifdef UNICODE std::wstring strLunarDate = lpLunarBirthDay; #else std::string strLunarDate = lpLunarBirthDay; #endif int nLunarYear = _ttoi(strLunarDate.substr(0,4).c_str()); int nLunarMonth = _ttoi(strLunarDate.substr(5, 2).c_str()); int nLunarDay = _ttoi(strLunarDate.substr(8, 2).c_str()); #endif // 获取今年年份; __time64_t gmt = time(NULL);// 获取当前日历时间(1900-01-01开始的Unix时间戳); struct tm gmtm = {0}; //gmtime_s(&gmtm, &gmt); // 时间戳转成UTC时间(也叫GMT时间); localtime_s(&gmtm, &gmt); // 时间戳转成本地时间; // 获取今天的农历日期; LunarDate lunarToday; if ( SolarToLuanrDate(gmtm.tm_year + 1900, gmtm.tm_mon+1, gmtm.tm_mday, lunarToday) ) { LunarDate solarBirthday; if ( LuanrToSolarDate(lunarToday.nYear, nLunarMonth, nLunarDay, solarBirthday, bIsLeapMonth) ) { struct tm bdtm = {0}; bdtm.tm_year = solarBirthday.nYear - 1900; bdtm.tm_mon = solarBirthday.nMonth - 1; bdtm.tm_mday = solarBirthday.nDay; __time64_t bdgmt = _mkgmtime64(&bdtm); if (bdgmt >= gmt )// 不满周年; return lunarToday.nYear - nLunarYear - 1; else// 满周年; return lunarToday.nYear - nLunarYear; } } return -1; } };