Class: TZInfo::Data::TZDataDayOfMonth

Inherits:
Object
  • Object
show all
Defined in:
lib/tzinfo/data/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.



1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
# File 'lib/tzinfo/data/tzdataparser.rb', line 1066

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.



1062
1063
1064
# File 'lib/tzinfo/data/tzdataparser.rb', line 1062

def day_of_month
  @day_of_month
end

#day_of_weekObject (readonly)

Returns the value of attribute day_of_week.



1063
1064
1065
# File 'lib/tzinfo/data/tzdataparser.rb', line 1063

def day_of_week
  @day_of_week
end

#operatorObject (readonly)

Returns the value of attribute operator.



1064
1065
1066
# File 'lib/tzinfo/data/tzdataparser.rb', line 1064

def operator
  @operator
end

#typeObject (readonly)

:nodoc:



1061
1062
1063
# File 'lib/tzinfo/data/tzdataparser.rb', line 1061

def type
  @type
end

Instance Method Details

#to_absolute(year, month) ⇒ Object

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



1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
# File 'lib/tzinfo/data/tzdataparser.rb', line 1084

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