Sha256: 067eee12535c4f7ee6d1044db81d20e14594862a4a670ecd14ec9e3121b62a84

Contents?: true

Size: 1.5 KB

Versions: 5

Compression:

Stored size: 1.5 KB

Contents

require 'date'

# Allow date objects to do offsets by a time unit
# Date.today + Unit.new("1 week") => gives today+1 week
class Date
  alias unit_date_add +
  # @param [Object] other
  # @return [Unit]
  def +(other)
    case other
    when RubyUnits::Unit
      other = other.convert_to('d').round if %w[y decade century].include? other.units
      unit_date_add(other.convert_to('day').scalar)
    else
      unit_date_add(other)
    end
  end

  alias unit_date_sub -
  # @param [Object] other
  # @return [Unit]
  def -(other)
    case other
    when RubyUnits::Unit
      other = other.convert_to('d').round if %w[y decade century].include? other.units
      unit_date_sub(other.convert_to('day').scalar)
    else
      unit_date_sub(other)
    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 ? RubyUnits::Unit.new(self).convert_to(other) : RubyUnits::Unit.new(self)
  end

  # :nocov_19:
  unless Date.instance_methods.include?(:to_time)
    # @return [Time]
    def to_time
      Time.local(*ParseDate.parsedate(to_s))
    end
  end
  # :nocov_19:

  alias units_datetime_inspect inspect
  # @deprecated
  def inspect(dump = false)
    return units_datetime_inspect if dump
    to_s
  end

  unless Date.instance_methods.include?(:to_date)
    # :nocov_19:
    # @return [Date]
    def to_date
      Date.civil(year, month, day)
    end
    # :nocov_19:
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ruby-units-2.4.1 lib/ruby_units/date.rb
ruby-units-2.3.2 lib/ruby_units/date.rb
ruby-units-2.3.1 lib/ruby_units/date.rb
ruby-units-2.3.0 lib/ruby_units/date.rb
ruby-units-2.2.1 lib/ruby_units/date.rb