Sha256: 24b057cde63edb7305100ad61bbf43ef36366bbeeee0b1115ca34b529f693a07

Contents?: true

Size: 1.32 KB

Versions: 10

Compression:

Stored size: 1.32 KB

Contents

# = FILE
#
#   time/change.rb
#
# = DESCRIPTION
#
#   Time extension for changing time.
#
# = AUTHORS
#
#   CREDIT Thomas Sawyer

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

  class TestTimeChange < Test::Unit::TestCase

    def test_change
      t = Time.parse('4/20/2006 15:37')
      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

10 entries across 10 versions & 1 rubygems

Version Path
facets-2.0.0 lib/core/facets/time/change.rb
facets-2.0.1 lib/core/facets/time/change.rb
facets-2.0.2 lib/core/facets/time/change.rb
facets-2.0.3 lib/core/facets/time/change.rb
facets-2.0.4 lib/core/facets/time/change.rb
facets-2.0.5 lib/core/facets/time/change.rb
facets-2.1.0 lib/core/facets/time/change.rb
facets-2.1.1 lib/core/facets/time/change.rb
facets-2.1.2 lib/core/facets/time/change.rb
facets-2.1.3 lib/core/facets/time/change.rb