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.



976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
# File 'lib/tzinfo/tzdataparser.rb', line 976

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.



972
973
974
# File 'lib/tzinfo/tzdataparser.rb', line 972

def day_of_month
  @day_of_month
end

#day_of_weekObject (readonly)

Returns the value of attribute day_of_week.



973
974
975
# File 'lib/tzinfo/tzdataparser.rb', line 973

def day_of_week
  @day_of_week
end

#operatorObject (readonly)

Returns the value of attribute operator.



974
975
976
# File 'lib/tzinfo/tzdataparser.rb', line 974

def operator
  @operator
end

#typeObject (readonly)

:nodoc:



971
972
973
# File 'lib/tzinfo/tzdataparser.rb', line 971

def type
  @type
end

Instance Method Details

#to_absolute(year, month) ⇒ Object

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



994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
# File 'lib/tzinfo/tzdataparser.rb', line 994

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