根据本文档http://www.cplusplus.com/reference/clibrary/ctime/time/
for time(NULL) “如果函数无法检索日历时间,则返回 -1 值。”
这是我应该在我的代码中检查的内容吗?如果 time(NULL) 没有检索到时间,肯定是出了什么问题。
最佳答案
您可能在没有实时时钟的嵌入式设备上运行。
glibc 源代码声称时间系统调用在 Linux 上不会失败:
time_t res = INTERNAL_SYSCALL (time, err, 1, NULL);
/* There cannot be any error. */
如果您查看内核源代码,确实是这种情况:
SYSCALL_DEFINE1(time, time_t __user *, tloc)
{
time_t i = get_seconds();
if (tloc) {
if (put_user(i,tloc))
return -EFAULT;
}
force_successful_syscall_return();
return i;
}
关于c - 在 Linux 上什么时候调用 time(NULL) 会失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1571937/