249 lines
6.0 KiB
C
249 lines
6.0 KiB
C
/**
|
|
******************************************************************************
|
|
* @file date_calc.c
|
|
* @author AMG Energy Solution Team
|
|
* @version V1.0.0
|
|
* @date 07-Feb-2022
|
|
* @brief This file provides all the header of the calculation datetime.
|
|
******************************************************************************
|
|
* @attention
|
|
*
|
|
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
|
|
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
|
|
* TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
|
|
* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
|
|
* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
|
|
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
|
|
*
|
|
* <h2><center>© COPYRIGHT 2022 AMG</center></h2>
|
|
******************************************************************************
|
|
*/
|
|
|
|
/** @addtogroup mBMS2
|
|
* @{
|
|
*/
|
|
|
|
/* Includes ------------------------------------------------------------------*/
|
|
#include <includes.h>
|
|
#include "date_calc.h"
|
|
#include "asutil.h"
|
|
|
|
/* Private typedef -----------------------------------------------------------*/
|
|
/* Private define ------------------------------------------------------------*/
|
|
/* Private macro -------------------------------------------------------------*/
|
|
/* Private variables ---------------------------------------------------------*/
|
|
const uint16_t aMonth[13] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
|
|
const uint16_t bMonth[13] = {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366};
|
|
const uint8_t fstrWeek[7][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
|
|
|
|
/* Private function prototypes -----------------------------------------------*/
|
|
/* Private functions ---------------------------------------------------------*/
|
|
|
|
uint32_t CalcDay(uint32_t iYear, uint32_t iMonth, uint32_t iDay)
|
|
{
|
|
uint8_t flag = false;
|
|
uint32_t TotalDays, i, cnt;
|
|
|
|
cnt = 0;
|
|
for (i = 1; i < iYear; i++)
|
|
{
|
|
if (((i % 4) == 0 && (i % 100) !=0) || (i % 400) == 0) { // Leap year calculation.
|
|
cnt++;
|
|
}
|
|
}
|
|
|
|
if (((iYear % 4) == 0 && (iYear % 100) !=0) || (iYear % 400) == 0)
|
|
flag = true;
|
|
|
|
TotalDays = (iYear - 1) * 365 + cnt;
|
|
|
|
if (flag == true)
|
|
TotalDays += bMonth[iMonth - 1] + iDay;
|
|
else
|
|
TotalDays += aMonth[iMonth - 1] + iDay;
|
|
|
|
return TotalDays;
|
|
}
|
|
|
|
uint8_t CheckLeapYear(uint16_t Year)
|
|
{
|
|
uint8_t result = false;
|
|
|
|
if (((Year % 4) == 0 && (Year % 100) != 0) || (Year % 400) == 0)
|
|
result = true;
|
|
|
|
return result;
|
|
}
|
|
|
|
uint32_t CalcOneYearToDay(uint16_t Year)
|
|
{
|
|
uint32_t result;
|
|
|
|
result = 365;
|
|
|
|
if (((Year % 4) == 0 && (Year % 100) != 0) || (Year % 400) == 0)
|
|
result++;
|
|
|
|
return result;
|
|
}
|
|
|
|
//void CalcTimeStampToDateTime1(uint32_t TimeStamp, uint8_t *DateTime)
|
|
//{
|
|
// uint32_t tDay, DivDay, ModDay, i;
|
|
// uint32_t aDateStamp;
|
|
// uint16_t tYear, tMonth, tDate;
|
|
|
|
// aDateStamp = TimeStamp / 60 / 60 / 24;
|
|
// aDateStamp++;
|
|
|
|
// tDay = 0;
|
|
// i = 0;
|
|
|
|
// while (tDay < aDateStamp) {
|
|
// tDay += CalcOneYearToDay(UTC_YEAR + i++);
|
|
// if (i >= 100) break;
|
|
// }
|
|
|
|
// if (i < 100) {
|
|
// i--;
|
|
// tDay -= CalcOneYearToDay(UTC_YEAR + i);
|
|
|
|
// tYear = UTC_YEAR + i;
|
|
|
|
// DivDay = aDateStamp - tDay;
|
|
|
|
// if (CheckLeapYear(tYear)) {
|
|
// for (i = 1; i < 13; i++)
|
|
// if (DivDay <= bMonth[i]) break;
|
|
// } else {
|
|
// for (i = 1; i < 13; i++)
|
|
// if (DivDay <= aMonth[i]) break;
|
|
// }
|
|
|
|
// tMonth = i;
|
|
|
|
// if (CheckLeapYear(tYear)) {
|
|
// ModDay = DivDay - bMonth[i - 1];
|
|
// } else {
|
|
// ModDay = DivDay - aMonth[i - 1];
|
|
// }
|
|
|
|
// tDate = ModDay;
|
|
|
|
// sprintf((char *)DateTime, "%04d/%02d/%02d",
|
|
// tYear,
|
|
// tMonth,
|
|
// tDate
|
|
// );
|
|
// } else {
|
|
// sprintf((char *)DateTime, "----/--/--");
|
|
// }
|
|
//}
|
|
|
|
#ifdef CONSOLE_DEBUG
|
|
|
|
void CalcDateStampToDateTime(uint32_t DateStamp, uint8_t *DateTime)
|
|
{
|
|
uint32_t DivDay, ModDay, tDay, i;
|
|
uint16_t year, month, date;
|
|
|
|
tDay = 0;
|
|
i = 0;
|
|
|
|
while (tDay < DateStamp)
|
|
{
|
|
tDay += CalcOneYearToDay(UTC_YEAR + i++);
|
|
if (i >= 200) break;
|
|
}
|
|
|
|
if (i < 200)
|
|
{
|
|
i--;
|
|
tDay -= CalcOneYearToDay(UTC_YEAR + i);
|
|
|
|
year = UTC_YEAR + i;
|
|
|
|
DivDay = DateStamp - tDay;
|
|
|
|
if (CheckLeapYear(year))
|
|
{
|
|
for (i = 0; i < 13; i++)
|
|
if (DivDay <= bMonth[i]) break;
|
|
}
|
|
else
|
|
{
|
|
for (i = 0; i < 13; i++)
|
|
if (DivDay <= aMonth[i]) break;
|
|
}
|
|
month = i;
|
|
|
|
if (CheckLeapYear(year))
|
|
ModDay = DivDay - bMonth[i - 1];
|
|
else
|
|
ModDay = DivDay - aMonth[i - 1];
|
|
|
|
date = ModDay;
|
|
|
|
sprintf((char *)DateTime, "%04d/%02d/%02d",
|
|
year,
|
|
month,
|
|
date
|
|
);
|
|
}
|
|
else
|
|
{
|
|
sprintf((char *)DateTime, "----/--/--");
|
|
}
|
|
}
|
|
#endif // #ifdef CONSOLE_DEBUG
|
|
|
|
uint32_t DateStampToTimeStamp(uint32_t DateStamp, uint8_t Zone)
|
|
{
|
|
if (Zone == KOREA_ZONE)
|
|
return (DateStamp * 24 * 60 * 60 - UTC_KOREA);
|
|
else
|
|
return (DateStamp * 24 * 60 * 60);
|
|
}
|
|
|
|
uint32_t DateStrToDateStamp(uint8_t *DateTime)
|
|
{
|
|
uint32_t year, month, date;
|
|
uint32_t aDateStamp;
|
|
|
|
year = atoin((char *)&DateTime[0], 4);
|
|
month = atoin((char *)&DateTime[4], 2);
|
|
date = atoin((char *)&DateTime[6], 2);
|
|
|
|
aDateStamp = CalcDay(year, month, date) - UTC_TIMESTAMP;
|
|
|
|
return (DateStampToTimeStamp(aDateStamp, KOREA_ZONE));
|
|
}
|
|
|
|
uint32_t DateTimeStrToTimeStamp(uint8_t *DateTime)
|
|
{
|
|
uint32_t year, month, date;
|
|
uint32_t hour, min, sec;
|
|
uint32_t aDateStamp;
|
|
uint32_t aTimeStamp;
|
|
|
|
year = atoin((char *)&DateTime[0], 4);
|
|
month = atoin((char *)&DateTime[4], 2);
|
|
date = atoin((char *)&DateTime[6], 2);
|
|
|
|
hour = atoin((char *)&DateTime[8], 2);
|
|
min = atoin((char *)&DateTime[10], 2);
|
|
sec = atoin((char *)&DateTime[12], 2);
|
|
|
|
aDateStamp = CalcDay(year, month, date) - UTC_TIMESTAMP;
|
|
printf("\r\n DateStamp = %d", aDateStamp);
|
|
aTimeStamp = DateStampToTimeStamp(aDateStamp, GMT_ZONE)
|
|
+ (hour * 60 * 60)
|
|
+ (min * 60)
|
|
+ sec
|
|
;
|
|
|
|
return (aTimeStamp);
|
|
}
|
|
|
|
/*******************(C)COPYRIGHT 2022 AMG ********************END OF FILE******/
|