Class: Suprdate::Day

Inherits:
Unit
  • Object
show all
Defined in:
lib/suprdate/day.rb

Constant Summary collapse

STRFTIME_STR =
'%Y-%m-%d'

Instance Attribute Summary collapse

Attributes inherited from Unit

#value

Instance Method Summary collapse

Methods inherited from Unit

<=>, #==, #hash, #new, significance, #to_s

Methods included from Utility::CleanConstantName

#name_plural, #name_singular, #to_sym

Instance Attribute Details

#monthObject (readonly)

Returns the value of attribute month.



6
7
8
# File 'lib/suprdate/day.rb', line 6

def month
  @month
end

Instance Method Details

#+(increase) ⇒ Object

Return a new day incremented by an integer.



57
# File 'lib/suprdate/day.rb', line 57

def +(increase) new_from_date(date + increase) end

#-(decrease) ⇒ Object

Return a new day decremented by an integer.



60
# File 'lib/suprdate/day.rb', line 60

def -(decrease) new_from_date(date - decrease) end

#<=>(operand) ⇒ Object



68
69
70
71
# File 'lib/suprdate/day.rb', line 68

def <=>(operand)
  return -1 if operand == Infinity
  date <=> operand.day.date
end

#dateObject

This day as a Ruby Date object.



27
# File 'lib/suprdate/day.rb', line 27

def date() Date.new(*parts) end

#datetimeObject

This day as a Ruby DateTime object.



30
# File 'lib/suprdate/day.rb', line 30

def datetime() DateTime.new(*parts) end

#dayObject

Polymorphic feature; guarantees you a Suprdate::Day



36
# File 'lib/suprdate/day.rb', line 36

def day() self end

#daysObject

Polymorphic feature; guarantees you a list of Suprdate::Day



33
# File 'lib/suprdate/day.rb', line 33

def days() [self] end

#inspectObject



20
# File 'lib/suprdate/day.rb', line 20

def inspect() "#@month-#{@value.to_s.rjust(2, '0')}" end

#leap?Boolean

Whether this day is a leap day or, in other words, February 29th.

Returns:

  • (Boolean)


51
# File 'lib/suprdate/day.rb', line 51

def leap?() value == 29 && @month.value == 2 end

#new_from_date(date) ⇒ Object

:nodoc:



88
89
90
# File 'lib/suprdate/day.rb', line 88

def new_from_date(date) # :nodoc:
  new(@month.new(year.new(date.year), date.month), date.day)
end

#of_week_as_iObject

Day of the week as an integer. Presently Sunday is 1, Monday is 2 etc.



45
# File 'lib/suprdate/day.rb', line 45

def of_week_as_i() date.wday + 1 end

#of_week_as_sObject

Day of the week as a string. (See Suprdate::WEEKDAYS_AS_STR)



42
# File 'lib/suprdate/day.rb', line 42

def of_week_as_s() WEEKDAYS_AS_STR[of_week_as_i] end

#of_week_as_symObject

Day of the week as a symbol. (See Suprdate::WEEKDAYS_AS_SYM)



39
# File 'lib/suprdate/day.rb', line 39

def of_week_as_sym() WEEKDAYS_AS_SYM[of_week_as_i] end

#of_yearObject

Day of the year. January 1st is 1.



48
# File 'lib/suprdate/day.rb', line 48

def of_year() (date - Date.new(year.value, 1, 1)).numerator + 1 end

#partsObject

:nodoc:



86
# File 'lib/suprdate/day.rb', line 86

def parts() [year.value, @month.value, value] end

#since(operand) ⇒ Object

The number of days since parameter#day.



63
# File 'lib/suprdate/day.rb', line 63

def since(operand) (date - operand.day.date).numerator end

#succObject

Next successive day.



54
# File 'lib/suprdate/day.rb', line 54

def succ() self + 1 end

#timeObject

This day as a Ruby Time object.



24
# File 'lib/suprdate/day.rb', line 24

def time() Time.mktime(*parts) end

#until(operand) ⇒ Object

The number of days until parameter#day.



66
# File 'lib/suprdate/day.rb', line 66

def until(operand) (operand.day.date - date).numerator end

#weekday_occurrence_this_monthObject

If this day is the first Monday of the month this method returns 1. If this day is the second Monday of the month this method returns 2. Works for all days.



76
77
78
79
80
81
82
# File 'lib/suprdate/day.rb', line 76

def weekday_occurrence_this_month
  w = of_week_as_i
  w_days_this_month = @month.days[0..value - 1].select do |day|
    day.of_week_as_i == w
  end
  ORDINALS[w_days_this_month.nitems]
end

#yearObject



21
# File 'lib/suprdate/day.rb', line 21

def year() @month.year end