关于获取以毫秒为单位的时间

发布于 2024-09-30 09:57:53 字数 630 浏览 0 评论 0原文

我正在 QNX 平台上使用 C 语言开发记录器,使用 Momnetics 以以下格式打印时间

2010-11-02 14:45:15.000

我能够使用获取日期、小时、分钟和秒

 time(&timeSpec);
 struct tm gmt;
 int iSysTimeSec = timeSpec;
 gmtime_r((time_t *)&iSysTimeSec, &gmt);
 sprintf(&MsgStamp[0], SYS_MSG_STAMP_PRINTF_FORMAT, gmt.tm_year+1900, gmt.tm_mon + 1, gmt.tm_mday, gmt.tm_hour, gmt.tm_min, gmt.tm_sec, iSysTimeMs );

问题是如何获得毫秒粒度使用 QNX Momentics。

我尝试使用 QNX 特定的方法获得毫秒级的粒度 int iSysTimeMs = ( (ClockCycles () * 1000) / SYSPAGE_ENTRY(qtime)->cycles_per_sec ) % 1000;

但我想采用这种 POSIX 方式,以便它是可移植的。我们该怎么做?

谢谢! 文卡塔

I am working on logger using C language on QNX platform using Momnetics to print time in following format

2010-11-02 14:45:15.000

I able to get date, hour, minutes, and seconds using

 time(&timeSpec);
 struct tm gmt;
 int iSysTimeSec = timeSpec;
 gmtime_r((time_t *)&iSysTimeSec, &gmt);
 sprintf(&MsgStamp[0], SYS_MSG_STAMP_PRINTF_FORMAT, gmt.tm_year+1900, gmt.tm_mon + 1, gmt.tm_mday, gmt.tm_hour, gmt.tm_min, gmt.tm_sec, iSysTimeMs );

Question is how do i get milliseconds granularity using QNX Momentics.

I tried to get granulaity for milliseconds using QNX specific
int iSysTimeMs = ( (ClockCycles () * 1000) / SYSPAGE_ENTRY(qtime)->cycles_per_sec ) % 1000;

but i want to do this POSIX way so that it is portable. How do we do this?

Thanks!
Venkata

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

邮友 2024-10-07 09:57:53

在QNX6中,您可以使用clock_gettime来获得最大粒度
系统允许。

struct timespec start;
clock_gettime( CLOCK_REALTIME, &start);

In QNX6 You can use the clock_gettime to have the max granularity
allowed by system.

struct timespec start;
clock_gettime( CLOCK_REALTIME, &start);
往昔成烟 2024-10-07 09:57:53

gettimeofday() 系统调用将返回一个结构,其中保存当前 Unix 时间(以秒为单位)以及属于当前秒的微秒数。

要获取总微秒数:

struct timeval tv;
gettimeofday(&tv, NULL);
u_int64_t now = tv.tv_sec * 1000000ULL + tv.tv_usec;

The gettimeofday() system call will return a structure holding the current Unix time in seconds and the number of microseconds belonging to the current second.

To get the total number of microseconds:

struct timeval tv;
gettimeofday(&tv, NULL);
u_int64_t now = tv.tv_sec * 1000000ULL + tv.tv_usec;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文