From 97eec3849b4ba9696393a6e5d8f9980a7c6bee11 Mon Sep 17 00:00:00 2001 From: cfif Date: Fri, 16 Jan 2026 12:58:16 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9E=D0=B1=D0=BD=D0=BE=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Src/RtcFC7240.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 74 insertions(+), 1 deletion(-) diff --git a/Src/RtcFC7240.c b/Src/RtcFC7240.c index f4837cb..652c049 100644 --- a/Src/RtcFC7240.c +++ b/Src/RtcFC7240.c @@ -78,7 +78,7 @@ static bool RTC_CheckTimeDate(const RTC_TimeDate * const pTimeDate) } return bCheckReturn; } - +/* static void RTC_TimeDate_To_Second(RTC_TimeDate * const pTimeDate, uint32_t u32Second) { uint32_t u32CountDays; @@ -122,6 +122,7 @@ static void RTC_TimeDate_To_Second(RTC_TimeDate * const pTimeDate, uint32_t u32S } } + static uint32_t RTC_Second_To_TimeDate(const RTC_TimeDate * const pTimeDate) { uint32_t u32Second; @@ -151,6 +152,78 @@ static uint32_t RTC_Second_To_TimeDate(const RTC_TimeDate * const pTimeDate) return u32Second; } + */ +static void RTC_TimeDate_To_Second(RTC_TimeDate * const pTimeDate, uint32_t u32Second) +{ + uint32_t u32CountDays; + uint32_t u32TempCount; + + u32CountDays = u32Second / SECONDS_DAY; + u32TempCount = u32Second % SECONDS_DAY; + + pTimeDate->u8Hour = (uint8_t)(u32TempCount / SECONDS_HOUR); + u32TempCount = u32TempCount % SECONDS_HOUR; + + pTimeDate->u8Minute = (uint8_t)(u32TempCount / SECONDS_MINUTE); + pTimeDate->u8Second = (uint8_t)(u32TempCount % SECONDS_MINUTE); + + pTimeDate->u16Year = (uint16_t)START_YEAR; + + // Вычисляем год + uint32_t u32DaysInYear = DAYS_COM_YEAR; + while (u32CountDays >= u32DaysInYear) + { + pTimeDate->u16Year++; + u32CountDays -= u32DaysInYear; + u32DaysInYear = RTC_IsLeapYear(pTimeDate->u16Year) ? DAYS_LEAP_YEAR : DAYS_COM_YEAR; + } + + // u32CountDays теперь содержит день года (0-364/365) + // Преобразуем в месяц и день + const uint8_t* pMonthDays = RTC_IsLeapYear(pTimeDate->u16Year) ? LeapYear : ComYear; + uint32_t u32DayOfYear = u32CountDays + 1; // День года (1-366) + + for (uint8_t u8TempMonth = 1; u8TempMonth <= 12; u8TempMonth++) + { + uint32_t u32DaysInMonth = pMonthDays[u8TempMonth]; + if (u32DayOfYear <= u32DaysInMonth) + { + pTimeDate->u8Month = u8TempMonth; + pTimeDate->u8Day = (uint8_t)u32DayOfYear; + break; + } + else + { + u32DayOfYear -= u32DaysInMonth; + } + } +} + +static uint32_t RTC_Second_To_TimeDate(const RTC_TimeDate * const pTimeDate) +{ + uint32_t u32Second = 0; + + // Считаем дни за полные годы с 1970 + for (uint16_t u16Year = START_YEAR; u16Year < pTimeDate->u16Year; u16Year++) + { + u32Second += RTC_IsLeapYear(u16Year) ? DAYS_LEAP_YEAR * SECONDS_DAY : DAYS_COM_YEAR * SECONDS_DAY; + } + + // Считаем дни за полные месяцы в текущем году + const uint8_t* pMonthDays = RTC_IsLeapYear(pTimeDate->u16Year) ? LeapYear : ComYear; + for (uint8_t u8Month = 1; u8Month < pTimeDate->u8Month; u8Month++) + { + u32Second += pMonthDays[u8Month] * SECONDS_DAY; + } + + // Считаем дни, часы, минуты, секунды + u32Second += ((uint32_t)(pTimeDate->u8Day - 1) * SECONDS_DAY) + + ((uint32_t)pTimeDate->u8Hour * SECONDS_HOUR) + + ((uint32_t)pTimeDate->u8Minute * SECONDS_MINUTE) + + ((uint32_t)pTimeDate->u8Second); + + return u32Second; +} /** * @brief Set time and date