本文介绍了有什么办法可以在 Linux 的 32 位程序中获得 64 位的 time_t?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 在 Windows 上,我可以调用:On Windows I can call:_time32(__time32_t); // to get 32-bit time_t_time64(__time64_t); // to get 64-bit time_t(在 32 位和 64 位程序中)(both in 32 and 64-bit programs)在 Linux 中有没有办法做到这一点(使用 GCC 编译)?Is there any way do this in Linux (compiling with GCC)?推荐答案显然,不,这是不可能的.对于初学者来说,Linux 中只有一个 time() 函数,没有 time32() 或 time64().Apparently, no it's not possible. For starters, there is only one time() function in Linux, no time32() or time64().搜索了一会,发现不是libc的问题,罪魁祸首其实是内核.After searching for a while, I can see that it's not libc's fault, but the culprit is actually the kernel.为了让 libc 获取当前时间,它需要为它执行一个系统调用:In order for libc to fetch the current time, it need to execute a system call for it:time_t time (t) time_t *t;{ // ... INTERNAL_SYSCALL_DECL (err); time_t res = INTERNAL_SYSCALL (time, err, 1, NULL); // ... return res;}系统调用定义为:SYSCALL_DEFINE1(time, time_t __user *, tloc){ time_t i = get_seconds(); // ... return i;}函数 get_seconds() 返回一个 unsigned long,如下所示:The function get_seconds() returns an unsigned long, like so:unsigned long get_seconds(void){ struct timekeeper *tk = &timekeeper; return tk->xtime_sec;}而且 timekeeper.xtime_sec 实际上是 64 位的:struct timekeeper { // ... /* Current CLOCK_REALTIME time in seconds */ u64 xtime_sec; // ...}现在,如果你了解你的 C,你就会知道 unsigned long 的大小实际上是依赖于实现的.在我这里的 64 位机器上,它是 64 位的;但是在我这里的 32 位机器上,它是 32 位的.它可能在某些 32 位实现上是 64 位的,但不能保证.Now, if you know your C, you know that the size of unsigned long is actually implementation-dependant. On my 64-bit machine here, it's 64-bit; but on my 32-bit machine here, it's 32-bit. It possibly could be 64-bit on some 32-bit implementation, but there's no guarantee.另一方面,u64 始终是 64 位的,因此从根本上说,内核以 64 位类型跟踪时间.为什么它接着将其作为 unsigned long 返回,它不能保证是 64 位长,这超出了我的理解.On the other hand, u64 is always 64-bit, so at the very base, the kernel keeps track of the time in a 64-bit type. Why it then proceeds to return this as an unsigned long, which is not guaranteed to be 64-bit long, is beyond me.最后,即使 libc 会强制 time_t 保存 64 位值,它也不会改变任何事情.In the end, even if libc's would force time_t to hold a 64-bit value, it wouldn't change a thing.您可以将您的应用程序深深地绑定到内核中,但我认为它甚至不值得.You could tie your application deeply into the kernel, but I don't think it's even worth it. 这篇关于有什么办法可以在 Linux 的 32 位程序中获得 64 位的 time_t?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-27 23:24