微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 无线和射频 > TI蓝牙设计交流 > 大家一起来找茬

大家一起来找茬

时间:10-02 整理:3721RD 点击:

以下的两个函数是实现年月日时分秒到秒数的相互转换。实际应用中发现问题,这种解bug的感觉不忍独享,特定分享出来给大家,哈哈哈哈

typedef uint32 UTCTime;

// To be used with
typedef struct
{
uint8 seconds; // 0-59
uint8 minutes; // 0-59
uint8 hour; // 0-23
uint8 day; // 0-30
uint8 month; // 0-11
uint16 year; // 2000+
} UTCTimeStruct;

/*********************************************************************
* @fn osal_ConvertUTCTime
*
* @brief Converts UTCTime to UTCTimeStruct
*
* @param tm - pointer to breakdown struct
*
* @param secTime - number of seconds since 0 hrs, 0 minutes,
* 0 seconds, on the 1st of January 2000 UTC
*
* @return none
*/
void osal_ConvertUTCTime( UTCTimeStruct *tm, UTCTime secTime )
{
// calculate the time less than a day - hours, minutes, seconds
{
uint32 day = secTime % DAY;
tm->seconds = day % 60UL;
tm->minutes = (day % 3600UL) / 60UL;
tm->hour = day / 3600UL;
}

// Fill in the calendar - day, month, year
{
uint16 numDays = secTime / DAY;
tm->year = BEGYEAR;
while ( numDays >= YearLength( tm->year ) )
{
numDays -= YearLength( tm->year );
tm->year++;
}

tm->month = 0;
while ( numDays >= monthLength( IsLeapYear( tm->year ), tm->month ) )
{
numDays -= monthLength( IsLeapYear( tm->year ), tm->month );
tm->month++;
}

tm->day = numDays;
}
}


/*********************************************************************
* @fn osal_ConvertUTCSecs
*
* @brief Converts a UTCTimeStruct to UTCTime
*
* @param tm - pointer to provided struct
*
* @return number of seconds since 00:00:00 on 01/01/2000 (UTC)
*/
UTCTime osal_ConvertUTCSecs( UTCTimeStruct *tm )
{
uint32 seconds;

/* Seconds for the partial day */
seconds = (((tm->hour * 60UL) + tm->minutes) * 60UL) + tm->seconds;

/* Account for previous complete days */
{
/* Start with complete days in current month */
uint16 days = tm->day;

/* Next, complete months in current year */
{
int8 month = tm->month;
while ( --month >= 0 )
{
days += monthLength( IsLeapYear( tm->year ), month );
}
}

/* Next, complete years before current year */
{
uint16 year = tm->year;
while ( --year >= BEGYEAR )
{
days += YearLength( year );
}
}

/* Add total seconds before partial day */
seconds += (days * DAY);
}

return ( seconds );
}

 最佳会员,谢谢分享

谢谢分享!

自己瞎写了一个,随便用用吧。

//主要的入参是指针时间cu_time;格式是0~6--->秒分时周日月年

//后面两个入参在这里没有作用。

//返回值为32位的unix北京时间。

uint32 time_to_unix(unsigned char *cu_time,unsigned char *unix,unsigned char flag)
{
uint32 temp;
uint16 inter=0;
unsigned char remainder,i;

//unsigned char run_flag=0;
unsigned char month[12]={31,28,31,30,31,30,31,31,30,31,30,31};

// cu_time[6]=RTC_Bcd2ToBin(cu_time[6]);
inter=cu_time[6]/4;
remainder=cu_time[6]%4;

if(remainder==0)
temp=10957+inter*1461; //days
else
temp=10957+inter*1461+remainder*365+1; //days
temp=temp*24*60*60-28800; //sec ,include beijing time(28800)

inter=0;
// cu_time[5]=RTC_Bcd2ToBin(cu_time[5]);
if (remainder==0) //runnian
{
for(i=0;i<cu_time[5]-1;i++)
{
inter=inter+month[i]; //days
}
if(cu_time[5]>2)
inter+=1;
}
else
{
for(i=0;i<cu_time[5]-1;i++)
{
inter=inter+month[i]; //days
}
}
temp=temp+inter*24*3600;

// cu_time[4]=RTC_Bcd2ToBin(cu_time[4]);
inter=cu_time[4]-1;
temp=temp+inter*24*3600;

// cu_time[2]=RTC_Bcd2ToBin(cu_time[2]);
inter=cu_time[2];
temp=temp+inter*3600;

// cu_time[1]=RTC_Bcd2ToBin(cu_time[1]);
inter=cu_time[1];
temp=temp+inter*60;

// cu_time[0]=RTC_Bcd2ToBin(cu_time[0]);
temp=temp+cu_time[0];
return temp;

}

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top