mirror of
https://github.com/tsl0922/EPD-nRF5.git
synced 2025-12-06 15:42:48 +08:00
clang-format code
This commit is contained in:
364
GUI/Lunar.c
364
GUI/Lunar.c
@@ -1,32 +1,22 @@
|
||||
#include "Lunar.h"
|
||||
|
||||
const char Lunar_MonthString[13][7] = {
|
||||
"----",
|
||||
"正月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月",
|
||||
"冬月", "腊月"};
|
||||
const char Lunar_MonthString[13][7] = {"----", "正月", "二月", "三月", "四月", "五月", "六月",
|
||||
"七月", "八月", "九月", "十月", "冬月", "腊月"};
|
||||
|
||||
const char Lunar_MonthLeapString[2][4] = {
|
||||
" ",
|
||||
"闰"};
|
||||
const char Lunar_MonthLeapString[2][4] = {" ", "闰"};
|
||||
|
||||
const char Lunar_DateString[31][7] = {
|
||||
"----",
|
||||
"初一", "初二", "初三", "初四", "初五", "初六", "初七", "初八", "初九", "初十",
|
||||
"十一", "十二", "十三", "十四", "十五", "十六", "十七", "十八", "十九", "二十",
|
||||
"廿一", "廿二", "廿三", "廿四", "廿五", "廿六", "廿七", "廿八", "廿九", "三十"};
|
||||
const char Lunar_DateString[31][7] = {"----", "初一", "初二", "初三", "初四", "初五", "初六", "初七",
|
||||
"初八", "初九", "初十", "十一", "十二", "十三", "十四", "十五",
|
||||
"十六", "十七", "十八", "十九", "二十", "廿一", "廿二", "廿三",
|
||||
"廿四", "廿五", "廿六", "廿七", "廿八", "廿九", "三十"};
|
||||
|
||||
const char Lunar_DayString[7][4] = {
|
||||
"日",
|
||||
"一", "二", "三", "四", "五", "六"};
|
||||
const char Lunar_DayString[7][4] = {"日", "一", "二", "三", "四", "五", "六"};
|
||||
|
||||
const char Lunar_ZodiacString[12][4] = {
|
||||
"猴", "鸡", "狗", "猪", "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊"};
|
||||
const char Lunar_ZodiacString[12][4] = {"猴", "鸡", "狗", "猪", "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊"};
|
||||
|
||||
const char Lunar_StemStrig[10][4] = {
|
||||
"庚", "辛", "壬", "癸", "甲", "乙", "丙", "丁", "戊", "己"};
|
||||
const char Lunar_StemStrig[10][4] = {"庚", "辛", "壬", "癸", "甲", "乙", "丙", "丁", "戊", "己"};
|
||||
|
||||
const char Lunar_BranchStrig[12][4] = {
|
||||
"申", "酉", "戌", "亥", "子", "丑", "寅", "卯", "辰", "巳", "午", "未"};
|
||||
const char Lunar_BranchStrig[12][4] = {"申", "酉", "戌", "亥", "子", "丑", "寅", "卯", "辰", "巳", "午", "未"};
|
||||
|
||||
/* 2000 ~ 2199 */
|
||||
const uint32_t lunar_month_days[] = {
|
||||
@@ -82,28 +72,24 @@ const uint32_t solar_1_1[] = {
|
||||
0x00111648, 0x0011183C, 0x00111A4F, 0x00111C45, 0x00111E39, 0x0011204D, 0x00112242, 0x00112436, 0x0011264A,
|
||||
0x0011283E, 0x00112A51, 0x00112C46, 0x00112E3B, 0x0011304F*/};
|
||||
|
||||
static uint32_t GetBitInt(uint32_t data, uint8_t length, uint8_t shift)
|
||||
{
|
||||
static uint32_t GetBitInt(uint32_t data, uint8_t length, uint8_t shift) {
|
||||
return (data & (((1 << length) - 1) << shift)) >> shift;
|
||||
}
|
||||
|
||||
// WARNING: Dates before Oct. 1582 are inaccurate
|
||||
static uint16_t SolarToInt(uint16_t y, uint8_t m, uint8_t d)
|
||||
{
|
||||
static uint16_t SolarToInt(uint16_t y, uint8_t m, uint8_t d) {
|
||||
m = (m + 9) % 12;
|
||||
y = y - m / 10;
|
||||
return 365 * y + y / 4 - y / 100 + y / 400 + (m * 306 + 5) / 10 + (d - 1);
|
||||
}
|
||||
|
||||
void LUNAR_SolarToLunar(struct Lunar_Date *lunar, uint16_t solar_year, uint8_t solar_month, uint8_t solar_date)
|
||||
{
|
||||
void LUNAR_SolarToLunar(struct Lunar_Date* lunar, uint16_t solar_year, uint8_t solar_month, uint8_t solar_date) {
|
||||
uint8_t i, lunarM, m, d, leap, dm;
|
||||
uint16_t year_index, lunarY, y, offset;
|
||||
uint32_t solar_data, solar11, days;
|
||||
|
||||
if (solar_month < 1 || solar_month > 12 || solar_date < 1 || solar_date > 31 ||
|
||||
(solar_year - solar_1_1[0] < 3) || ((solar_year - solar_1_1[0]) > (sizeof(solar_1_1) / sizeof(uint32_t) - 2)))
|
||||
{
|
||||
if (solar_month < 1 || solar_month > 12 || solar_date < 1 || solar_date > 31 || (solar_year - solar_1_1[0] < 3) ||
|
||||
((solar_year - solar_1_1[0]) > (sizeof(solar_1_1) / sizeof(uint32_t) - 2))) {
|
||||
lunar->Year = 0;
|
||||
lunar->Month = 0;
|
||||
lunar->Date = 0;
|
||||
@@ -113,8 +99,7 @@ void LUNAR_SolarToLunar(struct Lunar_Date *lunar, uint16_t solar_year, uint8_t s
|
||||
|
||||
year_index = solar_year - solar_1_1[0];
|
||||
solar_data = ((uint32_t)solar_year << 9) | ((uint32_t)solar_month << 5) | ((uint32_t)solar_date);
|
||||
if (solar_1_1[year_index] > solar_data)
|
||||
{
|
||||
if (solar_1_1[year_index] > solar_data) {
|
||||
year_index -= 1;
|
||||
}
|
||||
solar11 = solar_1_1[year_index];
|
||||
@@ -129,31 +114,22 @@ void LUNAR_SolarToLunar(struct Lunar_Date *lunar, uint16_t solar_year, uint8_t s
|
||||
lunarY = year_index + solar_1_1[0];
|
||||
lunarM = 1;
|
||||
offset += 1;
|
||||
for (i = 0; i < 13; i++)
|
||||
{
|
||||
if (GetBitInt(days, 1, 12 - i) == 1)
|
||||
{
|
||||
for (i = 0; i < 13; i++) {
|
||||
if (GetBitInt(days, 1, 12 - i) == 1) {
|
||||
dm = 30;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
dm = 29;
|
||||
}
|
||||
if (offset > dm)
|
||||
{
|
||||
if (offset > dm) {
|
||||
lunarM += 1;
|
||||
offset -= dm;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
lunar->IsLeap = 0;
|
||||
if (leap != 0 && lunarM > leap)
|
||||
{
|
||||
if (lunarM == leap + 1)
|
||||
{
|
||||
if (leap != 0 && lunarM > leap) {
|
||||
if (lunarM == leap + 1) {
|
||||
lunar->IsLeap = 1;
|
||||
}
|
||||
lunarM -= 1;
|
||||
@@ -163,20 +139,11 @@ void LUNAR_SolarToLunar(struct Lunar_Date *lunar, uint16_t solar_year, uint8_t s
|
||||
lunar->Year = lunarY;
|
||||
}
|
||||
|
||||
uint8_t LUNAR_GetZodiac(const struct Lunar_Date *lunar)
|
||||
{
|
||||
return lunar->Year % 12;
|
||||
}
|
||||
uint8_t LUNAR_GetZodiac(const struct Lunar_Date* lunar) { return lunar->Year % 12; }
|
||||
|
||||
uint8_t LUNAR_GetStem(const struct Lunar_Date *lunar)
|
||||
{
|
||||
return lunar->Year % 10;
|
||||
}
|
||||
uint8_t LUNAR_GetStem(const struct Lunar_Date* lunar) { return lunar->Year % 10; }
|
||||
|
||||
uint8_t LUNAR_GetBranch(const struct Lunar_Date *lunar)
|
||||
{
|
||||
return lunar->Year % 12;
|
||||
}
|
||||
uint8_t LUNAR_GetBranch(const struct Lunar_Date* lunar) { return lunar->Year % 12; }
|
||||
|
||||
/*********************************************************************************************************
|
||||
** 以下为24节气计算相关程序
|
||||
@@ -188,113 +155,78 @@ uint8_t LUNAR_GetBranch(const struct Lunar_Date *lunar)
|
||||
有兴趣的朋友可按照上面给的原理添加其它年份的表格
|
||||
不是很清楚的朋友可给我发EMAIL
|
||||
*/
|
||||
static const uint8_t YearMonthBit[160] =
|
||||
{
|
||||
0x4E, 0xA6, 0x99, // 2000
|
||||
0x9C, 0xA2, 0x98, // 2001
|
||||
0x80, 0x00, 0x18, // 2002
|
||||
0x00, 0x10, 0x24, // 2003
|
||||
0x4E, 0xA6, 0x99, // 2004
|
||||
0x9C, 0xA2, 0x98, // 2005
|
||||
0x80, 0x82, 0x18, // 2006
|
||||
0x00, 0x10, 0x24, // 2007
|
||||
0x4E, 0xA6, 0xD9, // 2008
|
||||
0x9E, 0xA2, 0x98, // 2009
|
||||
static const uint8_t YearMonthBit[160] = {
|
||||
0x4E, 0xA6, 0x99, // 2000
|
||||
0x9C, 0xA2, 0x98, // 2001
|
||||
0x80, 0x00, 0x18, // 2002
|
||||
0x00, 0x10, 0x24, // 2003
|
||||
0x4E, 0xA6, 0x99, // 2004
|
||||
0x9C, 0xA2, 0x98, // 2005
|
||||
0x80, 0x82, 0x18, // 2006
|
||||
0x00, 0x10, 0x24, // 2007
|
||||
0x4E, 0xA6, 0xD9, // 2008
|
||||
0x9E, 0xA2, 0x98, // 2009
|
||||
|
||||
0x80, 0x82, 0x18, // 2010
|
||||
0x00, 0x10, 0x04, // 2011
|
||||
0x4E, 0xE6, 0xD9, // 2012
|
||||
0x9E, 0xA6, 0xA8, // 2013
|
||||
0x80, 0x82, 0x18, // 2014
|
||||
0x00, 0x10, 0x00, // 2015
|
||||
0x0F, 0xE6, 0xD9, // 2016
|
||||
0xBE, 0xA6, 0x98, // 2017
|
||||
0x88, 0x82, 0x18, // 2018
|
||||
0x80, 0x00, 0x00, // 2019
|
||||
0x80, 0x82, 0x18, // 2010
|
||||
0x00, 0x10, 0x04, // 2011
|
||||
0x4E, 0xE6, 0xD9, // 2012
|
||||
0x9E, 0xA6, 0xA8, // 2013
|
||||
0x80, 0x82, 0x18, // 2014
|
||||
0x00, 0x10, 0x00, // 2015
|
||||
0x0F, 0xE6, 0xD9, // 2016
|
||||
0xBE, 0xA6, 0x98, // 2017
|
||||
0x88, 0x82, 0x18, // 2018
|
||||
0x80, 0x00, 0x00, // 2019
|
||||
|
||||
0x0F, 0xEF, 0xD9, // 2020
|
||||
0xBE, 0xA6, 0x99, // 2021
|
||||
0x8C, 0x82, 0x98, // 2022
|
||||
0x80, 0x00, 0x00, // 2023
|
||||
0x0F, 0xEF, 0xDB, // 2024
|
||||
0xBE, 0xA6, 0x99, // 2025
|
||||
0x9C, 0xA2, 0x98, // 2026
|
||||
0x80, 0x00, 0x18, // 2027
|
||||
0x0F, 0xEF, 0xDB, // 2028
|
||||
0xBE, 0xA6, 0x99, // 2029
|
||||
0x0F, 0xEF, 0xD9, // 2020
|
||||
0xBE, 0xA6, 0x99, // 2021
|
||||
0x8C, 0x82, 0x98, // 2022
|
||||
0x80, 0x00, 0x00, // 2023
|
||||
0x0F, 0xEF, 0xDB, // 2024
|
||||
0xBE, 0xA6, 0x99, // 2025
|
||||
0x9C, 0xA2, 0x98, // 2026
|
||||
0x80, 0x00, 0x18, // 2027
|
||||
0x0F, 0xEF, 0xDB, // 2028
|
||||
0xBE, 0xA6, 0x99, // 2029
|
||||
|
||||
0x9C, 0xA2, 0x98, // 2030
|
||||
0x80, 0x00, 0x18, // 2031
|
||||
0x0F, 0xEF, 0xDB, // 2032
|
||||
0xBE, 0xA2, 0x99, // 2033
|
||||
0x8C, 0xA0, 0x98, // 2034
|
||||
0x80, 0x82, 0x18, // 2035
|
||||
0x0B, 0xEF, 0xDB, // 2036
|
||||
0xBE, 0xA6, 0x99, // 2037
|
||||
0x8C, 0xA2, 0x98, // 2038
|
||||
0x80, 0x82, 0x18, // 2039
|
||||
0x9C, 0xA2, 0x98, // 2030
|
||||
0x80, 0x00, 0x18, // 2031
|
||||
0x0F, 0xEF, 0xDB, // 2032
|
||||
0xBE, 0xA2, 0x99, // 2033
|
||||
0x8C, 0xA0, 0x98, // 2034
|
||||
0x80, 0x82, 0x18, // 2035
|
||||
0x0B, 0xEF, 0xDB, // 2036
|
||||
0xBE, 0xA6, 0x99, // 2037
|
||||
0x8C, 0xA2, 0x98, // 2038
|
||||
0x80, 0x82, 0x18, // 2039
|
||||
|
||||
0x0F, 0xEF, 0xDB, // 2040
|
||||
0xBE, 0xE6, 0xD9, // 2041
|
||||
0x9E, 0xA2, 0x98, // 2042
|
||||
0x80, 0x82, 0x18, // 2043
|
||||
0x0F, 0xEF, 0xFB, // 2044
|
||||
0xBF, 0xE6, 0xD9, // 2045
|
||||
0x9E, 0xA6, 0x98, // 2046
|
||||
0x80, 0x82, 0x18, // 2047
|
||||
0x0F, 0xFF, 0xFF, // 2048
|
||||
0xFC, 0xEF, 0xD9, // 2049
|
||||
0xBE, 0xA6, 0x18, // 2050
|
||||
0x0F, 0xEF, 0xDB, // 2040
|
||||
0xBE, 0xE6, 0xD9, // 2041
|
||||
0x9E, 0xA2, 0x98, // 2042
|
||||
0x80, 0x82, 0x18, // 2043
|
||||
0x0F, 0xEF, 0xFB, // 2044
|
||||
0xBF, 0xE6, 0xD9, // 2045
|
||||
0x9E, 0xA6, 0x98, // 2046
|
||||
0x80, 0x82, 0x18, // 2047
|
||||
0x0F, 0xFF, 0xFF, // 2048
|
||||
0xFC, 0xEF, 0xD9, // 2049
|
||||
0xBE, 0xA6, 0x18, // 2050
|
||||
};
|
||||
static const uint8_t days[24] =
|
||||
{
|
||||
6, 20, 4, 19, 6, 21, // 一月到三月 的节气基本日期
|
||||
5, 20, 6, 21, 6, 21, // 四月到六月 的节气基本日期
|
||||
7, 23, 8, 23, 8, 23, // 七月到九月 的节气基本日期
|
||||
8, 24, 8, 22, 7, 22, // 十月到十二月的节气基本日期
|
||||
static const uint8_t days[24] = {
|
||||
6, 20, 4, 19, 6, 21, // 一月到三月 的节气基本日期
|
||||
5, 20, 6, 21, 6, 21, // 四月到六月 的节气基本日期
|
||||
7, 23, 8, 23, 8, 23, // 七月到九月 的节气基本日期
|
||||
8, 24, 8, 22, 7, 22, // 十月到十二月的节气基本日期
|
||||
};
|
||||
/*立春、雨水、惊蛰、春分、清明、谷雨、立夏、小满、芒种、夏至、小暑、大暑、立秋、处暑、白露、秋分、寒露、霜降、立冬、小雪、大雪、冬至、小寒、大寒
|
||||
*
|
||||
*/
|
||||
const char JieQiStr[24][7] = {
|
||||
"小寒",
|
||||
"大寒",
|
||||
"立春",
|
||||
"雨水",
|
||||
"惊蛰",
|
||||
"春分",
|
||||
"清明",
|
||||
"谷雨",
|
||||
"立夏",
|
||||
"小满",
|
||||
"芒种",
|
||||
"夏至",
|
||||
"小暑",
|
||||
"大暑",
|
||||
"立秋",
|
||||
"处暑",
|
||||
"白露",
|
||||
"秋分",
|
||||
"寒露",
|
||||
"霜降",
|
||||
"立冬",
|
||||
"小雪",
|
||||
"大雪",
|
||||
"冬至",
|
||||
"小寒", "大寒", "立春", "雨水", "惊蛰", "春分", "清明", "谷雨", "立夏", "小满", "芒种", "夏至",
|
||||
"小暑", "大暑", "立秋", "处暑", "白露", "秋分", "寒露", "霜降", "立冬", "小雪", "大雪", "冬至",
|
||||
};
|
||||
const uint8_t MonthDayMax[12] = {
|
||||
31,
|
||||
28,
|
||||
31,
|
||||
30,
|
||||
31,
|
||||
30,
|
||||
31,
|
||||
31,
|
||||
30,
|
||||
31,
|
||||
30,
|
||||
31,
|
||||
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31,
|
||||
};
|
||||
|
||||
/*********************************************************************************************************
|
||||
@@ -314,24 +246,19 @@ const uint8_t MonthDayMax[12] = {
|
||||
** 日 期:
|
||||
**------------------------------------------------------------------------------------------------------
|
||||
********************************************************************************************************/
|
||||
uint8_t GetJieQi(uint16_t myear, uint8_t mmonth, uint8_t mday, uint8_t *JQdate)
|
||||
{
|
||||
uint8_t GetJieQi(uint16_t myear, uint8_t mmonth, uint8_t mday, uint8_t* JQdate) {
|
||||
uint8_t bak1, value, JQ;
|
||||
|
||||
if ((myear < 2000) || (myear > 2050))
|
||||
return 0;
|
||||
if ((mmonth == 0) || (mmonth > 12))
|
||||
return 0;
|
||||
JQ = (mmonth - 1) * 2; // 获得节气顺序标号(0~23
|
||||
if (mday >= 15)
|
||||
JQ++; // 判断是否是上半月
|
||||
if ((myear < 2000) || (myear > 2050)) return 0;
|
||||
if ((mmonth == 0) || (mmonth > 12)) return 0;
|
||||
JQ = (mmonth - 1) * 2; // 获得节气顺序标号(0~23
|
||||
if (mday >= 15) JQ++; // 判断是否是上半月
|
||||
|
||||
bak1 = YearMonthBit[(myear - 2000) * 3 + JQ / 8]; // 获得节气日期相对值所在字节
|
||||
value = ((bak1 << (JQ % 8)) & 0x80); // 获得节气日期相对值状态
|
||||
bak1 = YearMonthBit[(myear - 2000) * 3 + JQ / 8]; // 获得节气日期相对值所在字节
|
||||
value = ((bak1 << (JQ % 8)) & 0x80); // 获得节气日期相对值状态
|
||||
|
||||
*JQdate = days[JQ];
|
||||
if (value != 0)
|
||||
{
|
||||
if (value != 0) {
|
||||
// 判断年份,以决定节气相对值1代表1,还是-1。
|
||||
if ((JQ == 1 || JQ == 11 || JQ == 18 || JQ == 21) && myear < 2044)
|
||||
(*JQdate)++;
|
||||
@@ -357,19 +284,16 @@ uint8_t GetJieQi(uint16_t myear, uint8_t mmonth, uint8_t mday, uint8_t *JQdate)
|
||||
** 日 期:
|
||||
**------------------------------------------------------------------------------------------------------
|
||||
********************************************************************************************************/
|
||||
uint8_t GetJieQiStr(uint16_t myear, uint8_t mmonth, uint8_t mday, uint8_t *day)
|
||||
{
|
||||
uint8_t GetJieQiStr(uint16_t myear, uint8_t mmonth, uint8_t mday, uint8_t* day) {
|
||||
uint8_t JQdate, JQ, MaxDay;
|
||||
|
||||
if (GetJieQi(myear, mmonth, mday, &JQdate) == 0)
|
||||
return 0xFF;
|
||||
if (GetJieQi(myear, mmonth, mday, &JQdate) == 0) return 0xFF;
|
||||
|
||||
JQ = (mmonth - 1) * 2; // 获得节气顺序标号(0~23
|
||||
JQ = (mmonth - 1) * 2; // 获得节气顺序标号(0~23
|
||||
|
||||
if (mday >= 15)
|
||||
JQ++; // 判断是否是上半月
|
||||
if (mday >= 15) JQ++; // 判断是否是上半月
|
||||
|
||||
if (mday == JQdate) // 今天正是一个节气日
|
||||
if (mday == JQdate) // 今天正是一个节气日
|
||||
{
|
||||
*day = 0;
|
||||
return JQ;
|
||||
@@ -377,30 +301,24 @@ uint8_t GetJieQiStr(uint16_t myear, uint8_t mmonth, uint8_t mday, uint8_t *day)
|
||||
// 今天不是一个节气日
|
||||
// StrCopy(str, (uint8_t *)"离小寒还有??天", 15);
|
||||
|
||||
if (mday < JQdate) // 如果今天日期小于本月的节气日期
|
||||
if (mday < JQdate) // 如果今天日期小于本月的节气日期
|
||||
{
|
||||
|
||||
mday = JQdate - mday;
|
||||
}
|
||||
else // 如果今天日期大于本月的节气日期
|
||||
} else // 如果今天日期大于本月的节气日期
|
||||
{
|
||||
JQ++;
|
||||
|
||||
if (mday < 15)
|
||||
{
|
||||
if (mday < 15) {
|
||||
GetJieQi(myear, mmonth, 15, &JQdate);
|
||||
mday = JQdate - mday;
|
||||
}
|
||||
else // 翻月
|
||||
} else // 翻月
|
||||
{
|
||||
MaxDay = MonthDayMax[mmonth - 1];
|
||||
if (mmonth == 2) // 润月问题
|
||||
if (mmonth == 2) // 润月问题
|
||||
{
|
||||
if ((myear % 4 == 0) && ((myear % 100 != 0) || (myear % 400 == 0)))
|
||||
MaxDay++;
|
||||
if ((myear % 4 == 0) && ((myear % 100 != 0) || (myear % 400 == 0))) MaxDay++;
|
||||
}
|
||||
if (++mmonth == 13)
|
||||
mmonth = 1;
|
||||
if (++mmonth == 13) mmonth = 1;
|
||||
GetJieQi(myear, mmonth, 1, &JQdate);
|
||||
mday = MaxDay - mday + JQdate;
|
||||
}
|
||||
@@ -409,14 +327,12 @@ uint8_t GetJieQiStr(uint16_t myear, uint8_t mmonth, uint8_t mday, uint8_t *day)
|
||||
return JQ;
|
||||
}
|
||||
|
||||
uint32_t SEC_PER_YR[2] = {31536000, 31622400}; // 闰年和非闰年的秒数
|
||||
uint32_t SEC_PER_YR[2] = {31536000, 31622400}; // 闰年和非闰年的秒数
|
||||
uint32_t SEC_PER_MT[2][12] = {
|
||||
{2678400, 2419200, 2678400, 2592000, 2678400, 2592000,
|
||||
2678400, 2678400, 2592000, 2678400, 2592000, 2678400},
|
||||
{2678400, 2505600, 2678400, 2592000, 2678400, 2592000,
|
||||
2678400, 2678400, 2592000, 2678400, 2592000, 2678400},
|
||||
{2678400, 2419200, 2678400, 2592000, 2678400, 2592000, 2678400, 2678400, 2592000, 2678400, 2592000, 2678400},
|
||||
{2678400, 2505600, 2678400, 2592000, 2678400, 2592000, 2678400, 2678400, 2592000, 2678400, 2592000, 2678400},
|
||||
};
|
||||
#define SECOND_OF_DAY 86400 // 一天多少秒
|
||||
#define SECOND_OF_DAY 86400 // 一天多少秒
|
||||
|
||||
/**
|
||||
* @Name : static int is_leap(int yr)
|
||||
@@ -427,8 +343,7 @@ uint32_t SEC_PER_MT[2][12] = {
|
||||
* @Out : 1:是闰年 0:非闰年
|
||||
* @Author : Denis
|
||||
*/
|
||||
int is_leap(int yr)
|
||||
{
|
||||
int is_leap(int yr) {
|
||||
if (0 == (yr % 100))
|
||||
return (yr % 400 == 0) ? 1 : 0;
|
||||
else
|
||||
@@ -443,27 +358,22 @@ int is_leap(int yr)
|
||||
* @Out : 星期几
|
||||
* @Author : Denis
|
||||
*/
|
||||
unsigned char day_of_week_get(unsigned char month, unsigned char day,
|
||||
unsigned short year)
|
||||
{
|
||||
unsigned char day_of_week_get(unsigned char month, unsigned char day, unsigned short year) {
|
||||
/* Month should be a number 0 to 11, Day should be a number 1 to 31 */
|
||||
static int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
|
||||
year -= (uint8_t)(month < 3);
|
||||
return (year + year / 4 - year / 100 + year / 400 + t[month - 1] + day) % 7;
|
||||
}
|
||||
|
||||
void transformTime(uint32_t unix_time, struct devtm *result)
|
||||
{
|
||||
void transformTime(uint32_t unix_time, struct devtm* result) {
|
||||
int leapyr = 0;
|
||||
uint32_t ltime = unix_time;
|
||||
|
||||
memset(result, 0, sizeof(struct devtm));
|
||||
result->tm_year = EPOCH_YR;
|
||||
|
||||
while (1)
|
||||
{
|
||||
if (ltime < SEC_PER_YR[is_leap(result->tm_year)])
|
||||
{
|
||||
while (1) {
|
||||
if (ltime < SEC_PER_YR[is_leap(result->tm_year)]) {
|
||||
break;
|
||||
}
|
||||
ltime -= SEC_PER_YR[is_leap(result->tm_year)];
|
||||
@@ -472,10 +382,8 @@ void transformTime(uint32_t unix_time, struct devtm *result)
|
||||
|
||||
leapyr = is_leap(result->tm_year);
|
||||
|
||||
while (1)
|
||||
{
|
||||
if (ltime < SEC_PER_MT[leapyr][result->tm_mon])
|
||||
break;
|
||||
while (1) {
|
||||
if (ltime < SEC_PER_MT[leapyr][result->tm_mon]) break;
|
||||
ltime -= SEC_PER_MT[leapyr][result->tm_mon];
|
||||
++(result->tm_mon);
|
||||
}
|
||||
@@ -490,9 +398,7 @@ void transformTime(uint32_t unix_time, struct devtm *result)
|
||||
result->tm_min = ltime / 60;
|
||||
result->tm_sec = ltime % 60;
|
||||
|
||||
result->tm_wday =
|
||||
day_of_week_get(result->tm_mon + 1, result->tm_mday,
|
||||
result->tm_year);
|
||||
result->tm_wday = day_of_week_get(result->tm_mon + 1, result->tm_mday, result->tm_year);
|
||||
|
||||
/*
|
||||
* The number of years since YEAR0"
|
||||
@@ -505,10 +411,8 @@ uint8_t map[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
|
||||
/*
|
||||
获取一个月最后一天值
|
||||
*/
|
||||
uint8_t get_last_day(uint16_t year, uint8_t month)
|
||||
{
|
||||
if (month % 12 == 1)
|
||||
{
|
||||
uint8_t get_last_day(uint16_t year, uint8_t month) {
|
||||
if (month % 12 == 1) {
|
||||
return map[month % 12] + is_leap(year);
|
||||
}
|
||||
return map[month % 12];
|
||||
@@ -517,32 +421,26 @@ uint8_t get_last_day(uint16_t year, uint8_t month)
|
||||
/*
|
||||
获取一个月第一天星期值
|
||||
*/
|
||||
uint8_t get_first_day_week(uint16_t year, uint8_t month)
|
||||
{
|
||||
return day_of_week_get(month, 1, year);
|
||||
}
|
||||
uint8_t get_first_day_week(uint16_t year, uint8_t month) { return day_of_week_get(month, 1, year); }
|
||||
|
||||
// 时间结构体转时间戳
|
||||
uint32_t transformTimeStruct(struct devtm *result)
|
||||
{
|
||||
uint32_t transformTimeStruct(struct devtm* result) {
|
||||
uint32_t Cyear = 0;
|
||||
for (uint16_t i = 1970; i < result->tm_year; i++)
|
||||
{
|
||||
if (is_leap(i) == 1)
|
||||
Cyear++;
|
||||
for (uint16_t i = 1970; i < result->tm_year; i++) {
|
||||
if (is_leap(i) == 1) Cyear++;
|
||||
}
|
||||
|
||||
uint32_t CountDay = Cyear * (uint32_t)366 + (uint32_t)(result->tm_year - 1970 - Cyear) * (uint32_t)365 + result->tm_mday - 1;
|
||||
for (uint8_t i = 0; i < result->tm_mon - 1; i++)
|
||||
{
|
||||
uint32_t CountDay =
|
||||
Cyear * (uint32_t)366 + (uint32_t)(result->tm_year - 1970 - Cyear) * (uint32_t)365 + result->tm_mday - 1;
|
||||
for (uint8_t i = 0; i < result->tm_mon - 1; i++) {
|
||||
CountDay += get_last_day(result->tm_year, i);
|
||||
}
|
||||
|
||||
return (CountDay * SECOND_OF_DAY + (uint32_t)result->tm_sec + (uint32_t)result->tm_min * 60 + (uint32_t)result->tm_hour * 3600);
|
||||
return (CountDay * SECOND_OF_DAY + (uint32_t)result->tm_sec + (uint32_t)result->tm_min * 60 +
|
||||
(uint32_t)result->tm_hour * 3600);
|
||||
}
|
||||
|
||||
uint8_t thisMonthMaxDays(uint8_t year, uint8_t month)
|
||||
{
|
||||
uint8_t thisMonthMaxDays(uint8_t year, uint8_t month) {
|
||||
if (year % 4 == 0 && month == 2)
|
||||
return MonthDayMax[month - 1] + 1;
|
||||
else
|
||||
|
||||
Reference in New Issue
Block a user