gpt4 book ai didi

c - C 函数 time() 如何处理小数秒?

转载 作者:太空狗 更新时间:2023-10-29 15:54:08 27 4
gpt4 key购买 nike

time() 函数将返回自 1970 年以来的秒数。我想知道它如何对返回的秒数进行舍入。

比如100.4s,返回100还是101?

有明确的定义吗?

最佳答案

ISO C 标准并没有说明太多。它只说 time() 返回

the implementation’s best approximation to the current calendar time

结果是 time_t 类型,它是一个“能够表示时间”的实数类型(整数或 float )。

许多系统将 time_t 实现为有符号整数类型,表示自 1970-01-01 00:00:00 GMT 以来的秒数。

在我的 Ubuntu 系统上进行的快速实验(比较 time()gettimeofday() 返回的值)表明 time() 返回的值 被截断,因此例如如果高精度时间为 1514866171.750058,则 time() 函数将返回 1514866171。 ISO C 和 POSIX 都不能保证这一点,但我希望它在任何类 UNIX 系统上都能保持一致。

关于c - C 函数 time() 如何处理小数秒?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48054495/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com