lib/tzinfo/time_or_datetime.rb in tzinfo-0.2.2 vs lib/tzinfo/time_or_datetime.rb in tzinfo-0.3.0

- old
+ new

@@ -52,12 +52,12 @@ end end # Returns the time as a Time. def to_time - if @time.nil? - if !@timestamp.nil? + unless @time + if @timestamp @time = Time.at(@timestamp).utc else @time = Time.utc(year, mon, mday, hour, min, sec) end end @@ -65,20 +65,20 @@ @time end # Returns the time as a DateTime. def to_datetime - if @datetime.nil? + unless @datetime @datetime = DateTime.new(year, mon, mday, hour, min, sec) end @datetime end # Returns the time as an integer timestamp. def to_i - if @timestamp.nil? + unless @timestamp @timestamp = to_time.to_i end @timestamp end @@ -97,72 +97,77 @@ else "Timestamp: #{@orig.to_s}" end end + # Returns internal object state as a programmer-readable string. + def inspect + "#<#{self.class}: #{@orig.inspect}>" + end + # Returns the year. def year - if !@time.nil? + if @time @time.year - elsif !@datetime.nil? + elsif @datetime @datetime.year else to_time.year end end # Returns the month of the year (1..12). def mon - if !@time.nil? + if @time @time.mon - elsif !@datetime.nil? + elsif @datetime @datetime.mon else to_time.mon end end alias :month :mon # Returns the day of the month (1..n). def mday - if !@time.nil? + if @time @time.mday - elsif !@datetime.nil? + elsif @datetime @datetime.mday else to_time.mday end end alias :day :mday # Returns the hour of the day (0..23). def hour - if !@time.nil? + if @time @time.hour - elsif !@datetime.nil? + elsif @datetime @datetime.hour else to_time.hour end end # Returns the minute of the hour (0..59). def min - if !@time.nil? + if @time @time.min - elsif !@datetime.nil? + elsif @datetime @datetime.min else to_time.min end end # Returns the second of the minute (0..60). (60 for a leap second). def sec - if !@time.nil? + if @time @time.sec - elsif !@datetime.nil? + elsif @datetime @datetime.sec else to_time.sec end end @@ -241,10 +246,22 @@ end end end end + # Returns true if todt represents the same time and was originally + # constructed with the same type (DateTime, Time or timestamp) as this + # TimeOrDateTime. + def eql?(todt) + todt.respond_to?(:to_orig) && to_orig.eql?(todt.to_orig) + end + + # Returns a hash of this TimeOrDateTime. + def hash + @orig.hash + end + # If no block is given, returns a TimeOrDateTime wrapping the given # timeOrDateTime. If a block is specified, a TimeOrDateTime is constructed # and passed to the block. The result of the block must be a TimeOrDateTime. # to_orig will be called on the result and the result of to_orig will be # returned. @@ -269,13 +286,7 @@ end else t end end - - protected - # The time in the format it was originally constructed. - def orig - @orig - end end end