中移物联ML302模组OpenCPU获取系统时间

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);

  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值