123456789_123456789_123456789_123456789_123456789_

Class: Timezone::Parser

Relationships & Source Files
Namespace Children
Classes:
Inherits: Object
Defined in: lib/timezone/parser.rb

Overview

Responsible for parsing timezone data into an exportable format.

Constant Summary

Class Method Summary

Instance Method Summary

Constructor Details

.new(root) ⇒ Parser

[ GitHub ]

  
# File 'lib/timezone/parser.rb', line 17

def initialize(root)
  @config = Config.new(root)
end

Instance Method Details

#parse(file) (private)

[ GitHub ]

  
# File 'lib/timezone/parser.rb', line 102

def parse(file)
  zone = file.gsub("#{@config.zoneinfo}/", '')
  print "Parsing #{zone}... "
  data = zdump(file)

  last = 0
  result = []

  data.split("\n").each do |line|
    match = line.gsub(/^#{file}\s+/, '').match(LINE)
    next if match.nil?

    line = Line.new(match)

    # If we're just repeating info, pop the last one and
    # add an inclusive rule.
    if result.last && result.last == line
      last -= result.last.source
      result.pop
    end

    temp = line.source
    line.source = line.source - last
    last = temp

    result << line
  end

  result << RefLine.new(@config, file) if result.empty?

  write(zone, result)
  puts 'DONE'
end

#perform

[ GitHub ]

  
# File 'lib/timezone/parser.rb', line 21

def perform
  FileUtils.rm_rf('data')

  Dir["#{@config.zoneinfo}/**/*"].each do |file|
    next if File.directory?(file)
    next if file.end_with?('.tab')
    next if IGNORE.include?(File.basename(file))

    parse(file)
  end
end

#write(zone, data) (private)

[ GitHub ]

  
# File 'lib/timezone/parser.rb', line 140

def write(zone, data)
  system("mkdir -p data/#{File.dirname(zone)}")
  f = File.open("data/#{zone}", 'w')
  f.write(data.map(&:to_s).join("\n"))
  f.close
end

#zdump(file) (private)

[ GitHub ]

  
# File 'lib/timezone/parser.rb', line 136

def zdump(file)
  `#{@config.zdump} -V -c #{MIN_YEAR},#{MAX_YEAR} #{file}`
end