This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CC3235S: TI的SDK和TIRTOS如何获取毫秒级的UNIX时间戳?我目前只能实现秒级的。

Part Number: CC3235S

To:各位
 

我这里用TI的SDK和TIRTOS实现了秒级的UNIX时间戳的获取。

代码如下所示:

time_t ret = 0;
int32_t status = 0;
struct tm netTime;

status = ClockSync_get(&netTime);
if((status == 0) || (status == CLOCKSYNC_ERROR_INTERVAL)){
//UART_PRINT("Local time = %s\n\r",asctime(&netTime));
netTime.tm_year = netTime.tm_year - 70; //TIOSは1900 UNIXは1970 1970-1900=70
ret = mktime(&netTime);
//UART_PRINT("GetUnixTime :%l\r\n", ret);
}else{
UART_PRINT("GetUnixTime Error\r\n");
}


问题是,我现在想获取到毫秒级的UNIX时间戳,我该如何实现?还请详细指导一下。在哪里引用哪一个库和头文件去实现。拜托了。

  • 您好,

    官网并没有毫秒级的unix时间戳实现方式,

    因此通过引用库和头文件的方式是走不通的。

    提供一个思路:

    1.在秒级时间戳的基础上乘以1000

    2.使用更高分辨率的定时器来获取格外的毫秒数 可以看一下GPTimer

    然后需要将time ()函数返回的秒级时间戳和毫秒数结合起来

    大致的代码思路

    time_t currentTimeIME = time(NULL);
    
    unit32_t timevalue = TimerValueGet(TIMER_BASE.TIMER_A);
    
    uint64_4 unixTimeMs=((uint64_t)currentTime * 1000) +timevalue

    S1:获取当前的秒级时间戳

    S2:获取当前的计时器值,(需要将计时器设置为1ms溢出一次)

    S3:time ()函数返回的秒级时间戳和毫秒数结合起来