clock_settime 设置的是time的差值

#include <stdio.h>
#include <time.h>
#include <stdint.h>
#include <error.h>

int main()
{
        struct timespec tpset, tsreset;

        if (clock_gettime(CLOCK_REALTIME, &tsreset) != 0) {
                perror("clock_gettime() did not return success\n");
        }

        tpset.tv_sec = 0;
        tpset.tv_nsec = 0;

        clock_settime(CLOCK_REALTIME, &tpset);
        perror("clock_settime");
}


这段code 直接的结果会在调用clock_settime的时候提示非法参数,原因如下:
clock_settime->kc->clock_set(which_clock, &new_tp)->posix_clock_realtime_set->do_sys_settimeofday->do_sys_settimeofday64->do_settimeofday64
int do_settimeofday64(const struct timespec64 *ts)
{
    struct timekeeper *tk = &tk_core.timekeeper;
    struct timespec64 ts_delta, xt;
    unsigned long flags;
    int ret = 0;

    if (!timespec64_valid_strict(ts))
        return -EINVAL;

    raw_spin_lock_irqsave(&timekeeper_lock, flags);
    write_seqcount_begin(&tk_core.seq);

    timekeeping_forward_now(tk);

    xt = tk_xtime(tk);
    ts_delta.tv_sec = ts->tv_sec - xt.tv_sec;
    ts_delta.tv_nsec = ts->tv_nsec - xt.tv_nsec;
// 这里就换返回failed,从这里看到调用clock_settime 是设置和当前时间的差值,像上面的case,如果tpset.tv_sec = 0 和 tpset.tv_nsec = 0 都是零的话,这个差值会非常大,例如:clock_gettime(CLOCK_REALTIME, {1498723813, 202715700})
//因此在调用timespec64_compare时候就提示failed
    if (timespec64_compare(&tk->wall_to_monotonic, &ts_delta) > 0) {
        ret = -EINVAL;
        goto out;
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值