Sha256: 1c1d433cd3ba383ad16db527266aa35782034cbedba2622963ad5d0b181bb705

Contents?: true

Size: 1.22 KB

Versions: 24

Compression:

Stored size: 1.22 KB

Contents

class Time

  # Returns a new Time where one or more of the elements
  # have been changed according to the +options+ parameter.
  # The time options (hour, minute, sec, usec) reset
  # cascadingly, so if only the hour is passed, then
  # minute, sec, and usec is set to 0. If the hour and
  #  minute is passed, then sec and usec is set to 0.

  def change(options)
    opts={}; options.each_pair{ |k,v| opts[k] = v.to_i }
    self.class.send( self.utc? ? :utc : :local,
      opts[:year]  || self.year,
      opts[:month] || self.month,
      opts[:day]   || self.day,
      opts[:hour]  || self.hour,
      opts[:min]   || (opts[:hour] ? 0 : self.min),
      opts[:sec]   || ((opts[:hour] || opts[:min]) ? 0 : self.sec),
      opts[:usec]  || ((opts[:hour] || opts[:min] || opts[:usec]) ? 0 : self.usec)
    )
  end

end


#  _____         _
# |_   _|__  ___| |_
#   | |/ _ \/ __| __|
#   | |  __/\__ \ |_
#   |_|\___||___/\__|
#
=begin test

  require 'test/unit'
  require 'time'

  class TCTime < Test::Unit::TestCase

    def setup
      @t = Time.parse('4/20/2006 15:37')
    end

    def test_change
      n = Time.now
      n = n.change( :month=>4, :day=>20, :hour=>15, :min=>37, :year=>2006 )
      assert_equal( @t, n )
    end

  end

=end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
facets-1.0.3 packages/core/lib/facet/time/change.rb
facets-1.3.0 lib/facets/core/time/change.rb
facets-1.1.0 lib/facet/time/change.rb
facets-1.2.0 lib/facets/core/time/change.rb
facets-1.2.1 lib/facets/core/time/change.rb
facets-1.3.2 lib/facets/core/time/change.rb
facets-1.3.1 lib/facets/core/time/change.rb
facets-1.3.3 lib/facets/core/time/change.rb
facets-1.4.2 lib/facets/core/time/change.rb
facets-1.4.0 lib/facets/core/time/change.rb
facets-1.4.1 lib/facets/core/time/change.rb
facets-1.4.3 lib/facets/core/time/change.rb
facets-1.4.4 lib/facets/core/time/change.rb
facets-1.4.5 lib/facets/core/time/change.rb
facets-1.7.30 lib/facets/core/time/change.rb
facets-1.7.0 lib/facets/core/time/change.rb
facets-1.7.38 lib/facets/core/time/change.rb
facets-1.7.46 lib/facets/core/time/change.rb
facets-1.8.0 lib/facets/core/time/change.rb
facets-1.8.20 lib/facets/core/time/change.rb