Sha256: af2959234acf3583db39c37f5624a5718aea286348b7a80e2901b90b0a5469df

Contents?: true

Size: 805 Bytes

Versions: 3

Compression:

Stored size: 805 Bytes

Contents

require 'timezone/parser/rule/on'

# A simple DSL for definining rules to parse the "ON" field in TZData rules.
module Timezone::Parser::Rule
  on 'lastDAY', /^last(\w+)$/, lambda{ |match, _, month, year|
    31.downto(1).each do |day|
      begin
        date = Time.strptime("#{year} #{month} #{day}", '%Y %b %d')

        if date.strftime('%a') == match[1]
          return [month, date.strftime('%d')]
        end
      rescue
        next
      end
    end
  }

  on 'DAY>=NUM', /^(\w+)>=(\d+)$/, lambda{ |match, _, month, year|
    start = Time.strptime("#{year} #{month} #{match[2]}", '%Y %b %d')

    (1..8).to_a.each do |plus|
      date = start + (plus * 24 * 60 * 60)

      if date.strftime('%a') == match[1]
        return [date.strftime('%b'), date.strftime('%d')]
      end
    end
  }
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
timezone-0.3.2 lib/timezone/parser/rule/on_rules.rb
timezone-0.3.1 lib/timezone/parser/rule/on_rules.rb
timezone-0.3.0 lib/timezone/parser/rule/on_rules.rb