网上有很多关于秒级实现方式,找了很多方法。感觉ms级的实现很难找,本文总结一下自己关于VxWorks 毫秒级获取系统时间得办法。
直接贴例子吧
代码实现为系统运行天数和运行的毫秒数。
需要包含的头问价为 .. / project / h / time.h
UNS8 Timestamp_Send(void)
{
UNS8 err;
UNS32 days,s,ms;
Message m;
time_t seconds,temp;
struct tm * p;
struct timespec tp;
/* read real time */
time(&seconds);
days = seconds/(3600*24);
s = seconds%(3600*24);
/* Timestamp method */
clock_gettime ( CLOCK_REALTIME, &tp );
temp = tp.tv_sec;
ms = tp.tv_nsec/1000000;
ms = s*1000 + ms;
//p = localtime (&seconds);
/* print */
printf("....................................\n");
printf("seconds = %ld", seconds);
printf("days = %ld", days);
printf("....................................\n");
printf("temp = %ld", temp);
printf("ms = %ld", ms);
printf("....................................\n");
return 0;
}
如果需要转换成年月日,时分秒;
可以调用gmtime和localtime。gmtime()为没有考虑时区问题;localtime加入了时区计算;
上图中有包括localtime的实现方法,可参考。