32 bit ruby 1.9.2p290 Time can't handle 2038?

364 views Asked by At

32-bit ruby 1.9.2p290 (which I must use), seems to be having a hard time with adding 25 (but not 24!) years to the current time.

now = Time.now
more_time = (24*365*24*60*60)
puts "more_time.class = #{more_time.class}"
later = now + more_time

now = Time.now
more_time = (25*365*24*60*60)
puts "more_time.class = #{more_time.class}"
later = now + more_time

Produces:

more_time.class = Fixnum

more_time.class = Fixnum

ruby_time.rb:11:in `+': time + 788400000.000000 out of Time range (RangeError) from ruby_time.rb:11

Am I running into a year 2038 problem? I don't have this issue with 64-bit ruby 1.9.2-p290.

1

There are 1 answers

0
Matt Bryant On BEST ANSWER

32 bit Ruby uses 32 bits to represent the time, therefore it has a valid range from 13 Dec 1901 20:45:54 UTC to 19 Jan 2038 03:14:07 UTC, as these are the minimum/maximum signed integer values representable with 32 bits, with time 0 being unix epoch time (1 Jan 1970 00:00:00 UTC).

64 bit Ruby uses 64 bits to represent the time, therefore it has a valid range of basically anything.

To fix this, you could look into using the DateTime class, which is not limited to 32 bits.