Class: Date

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_units/date.rb

Overview

Allow date objects to do offsets by a time unit Date.today + Unit.new(“1 week”) => gives today+1 week

Instance Method Summary collapse

Instance Method Details

#+(other) ⇒ Unit

Parameters:

  • other (Object)

Returns:



9
10
11
12
13
14
15
16
17
# File 'lib/ruby_units/date.rb', line 9

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

#-(other) ⇒ Unit

Parameters:

  • other (Object)

Returns:



22
23
24
25
26
27
28
29
30
# File 'lib/ruby_units/date.rb', line 22

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

#inspect(dump = false) ⇒ Object

Deprecated.


51
52
53
54
# File 'lib/ruby_units/date.rb', line 51

def inspect(dump = false)
  return units_datetime_inspect if dump
  to_s
end

#to_dateDate

:nocov_19:

Returns:



59
60
61
# File 'lib/ruby_units/date.rb', line 59

def to_date
  Date.civil(year, month, day)
end

#to_timeTime

Returns:



43
44
45
# File 'lib/ruby_units/date.rb', line 43

def to_time
  Time.local(*ParseDate.parsedate(to_s))
end

#to_unit(other = nil) ⇒ Unit

Construct a unit from a Date

Examples:

Date.today.to_unit => Unit

Parameters:

  • other (Object) (defaults to: nil)

    convert to same units as passed

Returns:



36
37
38
# File 'lib/ruby_units/date.rb', line 36

def to_unit(other = nil)
  other ? RubyUnits::Unit.new(self).convert_to(other) : RubyUnits::Unit.new(self)
end

#unit_date_addObject



6
# File 'lib/ruby_units/date.rb', line 6

alias unit_date_add +

#unit_date_subObject



19
# File 'lib/ruby_units/date.rb', line 19

alias unit_date_sub -

#units_datetime_inspectObject

:nocov_19:



49
# File 'lib/ruby_units/date.rb', line 49

alias units_datetime_inspect inspect