On Windows I can call:
在Windows上我可以打电话:
_time32(__time32_t); // to get 32bit time_t
_time64(__time64_t); // to get 64bit time_t
(both in 32 and 64 bit programs)
(包括32位和64位程序)
Is there any way do this in Linux (compiling with GCC)?
在Linux中有什么方法可以这样做(使用GCC编译)吗?
3 个解决方案
#1
10
Apparently, no it's not possible. For starters, there is only one time()
function in Linux, no time32()
or time64()
.
显然,这是不可能的。对于初学者,Linux中只有一个time()函数,没有time32()或time64()。
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: (Source)
为了让libc获取当前时间,它需要为它执行一个系统调用:(Source)
time_t time (t) time_t *t;
{
// ...
INTERNAL_SYSCALL_DECL (err);
time_t res = INTERNAL_SYSCALL (time, err, 1, NULL);
// ...
return res;
}
The system call is defined as: (Source)
系统调用定义为:(源)
SYSCALL_DEFINE1(time, time_t __user *, tloc)
{
time_t i = get_seconds();
// ...
return i;
}
The function get_seconds()
returns an unsigned long
, like so: (Source)
函数get_seconds()返回一个无符号长,如so:(源代码)
unsigned long get_seconds(void)
{
struct timekeeper *tk = &timekeeper;
return tk->xtime_sec;
}
And timekeeper.xtime_sec
is actually 64-bit: (Source)
和计时员。xtime_sec实际上是64位的:(源代码)
struct timekeeper {
// ...
/* Current CLOCK_REALTIME time in seconds */
u64 xtime_sec;
// ...
}
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.
现在,如果您知道您的C,您就知道无符号长的大小实际上是依赖于实现的。在我的64位机器上,它是64位的;但是在我的32位机器上,是32位。在某种32位实现上,它可能是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.
另一方面,u64始终是64位的,所以从根本上说,内核以64位类型来跟踪时间。我无法理解为什么它接着返回一个无符号长(不保证是64位长)。
In the end, even if libc's would force time_t
to hold a 64-bit value, it wouldn't change a thing.
最后,即使libc强制time_t保留一个64位值,它也不会改变任何事情。
You could tie your application deeply into the kernel, but I don't think it's even worth it.
您可以将您的应用程序深入到内核中,但我认为它甚至不值得。
#2
6
No time64()/time32()
function are included into standard libraries.
标准库中不包含time64()/time32()函数。
No time32_t/time64_t
defines are contemplated in standard headers.
没有时间32_t/时间64_t定义在标准头中被考虑。
time_t
is defined into time.h
as typedef __time_t time_t
;
time_t定义为time。h为typedef __time_t time_t;
Following a long chain of redefines, you'll discover that __time_t
is defined as 32 bit on 32 bit machines and 64bit on 64 bit machines.
在一系列重新定义之后,您将发现__time_t在32位机器上定义为32位,在64位机器上定义为64位。
#3
0
If you really need this, why not roll your own?
如果你真的需要这个,为什么不自己动手呢?
typedef int32_t my_time32;
typedef int64_t my_time64;
my_time32 get_mytime32() {
if (sizeof(time_t) == sizeof(my_time32))
return time(NULL);
else {
/* Check for overflow etc. here... */
return (my_time32)(time(NULL));
}
}
And similarly for get_mytime64()
.
和同样get_mytime64()。
If you do not care about overflow, a simple return time(NULL);
would work for both functions thanks to C's implicit numeric conversions.
如果您不关心溢出,则返回一个简单的返回时间(NULL);由于C的隐式数值转换,这两个函数都可以工作。
#1
10
Apparently, no it's not possible. For starters, there is only one time()
function in Linux, no time32()
or time64()
.
显然,这是不可能的。对于初学者,Linux中只有一个time()函数,没有time32()或time64()。
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: (Source)
为了让libc获取当前时间,它需要为它执行一个系统调用:(Source)
time_t time (t) time_t *t;
{
// ...
INTERNAL_SYSCALL_DECL (err);
time_t res = INTERNAL_SYSCALL (time, err, 1, NULL);
// ...
return res;
}
The system call is defined as: (Source)
系统调用定义为:(源)
SYSCALL_DEFINE1(time, time_t __user *, tloc)
{
time_t i = get_seconds();
// ...
return i;
}
The function get_seconds()
returns an unsigned long
, like so: (Source)
函数get_seconds()返回一个无符号长,如so:(源代码)
unsigned long get_seconds(void)
{
struct timekeeper *tk = &timekeeper;
return tk->xtime_sec;
}
And timekeeper.xtime_sec
is actually 64-bit: (Source)
和计时员。xtime_sec实际上是64位的:(源代码)
struct timekeeper {
// ...
/* Current CLOCK_REALTIME time in seconds */
u64 xtime_sec;
// ...
}
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.
现在,如果您知道您的C,您就知道无符号长的大小实际上是依赖于实现的。在我的64位机器上,它是64位的;但是在我的32位机器上,是32位。在某种32位实现上,它可能是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.
另一方面,u64始终是64位的,所以从根本上说,内核以64位类型来跟踪时间。我无法理解为什么它接着返回一个无符号长(不保证是64位长)。
In the end, even if libc's would force time_t
to hold a 64-bit value, it wouldn't change a thing.
最后,即使libc强制time_t保留一个64位值,它也不会改变任何事情。
You could tie your application deeply into the kernel, but I don't think it's even worth it.
您可以将您的应用程序深入到内核中,但我认为它甚至不值得。
#2
6
No time64()/time32()
function are included into standard libraries.
标准库中不包含time64()/time32()函数。
No time32_t/time64_t
defines are contemplated in standard headers.
没有时间32_t/时间64_t定义在标准头中被考虑。
time_t
is defined into time.h
as typedef __time_t time_t
;
time_t定义为time。h为typedef __time_t time_t;
Following a long chain of redefines, you'll discover that __time_t
is defined as 32 bit on 32 bit machines and 64bit on 64 bit machines.
在一系列重新定义之后,您将发现__time_t在32位机器上定义为32位,在64位机器上定义为64位。
#3
0
If you really need this, why not roll your own?
如果你真的需要这个,为什么不自己动手呢?
typedef int32_t my_time32;
typedef int64_t my_time64;
my_time32 get_mytime32() {
if (sizeof(time_t) == sizeof(my_time32))
return time(NULL);
else {
/* Check for overflow etc. here... */
return (my_time32)(time(NULL));
}
}
And similarly for get_mytime64()
.
和同样get_mytime64()。
If you do not care about overflow, a simple return time(NULL);
would work for both functions thanks to C's implicit numeric conversions.
如果您不关心溢出,则返回一个简单的返回时间(NULL);由于C的隐式数值转换,这两个函数都可以工作。