Sha256: d39f698539e58c6bb29cbab76c9048211c1ec9b1e173caf1de0db8858a1a8a13

Contents?: true

Size: 1.37 KB

Versions: 15

Compression:

Stored size: 1.37 KB

Contents

require 'timezone/error'

module Timezone # rubocop:disable Style/Documentation
  # Responsible for loading and parsing timezone data from files.
  module Loader
    ZONE_FILE_PATH = File.expand_path(File.dirname(__FILE__) + '/../../data')
    SOURCE_BIT = 0

    class << self
      def load(name)
        raise ::Timezone::Error::InvalidZone unless valid?(name)

        @rules ||= {}
        @rules[name] ||= parse_zone_data(get_zone_data(name))
      end

      def names
        @names ||= parse_zone_names
      end

      def valid?(name)
        names.include?(name)
      end

      private

      def parse_zone_names
        files = Dir[File.join(ZONE_FILE_PATH, '**/*')].map do |file|
          next if File.directory?(file)

          file.sub("#{ZONE_FILE_PATH}/", '')
        end

        files.compact
      end

      def parse_zone_data(data)
        rules = []

        data.split("\n").each do |line|
          source, name, dst, offset = line.split(':')
          source = source.to_i
          dst = dst == '1'
          offset = offset.to_i
          source = rules.last[SOURCE_BIT] + source if rules.last
          rules << [source, name, dst, offset]
        end

        rules
      end

      # Retrieve the data from a particular time zone
      def get_zone_data(name)
        File.read(File.join(ZONE_FILE_PATH, name))
      end
    end
  end

  private_constant :Loader
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
timezone-1.2.8 lib/timezone/loader.rb
timezone-1.2.7 lib/timezone/loader.rb
timezone-1.2.6 lib/timezone/loader.rb
timezone-1.2.5 lib/timezone/loader.rb
timezone-1.2.4 lib/timezone/loader.rb
timezone-1.2.3 lib/timezone/loader.rb
timezone-1.2.2 lib/timezone/loader.rb
timezone-1.2.1 lib/timezone/loader.rb
timezone-1.2.0 lib/timezone/loader.rb
timezone-1.1.1 lib/timezone/loader.rb
timezone-1.1.0 lib/timezone/loader.rb
timezone-1.0.0 lib/timezone/loader.rb
timezone-0.99.2 lib/timezone/loader.rb
timezone-0.99.1 lib/timezone/loader.rb
timezone-0.99.0 lib/timezone/loader.rb