tags:

views:

178

answers:

1

I'm running into discrepancies running the following command on different Ruby installations:

Time.utc(2099, 12, 31, 23, 59, 59) 

On some systems I get an error, on some a valid response.

Any ideas why this may be?

+2  A: 

The Ruby Time class defines its maximum and minimum values based on the size of the platform's time_t type. The following is from Ruby's time.c file:

#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)

The size of time_t varies on different platforms. On most modern platforms, time_t is 64 bits, which will allow your time on 31 December 2009 to be represented. On older platforms (including Microsoft's Visual C++ .NET 2003 compiler) time_t is only 32 bits, which gives you a maximum possible value of 19 January 2038 03:14:07 UTC. Trying to create your time on such a platform will give you a 'time out of range' error.

Phil Ross
I remember this being solved in newer releases though, so try updating is my addition to this anwser
ChaosR
This is definitely fixed in Ruby 1.9.2, which unfortunately is still quite a while away. Not sure about 1.9.1.
Jörg W Mittag
Ruby 1.9.1 compiled with Visual C++ .NET 2003 raises a 'time out of range' ArgumentError when running `Time.utc(2099,12,31)`.
Phil Ross