显然,不,这是不可能的。对于初学者,Linux 中只有一个time() 函数,没有time32() 或time64()。
搜索了一会,发现不是libc的错,而罪魁祸首其实是内核。
为了让 libc 获取当前时间,它需要对其执行系统调用:(Source)
time_t time (t) time_t *t;
{
// ...
INTERNAL_SYSCALL_DECL (err);
time_t res = INTERNAL_SYSCALL (time, err, 1, NULL);
// ...
return res;
}
系统调用定义为:(Source)
SYSCALL_DEFINE1(time, time_t __user *, tloc)
{
time_t i = get_seconds();
// ...
return i;
}
函数get_seconds() 返回一个unsigned long,如下所示:(Source)
unsigned long get_seconds(void)
{
struct timekeeper *tk = &timekeeper;
return tk->xtime_sec;
}
而timekeeper.xtime_sec实际上是64位的:(Source)
struct timekeeper {
// ...
/* Current CLOCK_REALTIME time in seconds */
u64 xtime_sec;
// ...
}
现在,如果你了解你的 C,你就会知道 unsigned long 的大小实际上是依赖于实现的。在我这里的 64 位机器上,它是 64 位的;但在我这里的 32 位机器上,它是 32 位的。 可能在某些 32 位实现上可能是 64 位,但不能保证。
另一方面,u64 始终是 64 位的,因此在最基础上,内核以 64 位类型跟踪时间。为什么它会继续以unsigned long 的形式返回它,它不能保证是 64 位长,这超出了我的理解。
最后,即使 libc 会强制 time_t 保存一个 64 位值,它也不会改变任何事情。
您可以将您的应用程序深深地绑定到内核中,但我认为这甚至不值得。