我在不同的 Ruby 安装上运行以下命令时遇到了差异:
Time.utc(2099, 12, 31, 23, 59, 59)
在某些系统上,我得到一个错误,在一些有效的响应上。
任何想法为什么会这样?
time_t
Ruby Time 类根据平台类型的大小定义其最大值和最小值。以下来自Ruby的time.c
文件:
#if SIZEOF_TIME_T == SIZEOF_LONG
typedef unsigned long unsigned_time_t;
#elif SIZEOF_TIME_T == SIZEOF_INT
typedef unsigned int unsigned_time_t;
#elif SIZEOF_TIME_T == SIZEOF_LONG_LONG
typedef unsigned LONG_LONG unsigned_time_t;
#else
# error cannot find integer type which size is same as time_t.
#endif
#define TIMET_MAX (~(time_t)0 <= 0 ?
(time_t)((~(unsigned_time_t)0) >> 1) : (~(unsigned_time_t)0))
#define TIMET_MIN (~(time_t)0 <= 0 ?
(time_t)(((unsigned_time_t)1) << (sizeof(time_t) * CHAR_BIT - 1)) : (time_t)0)
大小time_t
在不同平台上有所不同。在大多数现代平台上,time_t
是 64 位,这将允许您表示 2009 年 12 月 31 日的时间。在旧平台上(包括 Microsoft 的 Visual C++ .NET 2003 编译器)time_t
只有 32 位,这为您提供了 2038 年 1 月 19 日 03:14:07 UTC 的最大可能值。试图在这样的平台上创建你的时间会给你一个“时间超出范围”的错误。