Files
HBshuiwuConcentrator/filedata/fileidatatime.c
2025-12-15 16:07:49 +08:00

113 lines
2.4 KiB
C

#include "file.h"
#include "filedata.h"
#include "rtc_ext.h"
//include/linux/time.h
//内核中的mktime()函数位于kernel/time.c内
uint8_t rtc_getRTC(RTC_TimeDateTypeDef* para)
{
uint8_t Result = 1;
st_rtc_ext TempTime1;
read_rtc_time(&TempTime1);//读一次时间
Result = 0;
para->Year = TempTime1.year;
para->Month = TempTime1.month;
para->Date = TempTime1.day;
para->Hour = TempTime1.hour;
para->Minute = TempTime1.min;
para->Second = TempTime1.sec;
para->Week = TempTime1.week;
return Result;
}
unsigned long mktime (unsigned int year, unsigned int mon,
unsigned int day, unsigned int hour,
unsigned int min, unsigned int sec)
{
if (0 >= (int) (mon -= 2)) { /* 1..12 -> 11,12,1..10 */
mon += 12; /* Puts Feb last since it has leap day */
year -= 1;
}
return (((
(unsigned long) (year/4 - year/100 + year/400 + 367*mon/12 + day) +
year*365 - 719499
)*24 + hour /* now have hours */
)*60 + min /* now have minutes */
)*60 + sec; /* finally seconds */
}
uint8_t bcd_to_dec(uint8_t val)
{
return ((((val)>>4)*10) + ((val)&0x0F));
}
uint8_t get_space_day(uint32_t oldtime,uint32_t newtime)
{
uint8_t day = 0;
if(newtime>oldtime)
{
day = (newtime - oldtime)/(3600*24);
}
return day;
}
uint32_t get_curtime(uint8_t *curyear,uint8_t *curmon,uint8_t *curday)
{
RTC_TimeDateTypeDef rtctime;
uint32_t time = 0;
uint32_t year = 0;
uint8_t mon = 0;
uint8_t day = 0;
rtc_getRTC(&rtctime);
year = bcd_to_dec(rtctime.Year);
mon = bcd_to_dec(rtctime.Month);
day = bcd_to_dec(rtctime.Date);
year = year + DEC_YEAR;
time = mktime(year,mon,day,0,0,0);
*curyear = year;
*curmon = mon;
*curday = day;
#ifdef DEBUG_FILEI2C
HT_PRINT("time = %d year = %d mon = %d day = %d\r\n",time,year,mon,day);
#endif
return time;
}
void get_complete_curtime(uint32_t *curyear,uint8_t *curmon,uint8_t *curday,uint8_t *curhour,uint8_t *curmin)
{
RTC_TimeDateTypeDef rtctime;
uint32_t year = 0;
uint8_t mon = 0;
uint8_t day = 0;
uint8_t hour = 0;
uint8_t min = 0;
rtc_getRTC(&rtctime);
year = bcd_to_dec(rtctime.Year);
mon = bcd_to_dec(rtctime.Month);
day = bcd_to_dec(rtctime.Date);
hour = bcd_to_dec(rtctime.Hour);
min = bcd_to_dec(rtctime.Minute);
year = year + DEC_YEAR;
*curyear = year;
*curmon = mon;
*curday = day;
*curhour = hour;
*curmin = min;
}