Problem opening the time class

All,

I am trying to open the Time class to add the following method.
I am always getting this error:
NameError: uninitialized constant Time::ZoneOffset

when referencing ZoneOffset. What am I doing wrong?

class Time
def self.offset_to_zone(offset)
offset = offset.to_i
ZoneOffset.each_pair do |time_zone, zone_offset| #ERROR OCCURS
HERE!!!
return time_zone if (zone_offset == offset)
end
raise ArgumentError, “Illegal arguments! The offset " <<
offset.to_s << " is unknown.”
end
end

On Dec 31 2009, 4:52 pm, Cris S. [email protected] wrote:

offset = offset.to_i
ZoneOffset.each_pair do |time_zone, zone_offset| #ERROR OCCURS

HERE!!!
return time_zone if (zone_offset == offset)
end
raise ArgumentError, “Illegal arguments! The offset " <<
offset.to_s << " is unknown.”
end
end

Posted viahttp://www.ruby-forum.com/.

As the error status, there is no Time::ZoneOffset constant…

Are you copying/pasting random code from somewhere?
If not, what is ZoneOffset even supposed to be?

pharrington,

Thanks for the reply.

From time.rb (the standard library)

class Time
class << Time

ZoneOffset = {
  'UTC' => 0,
  # ISO 8601
  'Z' => 0,
  # RFC 822
  'UT' => 0, 'GMT' => 0,
  'EST' => -5, 'EDT' => -4,
  'CST' => -6, 'CDT' => -5,
  'MST' => -7, 'MDT' => -6,
  'PST' => -8, 'PDT' => -7,
  # Following definition of military zones is original one.
  # See RFC 1123 and RFC 2822 for the error in RFC 822.
  'A' => +1, 'B' => +2, 'C' => +3, 'D' => +4,  'E' => +5,  'F' => 

+6,
‘G’ => +7, ‘H’ => +8, ‘I’ => +9, ‘K’ => +10, ‘L’ => +11, ‘M’ =>
+12,
‘N’ => -1, ‘O’ => -2, ‘P’ => -3, ‘Q’ => -4, ‘R’ => -5, ‘S’ =>
-6,
‘T’ => -7, ‘U’ => -8, ‘V’ => -9, ‘W’ => -10, ‘X’ => -11, ‘Y’ =>
-12,
}

I am trying to access the constant within.

Thanks,

Cris

pharrington wrote:

On Dec 31 2009, 4:52�pm, Cris S. [email protected] wrote:

� � offset = offset.to_i
� � ZoneOffset.each_pair do |time_zone, zone_offset| #ERROR OCCURS
HERE!!!
� � � return time_zone if (zone_offset == offset)
� � end
� � raise ArgumentError, “Illegal arguments! �The offset " <<
offset.to_s << " is unknown.”
� end
end

Posted viahttp://www.ruby-forum.com/.

As the error status, there is no Time::ZoneOffset constant…

Are you copying/pasting random code from somewhere?
If not, what is ZoneOffset even supposed to be?

require ‘time’
=> true

Time::ZoneOffset
NameError: uninitialized constant Time::ZoneOffset
from (irb):2

(class << Time; self; end)::ZoneOffset
=> {“V”=>-9, “K”=>10, “PDT”=>-7, “W”=>-10, “L”=>11, “A”=>1, “X”=>-11,
“M”=>12, “B”=>2, “Y”=>-12, “N”=>-1, “C”=>3, “MST”=>-7, “GMT”=>0,
“O”=>-2, “D”=>4, “CST”=>-6, “Z”=>0, “UTC”=>0, “P”=>-3, “E”=>5, “Q”=>-4,
“F”=>6, “PST”=>-8, “EST”=>-5, “R”=>-5, “G”=>7, “MDT”=>-6, “S”=>-6,
“H”=>8, “CDT”=>-5, “T”=>-7, “I”=>9, “UT”=>0, “U”=>-8, “EDT”=>-4}