time(NULL) 会 return 失败吗?
Can time(NULL) ever return failure?
如果传递的参数始终为 NULL,time_t time(time_t *t)
函数是否会 return 失败?
如果调用是 time(NULL)
,我们还需要检查 return 值吗?
唯一记录的错误代码是 EFAULT,它与指针无效有关。
是的。 time
有记录在案的 可能会失败 案例:
The time() function may fail if:
[EOVERFLOW] The number of seconds since the Epoch will not fit in an object of type time_t.
来源:http://pubs.opengroup.org/onlinepubs/9699919799/functions/time.html
预计这会在大约 22 年内在实践中发生,不会很快发生,而且不会发生在 64 位系统或使用 64 位 time_t
的 32 位系统上 time_t
。
此外,任何应失败或可能会失败情况的存在也允许实现定义的错误,尽管它们的存在将是严重的实施质量缺陷。
EFAULT
是一个 non-issue/non-existent 因为它只发生在你的程序有未定义的行为时。
所以尽管如此,在现实世界中,time
实际上不会失败。
(不考虑 POSIX 降级模式功能)
如果底层实时时钟子系统出现硬件故障,例如当单元在独立系统上关闭时时钟完整性(电池)丢失,time()
的 return 值当然可以 (time_t) -1
。在那种情况下,传入 time_t*
的内容没有区别。
我检查了 RHEL、SLES 和 UBTU; man 2 page 产生相同(相关)的东西:
time() returns the time since the Epoch (00:00:00 UTC, January 1, 1970), measured in seconds.
If t is non-NULL, the return value is also stored in the memory pointed to by t.
不管怎样,回到最初的问题
Q0: 如果传递的参数始终为 NULL,time_t time(time_t *t)
函数是否会 return 失败?
- A/R0:是,如果发生了一些非常特殊的事件(内存满,等等,... )
Q1: 如果调用是time(NULL),我们还需要检查return值吗?
- A/R1:实际答案是"NO",你不必; func 可以 return 相关的事实是另一回事。毕竟,如果不需要调用 func,为什么要调用它?
Q2: 唯一记录的错误代码是 EFAULT
,它与指针无效有关。
- 无效代码与您无关;正如你所说,你通过了
NULL
,所以没有问题。
在C标准中,time()
可以return (time_t)(-1)
if "the calendar time is not available"。例如,在 1999 标准中,即第 7.23.2.4 节第 3 段。
尽管该措辞不够具体,但我认为它代表了一种错误情况。大概一个实现可以 return (time_t)(-1)
如果它不能访问系统时钟,不能明智地解释它得到的数据,等等
R 的回答描述了 posix 规范的情况。
Can time(NULL) ever return failure?
没有。 C 标准说
C11:7.27.2.4:
The time function returns the implementation’s best approximation to the current calendar time. The value (time_t)(-1)
is returned if the calendar time is not available.
如果传递的参数始终为 NULL,time_t time(time_t *t)
函数是否会 return 失败?
如果调用是 time(NULL)
,我们还需要检查 return 值吗?
唯一记录的错误代码是 EFAULT,它与指针无效有关。
是的。 time
有记录在案的 可能会失败 案例:
The time() function may fail if:
[EOVERFLOW] The number of seconds since the Epoch will not fit in an object of type time_t.
来源:http://pubs.opengroup.org/onlinepubs/9699919799/functions/time.html
预计这会在大约 22 年内在实践中发生,不会很快发生,而且不会发生在 64 位系统或使用 64 位 time_t
的 32 位系统上 time_t
。
此外,任何应失败或可能会失败情况的存在也允许实现定义的错误,尽管它们的存在将是严重的实施质量缺陷。
EFAULT
是一个 non-issue/non-existent 因为它只发生在你的程序有未定义的行为时。
所以尽管如此,在现实世界中,time
实际上不会失败。
(不考虑 POSIX 降级模式功能)
如果底层实时时钟子系统出现硬件故障,例如当单元在独立系统上关闭时时钟完整性(电池)丢失,time()
的 return 值当然可以 (time_t) -1
。在那种情况下,传入 time_t*
的内容没有区别。
我检查了 RHEL、SLES 和 UBTU; man 2 page 产生相同(相关)的东西:
time() returns the time since the Epoch (00:00:00 UTC, January 1, 1970), measured in seconds.
If t is non-NULL, the return value is also stored in the memory pointed to by t.
不管怎样,回到最初的问题
Q0: 如果传递的参数始终为 NULL,
time_t time(time_t *t)
函数是否会 return 失败?- A/R0:是,如果发生了一些非常特殊的事件(内存满,等等,... )
Q1: 如果调用是time(NULL),我们还需要检查return值吗?
- A/R1:实际答案是"NO",你不必; func 可以 return 相关的事实是另一回事。毕竟,如果不需要调用 func,为什么要调用它?
Q2: 唯一记录的错误代码是
EFAULT
,它与指针无效有关。- 无效代码与您无关;正如你所说,你通过了
NULL
,所以没有问题。
- 无效代码与您无关;正如你所说,你通过了
在C标准中,time()
可以return (time_t)(-1)
if "the calendar time is not available"。例如,在 1999 标准中,即第 7.23.2.4 节第 3 段。
尽管该措辞不够具体,但我认为它代表了一种错误情况。大概一个实现可以 return (time_t)(-1)
如果它不能访问系统时钟,不能明智地解释它得到的数据,等等
R 的回答描述了 posix 规范的情况。
Can time(NULL) ever return failure?
没有。 C 标准说
C11:7.27.2.4:
The time function returns the implementation’s best approximation to the current calendar time. The value
(time_t)(-1)
is returned if the calendar time is not available.