三、初始化SNTP
设置三个时间服务器域名并初始化。
/**
@brief 初始化SNTP服务
@param 无
@return 无
*/
void ICACHE_FLASH_ATTR
SNTP_Init(void)
{
sntp_setservername(0,"0.cn.pool.ntp.org");
sntp_setservername(1,"1.cn.pool.ntp.org");
sntp_setservername(2,"2.cn.pool.ntp.org");
sntp_init();
}
/**
@brief 获取SNTP时间
@param pRealTime -[in&out] 实时时间
@return 无
*/
void ICACHE_FLASH_ATTR
GetSntpTime(char *pRealTime)
{
uint32_t time = sntp_get_current_timestamp();
os_sprintf(pRealTime, "%s", sntp_get_real_time(time));
os_sprintf(pRealTime, "%s", sntpTimeChangeToSimpleDateFormat(pRealTime));
}
由于通过 sntp_get_real_time()函数 获取出来的时间字符串格式为Thu Dec 27 22:40:21 2018
,不是我想要的格式,所以需要重新转换格式。
4.2 SNTP时间转化为简单日期格式
typedef struct sntpData_t
{
uint8 week;
uint8 month;
uint8 day;
uint8 hour;
uint8 minute;
uint8 second;
uint8 year;
} SntpData_t;
/**
@brief SNTP时间转化为简单日期格式
@param pSntpRealTime -[in] 实时时间
@return 简单日期格式时间字符串
*/
static char *ICACHE_FLASH_ATTR
sntpTimeChangeToSimpleDateFormat(char *pSntpRealTime)
{
if(!pSntpRealTime)
{
return "";
}
pSntpRealTime[24] = '