是否有一个更面向对象的替代在Linux中下使用函数gettimeofday()+ +?余像例如能够写类似的代码这样:

DateTime now = new DateTime;
DateTime duration = new DateTime(2300, DateTime.MILLISECONDS)
DateTime deadline = now + duration;

while(now < deadline){
    DoSomething();
    delete now;
    now = new DateTime()
}

在目标是一个嵌入式Linux系统,并且没有Boost库,但也许有个东西很容易端口(一些与头实现文件仅例如)。

有帮助吗?

解决方案 2

所以移植升压是不是我的目标的选项。相反,我不得不去的gettimeofday()。然而有与timeval中结构在sys/time.h

处理一些不错的宏
#include <sys/time.h>

void timeradd(struct timeval *a, struct timeval *b,
          struct timeval *res);

void timersub(struct timeval *a, struct timeval *b,
          struct timeval *res);

void timerclear(struct timeval *tvp);

void timerisset(struct timeval *tvp);

void timercmp(struct timeval *a, struct timeval *b, CMP);

过了一段时间来,虽然找到他们,因为他们在我的机器上的手册页则没有。看到这个页面: http://linux.die.net/man/3/timercmp

其他提示

有对于处理时间和时间间隔是这样的标准C ++库的一部分不面向对象的接口。

您可能想看看的Boost.Date_Time 虽然。 升压是非常有用和良好的书面其实际上与标准C的一部分++库。

我觉得你应该罚款用的东西从的ctime

#include <ctime>

time
difftime
mktime

应该做的工作在一个非常便携的方式。 cplusplus.com

/usr/include/linux/time.h 

给出的结构: -

struct timeval {
    time_t      tv_sec;     /* seconds */
    suseconds_t tv_usec;    /* microseconds */
};

在Linux上,可能是你可以使用它......

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top