Platform/ECM3532/M3/hw/hal/common/rtc/src/rtc_hal.c
/*
 * Convert epoch time to RTC time
 */
Convert epoch time to RTC time
Convert epoch time to RTC time
void RtcFromEpoch(uint32_t epoch, struct RtcTm *sTm)
{
	uint32_t tm;
	uint32_t t1;
	uint32_t a;
	uint32_t b;
	uint32_t c;
	uint32_t d;
	uint32_t e;
	uint32_t m;
	int16_t  year  = 0;
	int16_t  month = 0;
	int16_t  dow   = 0;
	int16_t  mday  = 0;
	int16_t  hour  = 0;
	int16_t  min   = 0;
	int16_t  sec   = 0;
	uint64_t JD    = 0;
	uint64_t JDN   = 0;
	JD  = ((epoch + 43200) / (86400 >>1 )) + (2440587 << 1) + 1;
	JDN = JD >> 1;
    tm = epoch; t1 = tm / 60; sec  = tm - (t1 * 60);
    tm = t1;    t1 = tm / 60; min  = tm - (t1 * 60);
    tm = t1;    t1 = tm / 24; hour = tm - (t1 * 24);
    dow   = JDN % 7;
    a     = JDN + 32044;
    b     = ((4 * a) + 3) / 146097;
    c     = a - ((146097 * b) / 4);
    d     = ((4 * c) + 3) / 1461;
    e     = c - ((1461 * d) / 4);
    m     = ((5 * e) + 2) / 153;
    mday  = e - (((153 * m) + 2) / 5) + 1;
    month = m + 3 - (12 * (m / 10));
    year  = (100 * b) + d - 4800 + (m / 10);
    sTm->year = year;
    sTm->month = month;
    sTm->date = mday;
    sTm->day = dow;
    sTm->hour = hour;
    sTm->min = min;
    sTm->sec = sec;
}
void RtcFromEpoch ( uint32_t epoch , struct RtcTm * sTm ) { uint32_t tm ; uint32_t t1 ; uint32_t a ; uint32_t b ; uint32_t c ; uint32_t d ; uint32_t e ; uint32_t m ; int16_t year = 0 ; int16_t month = 0 ; int16_t dow = 0 ; int16_t mday = 0 ; int16_t hour = 0 ; int16_t min = 0 ; int16_t sec = 0 ; uint64_t JD = 0 ; uint64_t JDN = 0 ; JD = ( ( epoch + 43200 ) / ( 86400 >> 1 ) ) + ( 2440587 << 1 ) + 1 ; JDN = JD >> 1 ; tm = epoch ; t1 = tm / 60 ; sec = tm - ( t1 * 60 ) ; tm = t1 ; t1 = tm / 60 ; min = tm - ( t1 * 60 ) ; tm = t1 ; t1 = tm / 24 ; hour = tm - ( t1 * 24 ) ; dow = JDN % 7 ; a = JDN + 32044 ; b = ( ( 4 * a ) + 3 ) / 146097 ; c = a - ( ( 146097 * b ) / 4 ) ; d = ( ( 4 * c ) + 3 ) / 1461 ; e = c - ( ( 1461 * d ) / 4 ) ; m = ( ( 5 * e ) + 2 ) / 153 ; mday = e - ( ( ( 153 * m ) + 2 ) / 5 ) + 1 ; month = m + 3 - ( 12 * ( m / 10 ) ) ; year = ( 100 * b ) + d - 4800 + ( m / 10 ) ; sTm -> year = year ; sTm -> month = month ; sTm -> date = mday ; sTm -> day = dow ; sTm -> hour = hour ; sTm -> min = min ; sTm -> sec = sec ; }
Convert epoch time to RTC time
Convert epoch time to RTC time
// These hardcore math's are taken from http://en.wikipedia.org/wiki/Julian_day