From 3315c6d9d9c128f11bd3efb93be1122ae0fc5068 Mon Sep 17 00:00:00 2001 From: penglei <1624952103@qq.com> Date: Tue, 9 Apr 2024 16:36:44 +0800 Subject: [PATCH] =?utf8?q?=E4=BC=98=E5=8C=96=E8=93=9D=E7=89=99=E4=BA=A7?= =?utf8?q?=E6=B5=8BRTC=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../drivers/peripheral/utils/custom_utils.c | 51 +++++++++++++++++++ .../drivers/peripheral/utils/custom_utils.h | 2 + 2 files changed, 53 insertions(+) diff --git a/code/sdk/drivers/peripheral/utils/custom_utils.c b/code/sdk/drivers/peripheral/utils/custom_utils.c index 9548c9c51..1171de756 100644 --- a/code/sdk/drivers/peripheral/utils/custom_utils.c +++ b/code/sdk/drivers/peripheral/utils/custom_utils.c @@ -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" diff --git a/code/sdk/drivers/peripheral/utils/custom_utils.h b/code/sdk/drivers/peripheral/utils/custom_utils.h index b1ff42fe8..221f7a9e5 100644 --- a/code/sdk/drivers/peripheral/utils/custom_utils.h +++ b/code/sdk/drivers/peripheral/utils/custom_utils.h @@ -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图片 -- 2.17.1