Sha256: 1cc4af19bfe122eb3e09535aa8026d3ea44b18043d84286eac4fe456f93c34f7

Contents?: true

Size: 1.52 KB

Versions: 6

Compression:

Stored size: 1.52 KB

Contents

require 'date'

# Allow date objects to do offsets by a time unit
# Date.today + U"1 week" => gives today+1 week
class Date
  alias :unit_date_add :+
  # @param [Object] unit
  # @return [Unit]
  def +(unit)
    case unit
    when Unit
      unit = unit.convert_to('d').round if ['y', 'decade', 'century'].include? unit.units 
      unit_date_add(unit.convert_to('day').scalar)
    else
      unit_date_add(unit)
    end
  end
 
  alias :unit_date_sub :-    
  # @param [Object] unit
  # @return [Unit]
  def -(unit)
    case unit
    when Unit 
      unit = unit.convert_to('d').round if ['y', 'decade', 'century'].include? unit.units 
      unit_date_sub(unit.convert_to('day').scalar)
    else
      unit_date_sub(unit)
    end
  end
  
  # Construct a unit from a Date
  # @example Date.today.to_unit => Unit
  # @return (see Unit#initialize)
  # @param [Object] other convert to same units as passed
  def to_unit(other = nil)
    other ? Unit.new(self).convert_to(other) : Unit.new(self)
  end
  alias :unit :to_unit
  
  # :nocov_19:
  unless Date.instance_methods.include?(:to_time)
    # @return [Time]
    def to_time
      Time.local(*ParseDate.parsedate(self.to_s))
    end
  end
  # :nocov_19:
  
  alias :units_datetime_inspect :inspect
  # @deprecated
  def inspect(raw = false)
    return self.units_datetime_inspect if raw
    self.to_s
  end
  
  unless Date.instance_methods.include?(:to_date)
    # :nocov_19:
    # @return [Date]
    def to_date
      Date.civil(self.year, self.month, self.day)
    end
    # :nocov_19:
  end
  
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
ruby-units-1.4.5 lib/ruby_units/date.rb
ruby-units-1.4.4 lib/ruby_units/date.rb
ruby-units-1.4.3 lib/ruby_units/date.rb
ruby-units-1.4.2 lib/ruby_units/date.rb
ruby-units-1.4.1 lib/ruby_units/date.rb
ruby-units-1.4.0 lib/ruby_units/date.rb