1.通过虚拟AT命令AT+CCLK?获得模组的当前时间;赋值给 struct l_tm指针变量tm。优点,返回结果为分解格式的时间,方便直接使用。确定,消耗时间较长。
/* @brief 获取时间函数
* @param[out] tm, struct l_tm类型的时间格式指针
* @param[out] timezone, 时区指针
* @retval true, 成功; false, 失败
*/
bool cm_get_rtc(struct l_tm *tm, int *timezone)
{
uint8_t buff[40];
unsigned int retry = 0;
while (retry < 3)
{
if (AT_OK == cm_at_cmd_exec("AT+CCLK?\r\n", 3000, "\r\nOK\r\n"))
{
cm_get_atrsp_line(buff, 40, "+CCLK:");
cm_atcmd_debug("%s() success, rtc: %s", __func__, buff);
if (7 == sscanf(buff, "%*[^\"]\"%2d/%2d/%2d,%2d:%2d:%2d%3d",
tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, timezone))
{
return true;
}
}
retry++;
osDelay(1000);
}
return false;
}
2. 通过osiEpochSecond()获得从1970-01-01 UTC到现在的秒数,然后再调用time_to_date转换。优点:速度快,调用简单。
/**
* @brief 获取1970-01-01 UTC到现在的秒数
* @return 秒数
*/
long osiEpochSecond(void);
/**
* @brief 把1970年到现在的秒数转换为年月日时分秒
* @param [in] time 秒数
* @param [in] t 时间结构体
* @return 空
*/
void time_to_date(long time,struct l_tm *t);