我搜索我的Linux盒子,看到这样的类型定义:
typedef __time_t time_t;
但是我找不到__time_t的定义。
我搜索我的Linux盒子,看到这样的类型定义:
typedef __time_t time_t;
但是我找不到__time_t的定义。
在Visual Studio 2008中,它默认为__int64,除非你定义了_USE_32BIT_TIME_T。最好是假装不知道它的定义,因为它可以(也将)在不同平台之间改变。
[根]#猫时间
#include <time.h>
int main(int argc, char** argv)
{
time_t test;
return 0;
}
[root]# gcc -E time.c | grep __time_t .c
类型定义long int __time_t;
它在$INCDIR/bits/types.h中定义:
# 131 "/usr/include/bits/types.h" 3 4
# 1 "/usr/include/bits/typesizes.h" 1 3 4
# 132 "/usr/include/bits/types.h" 2 3 4
在大多数传统平台上,它是一个32位有符号整数类型。然而,这会导致您的代码遭受2038年的错误。因此,现代C库应该将其定义为有符号的64位整型,这在几十亿年里是安全的。
time_t维基百科的文章给出了一些解释。归根结底,在C规范中,time_t的类型是不保证的。
The time_t datatype is a data type in the ISO C library defined for storing system time values. Such values are returned from the standard time() library function. This type is a typedef defined in the standard header. ISO C defines time_t as an arithmetic type, but does not specify any particular type, range, resolution, or encoding for it. Also unspecified are the meanings of arithmetic operations applied to time values. Unix and POSIX-compliant systems implement the time_t type as a signed integer (typically 32 or 64 bits wide) which represents the number of seconds since the start of the Unix epoch: midnight UTC of January 1, 1970 (not counting leap seconds). Some systems correctly handle negative time values, while others do not. Systems using a 32-bit time_t type are susceptible to the Year 2038 problem.
time_t只是8字节的类型定义(long long/__int64),所有编译器和操作系统都理解。在过去,它只用于长int(4字节),但现在不是了。如果你查看crtdef .h中的time_t,你会发现这两种实现,但操作系统将使用long long。
通常,您将在bits或asm头目录中找到这些用于gcc的底层实现特定的typedefs。对我来说,是/usr/include/x86_64-linux-gnu/bits/types.h。
您可以只使用grep,或者使用Quassnoi建议的预处理器调用来查看哪个特定的头文件。
答案肯定是特定于实现的。为了确定你的平台/编译器,只需要在你的代码中添加这个输出:
printf ("sizeof time_t is: %d\n", sizeof(time_t));
如果答案是4(32位),而你的数据意味着超过2038年,那么你有25年的时间来迁移你的代码。
如果你将数据存储为字符串,你的数据将会很好,即使它是一些简单的东西,如:
FILE *stream = [stream file pointer that you've opened correctly];
fprintf (stream, "%d\n", (int)time_t);
然后以同样的方式将其读入(fread, fscanf等读入int类型),就得到了纪元偏移时间。在. net中也存在类似的解决方案。我在Win和Linux系统之间传递64位的历元号没有问题(通过通信通道)。这会带来字节排序问题,但这是另一个主题。
为了回答paxdiablo的问题,我会说它打印了“19100”,因为程序是这样写的(我承认我自己在80年代也是这样写的):
time_t now;
struct tm local_date_time;
now = time(NULL);
// convert, then copy internal object to our object
memcpy (&local_date_time, localtime(&now), sizeof(local_date_time));
printf ("Year is: 19%02d\n", local_date_time.tm_year);
printf语句输出固定字符串“Year is: 19”,后面跟着一个由零填充的字符串“years since 1900”(tm->tm_year的定义)。在2000年,这个值显然是100。“%02d”填充两个零,但如果长度超过两个数字,则不会截断。
正确的方法是(只修改到最后一行):
printf ("Year is: %d\n", local_date_time.tm_year + 1900);
新问题:这种想法的基本原理是什么?
在64位机器上,Time_t的类型为long int,否则为long long int。
你可以在这些头文件中验证这一点:
time.h: /usr/include Types.h和typeszes .h: /usr/include/x86_64-linux-gnu/bits
(下面的陈述不是一个接一个的。他们可以在答复中找到。头文件使用Ctrl+f搜索。)
1)在time.h
typedef __time_t time_t;
2)在types.h
# define __STD_TYPE typedef
__STD_TYPE __TIME_T_TYPE __time_t;
3)在typesizes.h
#define __TIME_T_TYPE __SYSCALL_SLONG_TYPE
#if defined __x86_64__ && defined __ILP32__
# define __SYSCALL_SLONG_TYPE __SQUAD_TYPE
#else
# define __SYSCALL_SLONG_TYPE __SLONGWORD_TYPE
#endif
4)再次在types.h
#define __SLONGWORD_TYPE long int
#if __WORDSIZE == 32
# define __SQUAD_TYPE __quad_t
#elif __WORDSIZE == 64
# define __SQUAD_TYPE long int
#if __WORDSIZE == 64
typedef long int __quad_t;
#else
__extension__ typedef long long int __quad_t;
标准
威廉·布伦德尔引用了维基百科,但我更喜欢它来自马的嘴。
C99 N1256标准草案7.23.3 /3“时间组件”说:
声明的类型是size_t(在7.17中描述)clock_t和time_t,它们是能够表示时间的算术类型
6.2.5/18“类型”说明:
整型和浮动类型统称为算术类型。
POSIX 7 sys_types.h说:
[CX] time_t为整数类型。
其中[CX]定义为:
[CX] ISO C标准的扩展。
它是一种扩展,因为它提供了更强有力的保证:浮点数过时了。
gcc一行程序
不需要像Quassnoi提到的那样创建一个文件:
echo | gcc -E -xc -include 'time.h' - | grep time_t
在Ubuntu 15.10 GCC 5.2中,前两行是:
typedef long int __time_t;
typedef __time_t time_t;
命令分解和来自man gcc的一些引用:
-E:“预处理阶段结束后停止;不要正确运行编译器。” -xc:指定C语言,因为输入来自stdin,没有文件扩展名。 -include file: "处理文件,如果"#include "file""出现为主源文件的第一行。" -: stdin输入
time_t类型定义最终是什么?
健壮的代码并不关心类型是什么。
C物种time_t为实类型,如double, long long, int64_t, int等。
它甚至可以是无符号的,因为许多时间函数的返回值表明错误不是-1,而是(time_t)(-1) -这种实现选择并不常见。
关键是“需要知道”的类型很少。代码的编写应该避免这种需要。
然而,当代码想要打印原始time_t时,会出现一个常见的“需要知道的问题”。转换为最宽的整数类型将适应大多数现代情况。
time_t now = 0;
time(&now);
printf("%jd", (intmax_t) now);
// or
printf("%lld", (long long) now);
转换为双精度或长双精度也可以,但可能提供不精确的十进制输出
printf("%.16e", (double) now);
您可以使用typeid来找出time_t在您的系统中是如何定义的。
#include <iostream> // cout
#include <ctime> // time_t
#include <typeinfo> // typeid, name
using namespace std;
int main()
{
cout << "Test 1: The type of time_t is: \t\t"
<< typeid(time_t).name() << endl;
cout << "Test 2: time_t is a signed long?: \t"
<< (typeid(time_t) == typeid(signed long) ? "true" : "false") << endl;
cout << "Test 3: time_t is an unsigned long?: \t"
<< (typeid(time_t) == typeid(unsigned long) ? "true" : "false") << endl;
return 0;
}
在我的系统中,输出是:
Test 1: The type of time_t is: l Test 2: time_t is a signed long?: true Test 3: time_t is an unsigned long?: false