优化蓝牙产测RTC查询
authorpenglei <1624952103@qq.com>
Tue, 9 Apr 2024 08:36:44 +0000 (16:36 +0800)
committerpenglei <1624952103@qq.com>
Tue, 9 Apr 2024 08:36:44 +0000 (16:36 +0800)
code/sdk/drivers/peripheral/utils/custom_utils.c
code/sdk/drivers/peripheral/utils/custom_utils.h

index 9548c9c..1171de7 100644 (file)
@@ -867,6 +867,57 @@ void gsensor_get_single_data(gsenson_t *t)
     get_gsonsen_xyz(t);
 }
 
+
+// 判断是否为闰年
+int isLeapYear(int year) {
+    return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
+}
+
+// 计算某年某月的天数
+int daysInMonth(int year, int month) {
+    const int days[12] = {31, 28 + isLeapYear(year), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
+    return days[month - 1];
+}
+
+// 计算时间距离1970年1月1日00:00:00的秒数
+uint32_t secondsSinceEpoch(int year,int month,int day,int hour,int min,int sec) {
+    const int secondsPerMinute = 60;
+    const int secondsPerHour = 3600;
+    const int secondsPerDay = 86400;
+    const int secondsPerYear = 31536000; // 不考虑闰秒
+
+    long long seconds = 0;
+
+    // 计算年份之间的秒数
+    for (int i = 1970; i < year; ++i) {
+        seconds += isLeapYear(i) ? secondsPerYear + secondsPerDay : secondsPerYear;
+    }
+
+    // 计算月份之间的秒数
+    for (int i = 1; i < month; ++i) {
+        seconds += daysInMonth(year, i) * secondsPerDay;
+    }
+
+    // 计算当月的秒数
+    seconds += (day - 1) * secondsPerDay;
+    seconds += hour * secondsPerHour;
+    seconds += min * secondsPerMinute;
+    seconds += sec;
+
+    return seconds;
+}
+
+void get_rtc(uint32_t *msc)
+{
+    char time_data[64] = {0};
+    int year,month,day,hour,min,sec;
+    uapi_get_yyyymmddhhmmss_Date(time_data);
+    PRINT("timedata : %s \r\n",time_data);
+    sscanf(time_data, "%4d%2d%2d%2d%2d%2d", &year, &month, &day, &hour, &min, &sec);
+    *msc = secondsSinceEpoch(year,month,day,hour,min,sec);
+    PRINT("msc : %d \r\n",*msc);
+}
+
 //#include "lis2doc_reg.h"
 
 #include "stdio.h"
index b1ff42f..221f7a9 100644 (file)
@@ -90,6 +90,8 @@ void battery_getvolpower(uint8_t *power);
 void flash_getid(uint32_t *id);
 void flash_erase(void);
 
+void get_rtc(uint32_t *msc);
+
 //进入recovery
 int enter_recovery(void);
 //设置bmp图片