Class: TZInfo::TZDataDayOfMonth

Inherits:
Object
  • Object
show all
Defined in:
lib/tzinfo/tzdataparser.rb

Overview

A tz data day of the month reference. Can either be an absolute day, a last week day or a week day >= or <= than a specific day of month.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(spec) ⇒ TZDataDayOfMonth

Returns a new instance of TZDataDayOfMonth.



969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
# File 'lib/tzinfo/tzdataparser.rb', line 969

def initialize(spec)
  raise "Invalid on: #{spec}" if spec !~ /^([0-9]+)|(last([A-z]+))|(([A-z]+)([<>]=)([0-9]+))$/
  
  if $1
    @type = :absolute
    @day_of_month = $1.to_i
  elsif $3
    @type = :last
    @day_of_week = parse_day_of_week($3)
  else
    @type = :comparison
    @day_of_week = parse_day_of_week($5)
    @operator = parse_operator($6)
    @day_of_month = $7.to_i
  end
end

Instance Attribute Details

#day_of_monthObject (readonly)

Returns the value of attribute day_of_month.



965
966
967
# File 'lib/tzinfo/tzdataparser.rb', line 965

def day_of_month
  @day_of_month
end

#day_of_weekObject (readonly)

Returns the value of attribute day_of_week.



966
967
968
# File 'lib/tzinfo/tzdataparser.rb', line 966

def day_of_week
  @day_of_week
end

#operatorObject (readonly)

Returns the value of attribute operator.



967
968
969
# File 'lib/tzinfo/tzdataparser.rb', line 967

def operator
  @operator
end

#typeObject (readonly)

:nodoc:



964
965
966
# File 'lib/tzinfo/tzdataparser.rb', line 964

def type
  @type
end

Instance Method Details

#to_absolute(year, month) ⇒ Object

Returns the absolute day of month for the given year and month.



987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
# File 'lib/tzinfo/tzdataparser.rb', line 987

def to_absolute(year, month)
  case @type
    when :last
      last_day_in_month = (Date.new(year, month, 1) >> 1) - 1          
      offset = last_day_in_month.wday - @day_of_week
      offset = offset + 7 if offset < 0
      (last_day_in_month - offset).day
    when :comparison
      pivot = Date.new(year, month, @day_of_month)         
      offset = @day_of_week - pivot.wday
      offset = -offset if @operator == :less_equal
      offset = offset + 7 if offset < 0
      offset = -offset if @operator == :less_equal          
      result = pivot + offset
      if result.month != pivot.month
        puts self.inspect
        puts year
        puts month
      end
      raise 'No suitable date found' if result.month != pivot.month
      result.day          
    else #absolute
      @day_of_month
  end
end