sr_time.c
上传用户:fy98168
上传日期:2015-06-26
资源大小:13771k
文件大小:16k
- /**********************************************************************************
- File Name : sr_time.c
- Description : Time / Date Manager
- COPYRIGHT (C) STMicroelectronics 2004
- Date Modification Name
- ---- ------------ ----
- 8/18/2004 Created Louie
- <$ModMark> (do not move/delete)
- ***********************************************************************************/
- #ifndef __SR_TIME_C__ /* Prevent multiple inclusion of the file */
- #define __SR_TIME_C__
- /* C++ support */
- #ifdef __cplusplus
- extern "C" {
- #endif
- /* Includes ----------------------------------------------------------------- */
- #include <string.h>
- #include <time.h>
- #include "stcommon.h"
- #include "stdio.h"
- #include "sr_time.h"
- #include <debug.h>
- #include <task.h>
- #include "tbx.h"
- #include "usif.h"
- #include "usifORG.h"
- #include"db.h"//lsp 07/3/14
- /* Private Types ------------------------------------------------------------ */
- /* Private Constants -------------------------------------------------------- */
- /* UTC time in unix format: number of seconds elapsed since January 1, 1970 */
- #define U32_UTC_JANUARY_01_2004_1200_00AM 0x3FF36300
- #define IS_LEAP_YEAR(Y) ( ((Y)>0) && !((Y)%4) && ( ((Y)%100) || !((Y)%400) ) )
- /* Private Variables (static) ------------------------------------------------ */
- static U32 currentUTCtime;
- static task_t* ptidTimMgrTask;
- //static char monString[12][4] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
- // "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
- static char monString[12][3] = {"1", "2", "3", "4", "5", "6",
- "7", "8", "9", "10", "11", "12"};
- //static char WeekString[7][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
- static char WeekString[7][2] = {"7", "1", "2", "3", "4", "5", "6"};
- //static U32 daysPerMon[12]={31,28,31,30,31,30,31,31,30,31,30,31};//lsp 07/3/14
- static U32 monthToDaysInYear[13] = {
- 0,31,59,90,120,151,181,212,243,273,304,334,365
- };
- static semaphore_t tim_SemLock;
- /* Global Variables --------------------------------------------------------- */
- static int timeDateUserOffset;
- signed char xpdrTimeRef;
- //extern void SRUIF_UpdateBoxInfo(void);
- /* Private Macros ----------------------------------------------------------- */
- /* Private Function prototypes ---------------------------------------------- */
- static void TIM_ManagerProcess(void *pvParam);
- static ST_ErrorCode_t TIM_Protect(void);
- static ST_ErrorCode_t TIM_UnProtect(void);
- /* Functions ---------------------------------------------------------------- */
- /*-----------------------------------------------------------------------------
- * Function : TIM_Protect
- * Protect by semaphore shared data
- * Do the initialisation if needed.
- * Input : N/A
- * Output : N/A
- * Return : ErrCode
- * -------------------------------------------------------------------------- */
- static ST_ErrorCode_t TIM_Protect(void)
- {
- static BOOL tim_SemInit = FALSE;
- /* First check if init of the semaphore is already done */
- if (tim_SemInit == FALSE)
- {
- /* Init the semaphore to 1 and wait on the semaphore is the same as */
- /* setting directly the semaphore to 0. */
- semaphore_init_fifo(&tim_SemLock, 0);
- /* We are now in critical section */
- tim_SemInit = TRUE;
- }
- else
- {
- semaphore_wait(&tim_SemLock);
- }
- return ST_NO_ERROR;
- } /* TIM_Proctect */
- /*------------------------------------------------------------------------------
- * Function : TIM_UnProtect
- * Protect by semaphore shared data
- * Input : N/A
- * Output : N/A
- * Return : ErrCode
- * -------------------------------------------------------------------------- */
- static ST_ErrorCode_t TIM_UnProtect(void)
- {
- /* Wake up locked task on semaphore (if any) */
- semaphore_signal(&tim_SemLock);
- return ST_NO_ERROR;
- } /* TIM_UnProctect */
- /*******************************************************************************
- Name : S R T I M _ I n i t ( v o i d )
- Description : <$functionDescription>
- Parameters :
- Assumptions :
- Limitations :
- Returns : (ST_ErrorCode_t) <$returnDescription>
- *******************************************************************************/
- ST_ErrorCode_t SRTIM_Init(void)
- {
- currentUTCtime = 0; /* time not set */
- /*if(pstBoxInfo)
- {
- timeDateUserOffset = pstBoxInfo->timeDateUserOffset;
- xpdrTimeRef = pstBoxInfo->xpdrTimeRef;
- }
- else*/
- {
- //timeDateUserOffset = 0;//masked by lsp 07/3/14
- timeDateUserOffset=(int)KB_DBGetTimeDateUserOffset();//lsp 07/3/14
- printf("nSRTIM_Init(void) timeDateUserOffset=%un",timeDateUserOffset);
- //end lsp 07/3/14
- xpdrTimeRef = INVALID_LINK;
- }
- if ((ptidTimMgrTask = task_create(TIM_ManagerProcess,
- 0,
- 1024, //TMTM * 4,
- 5,
- "timemgr_task",
- 0)) == (task_t *) 0)
- {
- STTBX_Print(( "Failed to start Time Manager processn"));
- }
- else
- {
- STTBX_Print(( "nSuccessfully started Time Manager processn"));
- }
-
- return ST_NO_ERROR;
- }/* End of S R T I M _ I n i t ( v o i d ) ( ) */
- static void TIM_ManagerProcess(void *pvParam)
- {
- if(debugconnected())
- {
- currentUTCtime = (U32) time(NULL);
- }
- else
- currentUTCtime = 0;
- while(1)
- {
- task_delay(ST_GetClocksPerSecond());
- /* run every second */
- if(currentUTCtime)
- {
- TIM_Protect();
- currentUTCtime++;
- TIM_UnProtect();
- }
- }
- }
- //*****************************************************************************
- //名称:YMDtoMJD
- //
- //功能:将YMD时间转换成MJD时间
- //
- //输入:long Y 年long M 月long D 日
- //
- //返回:将转换好的字符串赋给MJD
- //注 :short &MJD MJD时间
- //*****************************************************************************
- void YMDtoMJD(unsigned long Y,unsigned long M,unsigned long D, unsigned short *MJD)
- {
- long L;
- if( M == 1 || M == 2)
- L = 1;
- else
- L = 0;
- *MJD = (unsigned short)(14956 +D + (((Y-1900) - L)*36525)/100 + ((M + 1 + L* 12)*306001)/10000);
- return;
- }
- //*****************************************************************************
- //名称:GetWeekDay
- //
- //功能:从输入的MJD时间得到今天是星期几
- //
- //输入:MJD时间
- //
- //返回: 0--周日, 1--周一; ..., 6
- //*****************************************************************************
- char GetWeekDay(unsigned short MJD)
- {
- char week_day;
-
- week_day = (MJD + 3) % 7;
- return week_day;
- }
- //*****************************************************************************
- //名称:GetWeekDay
- //
- //功能:从输入的MJD时间得到今天是星期几
- //
- //输入:MJD时间
- //
- //返回: 0--周日, 1--周一; ..., 6
- //*****************************************************************************
- int ConvertTimeStampToDateString(U32 timeStamp, char* out)
- {
- U32 year, month, day;
- U32 fullMinutes, fullHours,
- fullDays, fullDaysSince1968,
- numOfLeapYears, daysSinceCurLeapYear,
- fullYears, daysSinceStartOfYear, daysToMonth;
- unsigned short MJD;
- int week_day=-1;
- if(timeStamp == 0)
- {
- strcpy(out, "*******");
- return week_day;
- }
-
- fullMinutes = timeStamp / 60;
- fullHours = fullMinutes / 60;
- fullDays = fullHours / 24;
- fullDaysSince1968 = fullDays + 365 + 366;
- numOfLeapYears = fullDaysSince1968 / ((4 * 365) + 1);
- daysSinceCurLeapYear = fullDaysSince1968 % ((4 * 365) + 1);
- if((daysSinceCurLeapYear >= (31 + 29))) numOfLeapYears++;
- fullYears = (fullDaysSince1968 - numOfLeapYears) / 365;
- daysSinceStartOfYear = fullDaysSince1968 - (fullYears * 365) - numOfLeapYears;
- if((daysSinceCurLeapYear <= 365) && (daysSinceCurLeapYear >= 60)) daysSinceStartOfYear++;
- year = 1900 + fullYears + 68;
- month = 13;
- daysToMonth = 366;
- while (daysSinceStartOfYear < daysToMonth)
- {
- month--;
- daysToMonth = monthToDaysInYear[month];
- if((month >= 2) && ((year % 4) == 0)) daysToMonth++;
- }
- month++;
- day = daysSinceStartOfYear - daysToMonth + 1;
- YMDtoMJD(year,month,day, &MJD);
- week_day=(int)GetWeekDay(MJD);
- sprintf(out, "%s %02d %s %02d", WeekString[week_day],day, monString[month - 1], year%2000);
- return week_day;
- //sprintf(out, "%02d%s%02d", day, monString[month - 1], year%2000);
- }
- void ConvertTimeStampToTimeString(U32 timeStamp, char* out)
- {
- U32 days, hours, minutes;
- if(timeStamp == 0)
- {
- strcpy(out, "--:--");
- return;
- }
- days = timeStamp / 86400;
- timeStamp = timeStamp % 86400;
- hours = timeStamp /3600;
- timeStamp = timeStamp % 3600;
- minutes = timeStamp / 60;
- sprintf(out, "%02d:%02d", hours, minutes);
- }
- //*****************************************************************************
- //名称:ConvertTimeStampToHHMMSS
- //
- //功能:将时间转换成"HH:MM:SS"的格式输出
- //
- //输入:当前的格林威治时间
- //
- //返回:将转换好的字符串赋给out
- //注 :返回的时间字符串是当前时区的当地时间
- //*****************************************************************************
- void ConvertTimeStampToHHMMSS(U32 timeStamp, char* out)
- {
- U32 days, hours, minutes, seconds;
-
- if(timeStamp == 0)
- {
- strcpy(out, "0:00:00");
- return;
- }
- days = timeStamp / 86400;
- timeStamp = timeStamp % 86400;
- hours = timeStamp /3600;
- timeStamp = timeStamp % 3600;
- minutes = timeStamp / 60;
- seconds = timeStamp % 60;
- sprintf(out, "%d:%02d:%02d", hours, minutes, seconds);
- }
- //*****************************************************************************
- //名称:ConvertTimeStampToDate
- //
- //功能:将32位时间转换成年月日输出
- //
- //输入:32位时间
- //
- //输出:年月日
- //
- //返回:0--周日, 1--周一; ..., 6
- //*****************************************************************************
- int ConvertTimeStampToDate(U32 timeStamp, U32 *year, U32 *month, U32 *day)
- {
- U32 y, m, d;
- U32 fullMinutes, fullHours,
- fullDays, fullDaysSince1968,
- numOfLeapYears, daysSinceCurLeapYear,
- fullYears, daysSinceStartOfYear, daysToMonth;
- unsigned short MJD;
- int week_day;
- if(timeStamp == 0)
- {
- return -1;
- }
-
- fullMinutes = timeStamp / 60;
- fullHours = fullMinutes / 60;
- fullDays = fullHours / 24;
- fullDaysSince1968 = fullDays + 365 + 366;
- numOfLeapYears = fullDaysSince1968 / ((4 * 365) + 1);
- daysSinceCurLeapYear = fullDaysSince1968 % ((4 * 365) + 1);
- if((daysSinceCurLeapYear >= (31 + 29))) numOfLeapYears++;
- fullYears = (fullDaysSince1968 - numOfLeapYears) / 365;
- daysSinceStartOfYear = fullDaysSince1968 - (fullYears * 365) - numOfLeapYears;
- if((daysSinceCurLeapYear <= 365) && (daysSinceCurLeapYear >= 60)) daysSinceStartOfYear++;
- y = 1900 + fullYears + 68;
- m = 13;
- daysToMonth = 366;
- while (daysSinceStartOfYear < daysToMonth)
- {
- m--;
- daysToMonth = monthToDaysInYear[m];
- if((m >= 2) && ((y % 4) == 0)) daysToMonth++;
- }
- m++;
- d = daysSinceStartOfYear - daysToMonth + 1;
- YMDtoMJD(y,m,d, &MJD);
- week_day=(int)GetWeekDay(MJD);
- *year = y;
- *month = m;
- *day = d;
- return week_day;
- }
- //*****************************************************************************
- //名称:ConvertTimeStampToTime
- //
- //功能:将32位时间转换成时分秒输出
- //
- //输入:32位时间
- //
- //输出:时分秒
- //
- //返回:无
- //*****************************************************************************
- void ConvertTimeStampToTime(U32 timeStamp, U32 *hour, U32 *min, U32 *sec)
- {
- U32 d, h, m, s;
- d = timeStamp / 86400;
- timeStamp = timeStamp % 86400;
- h = timeStamp /3600;
- timeStamp = timeStamp % 3600;
- m = timeStamp / 60;
- s = timeStamp % 60;
- *hour = h;
- *min = m;
- *sec = s;
- }
- U32 GetU32CurrentTimeDate(void)
- {
- U32 retVal = 0;
- TIM_Protect();
- if(currentUTCtime)
- retVal = currentUTCtime + timeDateUserOffset;
- TIM_UnProtect();
- return retVal;
- }
- void SetU32CurrentTimeDate(U32 newTimeDate)
- {
- TIM_Protect();
- timeDateUserOffset = newTimeDate - (int) currentUTCtime;
- TIM_UnProtect();
- //SRUIF_UpdateBoxInfo();
- }
- void SetU32UTC(U32 newUTC)
- {
- TIM_Protect();
- currentUTCtime = newUTC;
- TIM_UnProtect();
- }
- U32 GetU32UTC(void* dummy)
- {
- U32 retVal;
- TIM_Protect();
- retVal = currentUTCtime;
- TIM_UnProtect();
- return retVal;
- }
- int GetTimeDateUserOffset(void)
- {
- U32 retVal;
- TIM_Protect();
- retVal = timeDateUserOffset;
- TIM_UnProtect();
- return retVal;
- }
- void SetTimeDateUserOffset(int newOffset)
- {
- TIM_Protect();
- timeDateUserOffset = newOffset;
- TIM_UnProtect();
- }
- void SetCurrentTime(U8 hours, U8 minutes, U8 seconds)
- {
- U32 timeStamp = GetU32CurrentTimeDate();
- timeStamp = timeStamp/(60 * 60 * 24); /* reference to 1200am of the same day */
- timeStamp = timeStamp * (60 * 60 * 24);
- timeStamp += (hours % 24) * 3600;
- timeStamp += (minutes % 60) * 60;
- timeStamp += (seconds % 60);
- SetU32CurrentTimeDate(timeStamp);
- }
- void SetCurrentDate(U16 year, U8 month, U8 date)
- {
- U32 timeStamp = GetU32CurrentTimeDate();
- //U32 timeStamp = 0;
- //U32 temp;//lsp 07/3/14
- U32 fullDays, curHours, fullHours, curMins, fullMins, curSecs, numDays;
- //if(year < 2004) return;//masked by lsp 07/3/14
- if(year <4) return;
- /* Extract the current time of the day */
- fullMins = timeStamp / 60;
- curSecs = timeStamp - (60 * fullMins);
- fullHours = fullMins / 60;
- curMins = fullMins - (60 * fullHours);
- fullDays = fullHours / 24;
- curHours = fullHours - (24 * fullDays);
- #if 0 /* BLOCK COMMENTED OUT by Louie on 1/3/2005 */
- /* Move the reference to Jan 01 2004 and add the current time */
- timeStamp += U32_UTC_JANUARY_01_2004_1200_00AM;
- /* add the number of years since 2004 in seconds, taking into account leap years */
- while(temp > 2004)
- timeStamp += (IS_LEAP_YEAR(--temp)?366:365) * (60 * 60 * 24);
- temp = month - 1;
- while(temp > 0)
- timeStamp += daysPerMon[--temp] * (60 * 60 * 24);
- timeStamp += ((date > daysPerMon[month - 1])?daysPerMon[month - 1]:date) * (60 * 60 * 24);
- if(IS_LEAP_YEAR(year) && (month > 2)) timeStamp += (60 * 60 * 24);
- if(IS_LEAP_YEAR(year) && (month ==2)&&date >=29)
- {
- timeStamp += (60 * 60 * 24);
- }
- #else
- numDays = 365 * (year - 70) + monthToDaysInYear[month -1] + (date - 1);
- numDays += ((year - 69) / 4);
- if(((month - 1) > 1) && ((year % 4) == 0)) numDays++;
- timeStamp = curSecs + 60 * curMins + 3600 * (curHours + 24 * numDays);
- #endif /* BLOCK COMMENT */
- SetU32CurrentTimeDate(timeStamp);
- }
- U32 MJDtoUNIX(const U8* pMjdData)
- {
- #define MJD_1970 40587
- U32 temp;
- temp = (*(pMjdData) * 0x100) + (U8) *(pMjdData + 1);
- if(temp > MJD_1970)
- {
- temp = temp - MJD_1970; /* number of days since January 1, 1970 */
- temp = temp * 60 * 60 * 24; /* number of days (in seconds unit) */
- temp += PackedBcd24ToSeconds((U8*) (pMjdData + 2));
- return temp;
- }
- return 0;
- }
- U32 PackedBcd24ToSeconds(const U8* pBcd24BitTime)
- {
- U32 temp = 0;
- U8 byteTemp, byteTemp2;
- byteTemp = (U8) *(pBcd24BitTime);
- byteTemp2 = ((byteTemp >> 4) * 10) + (byteTemp & 0x0F);
- temp += (byteTemp2) * 60 * 60;
- byteTemp = (U8) *(pBcd24BitTime + 1);
- byteTemp2 = ((byteTemp >> 4) * 10) + (byteTemp & 0x0F);
- temp += (byteTemp2) * 60;
- byteTemp = (U8) *(pBcd24BitTime + 2);
- byteTemp2 = ((byteTemp >> 4) * 10) + (byteTemp & 0x0F);
- temp += (byteTemp2);
- return temp;
- }
- /* end C++ support */
- #ifdef __cplusplus
- }
- #endif
- #endif /* #ifndef __SR_TIME_C__ */
- /* ----------------------------- End of file (sr_time.c) ------------------------- */