这些 msec<->timeval 函数正确吗?

发布于 2024-08-19 12:54:22 字数 491 浏览 2 评论 0 原文

我的程序有一个错误,我不断地回到这两个函数,但它们对我来说看起来很正确。这里有什么问题吗?

long visual_time_get_msec(VisTime *time_)
{
    visual_log_return_val_if_fail(time_ != NULL, 0);

    return time_->tv_sec * 1000 + time_->tv_usec / 1000;
}


int visual_time_set_from_msec(VisTime *time_, long msec)
{
    visual_log_return_val_if_fail(time_ != NULL, -VISUAL_ERROR_TIME_NULL);


    long sec = msec / 1000;
    long usec = 0;

    visual_time_set(time_, sec, usec);

    return VISUAL_OK;
}

I have a bug in this program, and I keep coming back to these two functions, but they look right to me. Anything wrong here?

long visual_time_get_msec(VisTime *time_)
{
    visual_log_return_val_if_fail(time_ != NULL, 0);

    return time_->tv_sec * 1000 + time_->tv_usec / 1000;
}


int visual_time_set_from_msec(VisTime *time_, long msec)
{
    visual_log_return_val_if_fail(time_ != NULL, -VISUAL_ERROR_TIME_NULL);


    long sec = msec / 1000;
    long usec = 0;

    visual_time_set(time_, sec, usec);

    return VISUAL_OK;
}

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

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

发布评论

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

评论(2

梦里泪两行 2024-08-26 12:54:22

您的第一个函数是向下舍入的,因此 1.000999 秒四舍五入为 1000 毫秒,而不是 1001 毫秒。要解决这个问题(使其四舍五入到最接近的毫秒),您可以这样做:

long visual_time_get_msec(VisTime *time_)
{
    visual_log_return_val_if_fail(time_ != NULL, 0);

    return time_->tv_sec * 1000 + (time_->tv_usec + 500) / 1000;
}

Fuzz 已经指出了你的第二个例子中的截断 - 我唯一要补充的是,你可以使用模运算符稍微简化它:(

long sec = msec / 1000;
long usec = (msec % 1000) * 1000;

以上所有假设你没有处理负时间值- 如果你是,事情会变得更加复杂)。

Your first function is rounding down, so that 1.000999 seconds is rounded to 1000ms, rather than 1001ms. To fix that (make it round to nearest millisecond), you could do this:

long visual_time_get_msec(VisTime *time_)
{
    visual_log_return_val_if_fail(time_ != NULL, 0);

    return time_->tv_sec * 1000 + (time_->tv_usec + 500) / 1000;
}

Fuzz has already pointed out the truncation in your second example - the only thing I would add is that you can simplify it a little using the modulo operator:

long sec = msec / 1000;
long usec = (msec % 1000) * 1000;

(The above all assume that you're not dealing with negative timevals - if you are, it gets more complicated).

浪推晚风 2024-08-26 12:54:22

Visual_time_set_from_msec 看起来不正确...

如果有人调用 Visual_time_set_from_msec(time, 999),那么你的结构将被设置为零,而不是 999,000us。

你应该做的是:

// Calculate number of seconds
long sec = msec / 1000; 
// Calculate remainding microseconds after number of seconds is taken in to account
long usec = (msec - 1000*sec) * 1000;

这实际上取决于你的输入,但这就是我的 2 美分:-)

visual_time_set_from_msec doesnt look right...

if someone calls visual_time_set_from_msec(time, 999), then your struct will be set to zero, rather the 999,000us.

What you should do is:

// Calculate number of seconds
long sec = msec / 1000; 
// Calculate remainding microseconds after number of seconds is taken in to account
long usec = (msec - 1000*sec) * 1000;

it really depends on your inputs, but thats my 2 cents :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文