问题标签 [time.h]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
3 回答
12321 浏览

c - c 中的时间戳,精度为毫秒

我对 C 编程比较陌生,我正在做一个需要非常准确的项目;因此我试图写一些东西来创建一个毫秒精度的时间戳。

它似乎有效,但我的问题是这种方式是否正确,还是有更简单的方法?这是我的代码:

0 投票
4 回答
13372 浏览

c++ - c/c++ 微秒时间戳

我使用这段代码在 c/c++ 中以微秒为单位获取时间戳。但它看起来不像微秒。我也不知道有没有办法格式化。

什么是正确的输出格式?谢谢!

0 投票
1 回答
61 浏览

time.h - time.h 关于疑问请解释

有人可以清除我对此代码的以下疑虑:

1)这个时钟()是什么?它是如何工作的?

2)我从来没有见过;afterfor语句我什么时候需要使用这些分号for ,它是否会循环通过唯一的下一行代码int tick=clock(); ?

3)他是否通过这样做来将滴答声转换为浮动:(float)tick?我可以对我首先初始化为short、integer、long、long long的每个变量执行此操作,然后仅通过执行 float(variable_name) 将其更改为 float 吗?

提前致谢

0 投票
1 回答
115 浏览

c++ - mktime 什么时候可以返回 -1?

我在mktime cppreference中看到了这一点:

自 epoch 以来的时间作为 std::time_t 对象成功或 -1 如果时间不能表示为 std::time_t 对象。

我开始想知道这什么时候会发生。谁能举一个mktime返回-1的例子?

0 投票
2 回答
1253 浏览

c - socket doesn't wait the expected 10 seconds

In my program i am using timeval structure of time.h for a TCP/IP socket program, in which the client waits for a timeout value as specified by this structure value the structure initialization is as below

and setting socket options as is. Since recv() is a blocking call I've put a timeout:

and receive data using recv() function. So to verify whether delay is ok I used two variables start & stop of type time_t:

so as per the definitions what I expect is the recv() functions waits for maximum of 10 seconds until data is received via socket. From the server side I didn't send anything. But upon calculating the difference the value I've obtained is 10 but I didn't feel a 10 second delay for reception, but just in the range of milliseconds, so I assume it only took about 10 millisecond

What might be the issue?? Any thoughts?


[update from comment]

My socket is non-blocking that's why I used setsocketopt() function, and I want to wait for a timeout value of 10 Seconds ,ie; if within 10seconds no data is received I have to exit from the recv() function...

0 投票
1 回答
134 浏览

c++ - c++程序(矩阵和函数)

请帮帮我!我需要做一个随机打印 0 和 1 的 6x6 矩阵的程序。最难的部分是程序必须显示 (0,0) 和 (5,5) 之间的道路在左、右、上和下移动,显示每个坐标。如果没有路,程序就必须说出来。

一个例子:

(0,0)-(0,1)-(1,1)-(2,1)-(2,2)-(2,3)-(3,3)-(3,4)-(3 ,5)-(4-5)-(5-5)

在这里我必须完成 cpp。

请帮帮我D:!我不知道该怎么做。

PD:对不起,我的英语。

0 投票
1 回答
1327 浏览

c++ - 比较文件统计时间

我需要注意在基于 Unix 的系统上对文件的修改,但我无权访问 Boost。我想知道以下逻辑是否合理。我认为它可能效率低下,而且我知道我在 while 循环中根本不睡觉而浪费了大量的周期,但我没有估计文件在修改之间要经过多长时间,我需要知道相对较快:

0 投票
1 回答
724 浏览

c++ - 为什么使用 mktime 处理指针和非指针时 difftime() 会导致不同的结果?

我尝试使用

以两种不同的方式计算两个不同时间之间的差异。只是为了好奇!我发现它会导致不同的结果,失败和成功。我不知道为什么会这样?

为什么会在以下两种情况下导致不同的结果?

0 投票
2 回答
8553 浏览

c - 如何正确设置 sem_timedwait 的 timespec 以防止 EINVAL 错误

我正在尝试使用 sem_timedwait() 重复锁定和解锁信号量。根据此处的示例,我以以下方式将我的 struct timespec 设置为 20 毫秒超时:

使用上面的代码,我的程序在运行一段时间后会一直失败,并出现 EINVAL 错误代码。调试后,我意识到失败是由于 ts.tv_nsec 在一段时间后超过 1000000000 造成的。我目前的解决方法如下:

我想知道 - 有没有更好的方法来做到这一点而不必自己直接调整 timespec 值?

0 投票
2 回答
271 浏览

c - asctime() 奇怪的行为



我有以下代码。

基本上,我想获得 t1 上的当前时间,然后(在等待 i 被读取造成的延迟之后)t2 上的当前时间。t1 和 t2 不同,T1.tm_sec 和 T2.tm_sec 也不同,但 printf 显示的日期完全相同(对应于 T1 的日期)。我知道在内部,asctime(http://www.cplusplus.com/reference/ctime/asctime/?kw=asctime)使用静态指针,但我尝试了一个较小的示例程序,其函数返回一个静态字符指针,他们工作。

我像这样使用它

它有效(打印的字符串不同)。

所以我想它不可能来自那里。我还复制了 asctime 代码(来自 cplusplus)并将一些 printfs 放入函数中,我注意到结果变量在 sprintf 语句之后得到正确更新。

那么 printf 显示相同的日期会发生什么?

来自 cplusplus.com 的 asctime 代码