⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ssp_time.c

📁 abstract rtos
💻 C
字号:
#include "syscfg.h"#ifdef __cplusplus#if __cplusplusextern "C"{#endif #endif #include "sys/sys_pub.h"#include "aos.h"#include "ssp_cputick.h"#define MILLISECONDS_PER_TICK (1000/CONFIG_TICKS_PER_SECOND)SYS_TIME_S g_SysTime = {2002,02,20,15,51,01,4,0};U32 g_high_ms=0;U32 g_low_ms=0;U32 g_seconds=0;U32 g_ticks=0;U32 ssp_days_per_month( U32 year, U32 month );AOS_INLINE S32 comp_sys_time ( SYS_TIME_S* pFirstTime, SYS_TIME_S* pSecondTime );                     U32 m_time_init(){    g_high_ms =0;    g_low_ms  =0;    g_seconds =0;    g_ticks   =0;        return AOS_SUCC;}U32 aos_time_get( SYS_TIME_S *pSysTime ){	U32 lockKey;	    if( NULL == pSysTime )    {        return TIME_NULL_POINTER_PARA;    }    lockKey = aos_int_lock();    pSysTime->year  = g_SysTime.year;    pSysTime->month = g_SysTime.month;    pSysTime->date  = g_SysTime.date;    pSysTime->hour  = g_SysTime.hour;    pSysTime->minute= g_SysTime.minute;    pSysTime->second= g_SysTime.second;    pSysTime->week  = g_SysTime.week;    pSysTime->millisec= g_ticks*10;    aos_int_unlock(lockKey);    return AOS_SUCC;}U32 aos_time_set( SYS_TIME_S *pSysTime ){    U32 max_days, lockKey;        if( NULL == pSysTime )    {        return TIME_NULL_POINTER_PARA;    }    if( pSysTime->year < 1997 || pSysTime->year > 3000 )    {        aos_printf( MPE_SYS, "Invalid year %d ", pSysTime->year );        return TIME_INVALID_YEAR;    }    if( pSysTime->month < 1 || pSysTime->month > 12 )    {        aos_printf( MPE_SYS, "Invalid month %d ", pSysTime->month );        return TIME_INVALID_MON;    }    max_days = ssp_days_per_month(  pSysTime->year, pSysTime->month );    if( pSysTime->date < 1 || pSysTime->date > max_days )    {        aos_printf( MPE_SYS, "Invalid date %d ", pSysTime->date );        return TIME_INVALID_DATE;    }    if( pSysTime->hour >= 24 )    {        aos_printf( MPE_SYS, "Invalid hour %d ", pSysTime->hour );        return TIME_INVALID_HOUR;    }    if( pSysTime->minute >= 60 )    {        aos_printf( MPE_SYS, "Invalid minute %d ", pSysTime->minute );        return TIME_INVALID_MIN;    }    if( pSysTime->second >= 60 )    {        aos_printf( MPE_SYS, "Invalid second %d ", pSysTime->minute );        return TIME_INVALID_SEC;    }    if(  pSysTime->millisec >= 1000 )    {        aos_printf( MPE_SYS, "Invalid millisecond %d ", pSysTime->millisec);        return TIME_INVALID_MILLISEC;    }    aos_time_to_week( pSysTime );    lockKey = aos_int_lock();    g_SysTime.year  = pSysTime->year;    g_SysTime.month = pSysTime->month;    g_SysTime.date  = pSysTime->date;    g_SysTime.hour  = pSysTime->hour;    g_SysTime.minute= pSysTime->minute;    g_SysTime.second= pSysTime->second;    g_SysTime.week  = pSysTime->week;;    g_SysTime.millisec= pSysTime->millisec;    g_ticks = pSysTime->millisec/MILLISECONDS_PER_TICK;    aos_int_unlock(lockKey);    return AOS_SUCC;}VOID aos_time_get_ms( U32 *pulHighMilliSec, U32 *pulLowMilliSec ){    if( (NULL == pulHighMilliSec) ||(NULL == pulLowMilliSec ) )    {        return;    }    *pulHighMilliSec = g_high_ms;    *pulLowMilliSec = g_low_ms;}VOID aos_time_get_sec( U32 *pulSec ){    if( NULL == pulSec )    {        return;    }    *pulSec = g_seconds;}VOID aos_time_get_usec( U32 *pulSec, U32 *pulUSec ){#if(CONFIG_OS_VXWORKS == TRUE )    CPU_TICK_S tm;    if( NULL == pulSec || NULL == pulUSec )    {        return;    }    aos_cputick_get(&tm);    aos_cputick_2us(&tm, pulSec, pulUSec);#else    if( NULL == pulSec || NULL == pulUSec )    {        return;    }    *pulSec  = g_seconds;    *pulUSec = g_ticks *(1000/CONFIG_TICKS_PER_SECOND)*1000;#endif}S32 aos_time_diff_day( SYS_TIME_S * pFirstTime, SYS_TIME_S * pSecondTime ){    S32 temp_diff, ret_diff, tempYear, tempMonth;    SYS_TIME_S * pTempFirstTime, *pTempSecondTime;        AOS_ASSERT( NULL != pFirstTime && NULL != pSecondTime );    if((pFirstTime == NULL) || (pSecondTime == NULL))    {        return (S32)AOS_FAIL;    }    temp_diff = comp_sys_time(pFirstTime, pSecondTime);    if( 0 == temp_diff )    {    	return 0;    }    else if( 0 > temp_diff )    {    	pTempFirstTime = pFirstTime;    	pTempSecondTime = pSecondTime;    }    else    {    	pTempFirstTime = pSecondTime;    	pTempSecondTime = pFirstTime;    }	if( pTempFirstTime->year == pTempSecondTime->year )	{		if( pTempFirstTime->month == pTempSecondTime->month )		{	 		ret_diff = pTempSecondTime->date - pTempFirstTime->date;		}else		{	 		ret_diff = (S32)(ssp_days_per_month( pTempFirstTime->year, pTempFirstTime->month )		       	- pTempFirstTime->date);  				 		tempMonth = pTempFirstTime->month;	 		while( ++tempMonth < pTempSecondTime->month )	 		{	 			ret_diff += (S32)ssp_days_per_month( pTempFirstTime->year, (U32)tempMonth );	 		}	 		ret_diff += pTempSecondTime->date;	 				}	}	else    	{				ret_diff = (S32)(ssp_days_per_month( pTempFirstTime->year, pTempFirstTime->month)				- pTempFirstTime->date);  		tempMonth = pTempFirstTime->month;				while( ++tempMonth <= 12)		{	 		ret_diff += (S32)ssp_days_per_month( pTempFirstTime->year, (U32)tempMonth );		}								for(tempYear = pTempFirstTime->year + 1;	 		tempYear < pTempSecondTime->year; tempYear++)		{	  		for(tempMonth = 1; tempMonth <= 12; tempMonth++)	  		{	  			ret_diff += (S32)ssp_days_per_month( (U32)tempYear, (U32)tempMonth );	  		}		} 	 		 	for( tempMonth=1; tempMonth < pTempSecondTime->month; tempMonth++ )	    {			ret_diff += (S32)ssp_days_per_month( pTempSecondTime->year, (U32)tempMonth );	    }  	    ret_diff += pTempSecondTime->date;    	}	return (temp_diff>0?ret_diff:0-ret_diff);}S32 aos_time_diff_sec( SYS_TIME_S * pFirstTime, SYS_TIME_S * pSecondTime ){	S32 diff_day, diff_sec;	    AOS_ASSERT( NULL != pFirstTime && NULL != pSecondTime );    if((pFirstTime == NULL) || (pSecondTime == NULL))    {        return (S32)AOS_FAIL;    }    diff_day = aos_time_diff_day( pFirstTime, pSecondTime);    diff_sec = pFirstTime->hour*3600 + pFirstTime->minute*60 +pFirstTime->second;    diff_sec -= pSecondTime->hour*3600 + pSecondTime->minute*60 +pSecondTime->second;    diff_sec += diff_day*24*3600;    return diff_sec;}U32 aos_time_since1970_in_sec( ){    SYS_TIME_S  cur_time,start_time;    S32         diff;    start_time.year  = 1970;    start_time.month = 1;    start_time.date  = 1;    start_time.hour  = 0;    start_time.minute= 0;    start_time.second= 0;    start_time.millisec= 0;    start_time.week    = 0;    aos_time_get(&cur_time);    diff = aos_time_diff_sec( &cur_time, &start_time );        return (U32)diff;}U32 aos_time_to_week( SYS_TIME_S*pSysTime ){	SYS_TIME_S one_time;	U32 diff_day;	one_time.year = 2000;	one_time.month= 1;	one_time.date= 1;	one_time.hour= 0;	one_time.minute= 0;	one_time.second= 0;	one_time.week= 6;	diff_day = (U32)aos_time_diff_day( pSysTime, &one_time );	diff_day += one_time.week;	diff_day %= 7;	if( !diff_day )	{		diff_day = 7;	}	pSysTime->week = (U8)diff_day;		return diff_day;}VOID ssp_time_update( U32 ticks ){    U32 temp_ms;    g_ticks += ticks;    temp_ms = g_low_ms;    g_low_ms += ticks*MILLISECONDS_PER_TICK;    if( temp_ms > g_low_ms )    {        g_high_ms++;    }    if( g_ticks < CONFIG_TICKS_PER_SECOND )    {        return;    }    g_seconds++;         g_ticks -= CONFIG_TICKS_PER_SECOND;     g_SysTime.second++;     if ( g_SysTime.second< 60 )    {        return;    }    g_SysTime.second= 0;    g_SysTime.minute++;    if ( g_SysTime.minute < 60 )    {        return;    }    g_SysTime.minute= 0;    g_SysTime.hour++;    if ( g_SysTime.hour < 24 )    {        return;    }        g_SysTime.hour= 0;    g_SysTime.date++;    g_SysTime.week++;    if( g_SysTime.week > 7 )    {        g_SysTime.week = 1;    }                if ( g_SysTime.date> ssp_days_per_month( g_SysTime.year, g_SysTime.month) )    {        g_SysTime.date= 1;        g_SysTime.month++;                    if ( g_SysTime.month> 12 )        {            g_SysTime.month= 1;            g_SysTime.year++;        }    }}VOID aos_time_add_1s( SYS_TIME_S *pstTime ){    pstTime->second++;    if ( pstTime->second< 60 )    {        return;    }    pstTime->second= 0;    pstTime->minute++;    if ( pstTime->minute < 60 )    {        return;    }    pstTime->minute= 0;    pstTime->hour++;    if ( pstTime->hour < 24 )    {        return;    }        pstTime->hour= 0;    pstTime->date++;    pstTime->week++;    if( pstTime->week > 7 )    {        pstTime->week = 1;    }                if ( pstTime->date> ssp_days_per_month( pstTime->year, pstTime->month) )    {        pstTime->date= 1;        pstTime->month++;                    if ( pstTime->month> 12 )        {            pstTime->month= 1;            pstTime->year++;        }    }}VOID aos_time_add_ms( SYS_TIME_S *pstTime, U32 millsec ){    U32 delta;        pstTime->millisec += millsec;    if( pstTime->millisec < 1000 )    {        return;    }    delta = pstTime->millisec / 1000;    pstTime->millisec %= 1000;    delta += pstTime->second;    if( delta < 60 )    {        pstTime->second = (U8)delta;        return;    }    pstTime->second = (U8)(delta % 60);    delta /= 60;       delta += pstTime->minute;    if ( delta < 60 )    {        pstTime->minute = (U8)delta;        return;    }    pstTime->minute = (U8)(delta % 60);    delta /= 60;    delta += pstTime->hour;    if ( delta < 24 )    {        pstTime->hour = (U8)delta;        return;    }    pstTime->hour = (U8)(delta %24);    delta = delta / 24;        while( delta-- > 0 )    {        pstTime->date++;        pstTime->week++;        if( pstTime->week > 7 )        {            pstTime->week = 1;        }                        if ( pstTime->date> ssp_days_per_month( pstTime->year, pstTime->month) )        {            pstTime->date= 1;            pstTime->month++;                            if ( pstTime->month> 12 )            {                pstTime->month= 1;                pstTime->year++;            }        }    }}VOID aos_time_sub_ms( SYS_TIME_S *pstTime, U32 millsec ){    U32 s1, s2;        s1 = millsec % 1000;    s2 = millsec / 1000;    if( pstTime->millisec < s1 )    {        s2++;        pstTime->millisec = 1000 + pstTime->millisec - s1;    }    else    {        pstTime->millisec = pstTime->millisec - s1;    }    if( !s2 )    {        return;    }        s1 = s2 % 60;    s2 = s2 / 60;    if( pstTime->second < s1 )    {        s2++;        pstTime->second = 60 + pstTime->second - s1;    }    else    {        pstTime->second = pstTime->second - s1;    }    if( !s2 )    {        return;    }        s1 = s2 % 60;    s2 = s2 / 60;    if( pstTime->minute < s1 )    {        s2++;        pstTime->minute = (U8)(60 + pstTime->minute - s1);    }    else    {        pstTime->minute = (U8)(pstTime->minute - s1);    }    if( !s2 )    {        return;    }            s1 = s2 % 24;    s2 = s2 / 24;    if( pstTime->hour < s1 )    {        s2++;        pstTime->hour = (U8)(24 + pstTime->hour - s1);    }    else    {        pstTime->hour = (U8)(pstTime->hour - s1);    }    if( !s2 )    {        return;    }            for( ;; )    {        if( s2 < pstTime->date )        {            pstTime->date = (U8)(pstTime->date-s2);            break;        }        else        {            s2 -= pstTime->date;            pstTime->month--;            if( 0 == pstTime->month )            {                pstTime->year--;                pstTime->month = 12;            }            pstTime->date = (U8)ssp_days_per_month( pstTime->year, pstTime->month );        }    }}U32 ssp_days_per_month( U32 year, U32 month ){	U32 max_date = 31;	switch ( month )	{		case 1  :		case 3  :		case 5  :		case 7  :		case 8  :		case 10 :		case 12 :			 max_date = 31 ;			 break ;		case 4  :		case 6  :		case 9  :		case 11 :			 max_date = 30 ;			 break ;		case 2  :			 if (( year % 4 ) == 0 )			 {				 if((year % 100) == 0)				 {					if ((year % 400) == 0)					   max_date = 29 ;					else					   max_date = 28;				 }				 else  max_date = 29;			 }			 else max_date = 28 ;			 break ;		default :			 AOS_ASSERT( 0 );			 break ;	} 	return( max_date );} AOS_INLINE S32 comp_sys_time ( SYS_TIME_S* pFirstTime, SYS_TIME_S* pSecondTime ){	U32  lFirstDay = 0, lSecondDay = 0;    AOS_ASSERT( NULL != pFirstTime && NULL != pSecondTime );    if((pFirstTime == NULL) || (pSecondTime == NULL))    {        return (S32)pFirstTime - (S32)pSecondTime;    }    	lFirstDay =    pFirstTime->year;	lFirstDay <<=  8;	lFirstDay +=   pFirstTime->month;	lFirstDay <<=  8;	lFirstDay +=   pFirstTime->date;	lSecondDay =   pSecondTime->year;	lSecondDay <<= 8;	lSecondDay +=  pSecondTime->month;	lSecondDay <<= 8;	lSecondDay +=  pSecondTime->date;	return (S32)(lFirstDay-lSecondDay);}#ifdef __cplusplus#if __cplusplus}#endif #endif 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -