Class: TimezoneParser::Data

Inherits:
Object
  • Object
show all
Defined in:
lib/timezone_parser/data.rb,
lib/timezone_parser/data/storage.rb

Overview

Timezone data

Direct Known Subclasses

RailsData, WindowsData

Defined Under Namespace

Classes: Storage

Constant Summary collapse

RootDir =

Library Root directory

Pathname.new(__FILE__).realpath.dirname.parent.parent
DataDir =

Path to Data directory

RootDir + 'data'
VendorDir =

Path to Vendor directory

RootDir + 'vendor'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeData

Returns a new instance of Data.



21
22
23
24
25
26
# File 'lib/timezone_parser/data.rb', line 21

def initialize
    @Offsets = SortedSet.new
    @Timezones = SortedSet.new
    @Types = SortedSet.new
    @Metazones = SortedSet.new
end

Instance Attribute Details

#MetazonesObject (readonly)

Returns the value of attribute Metazones.



20
21
22
# File 'lib/timezone_parser/data.rb', line 20

def Metazones
  @Metazones
end

#OffsetsObject (readonly)

Returns the value of attribute Offsets.



17
18
19
# File 'lib/timezone_parser/data.rb', line 17

def Offsets
  @Offsets
end

#TimezonesObject (readonly)

Returns the value of attribute Timezones.



18
19
20
# File 'lib/timezone_parser/data.rb', line 18

def Timezones
  @Timezones
end

#TypesObject (readonly)

Returns the value of attribute Types.



19
20
21
# File 'lib/timezone_parser/data.rb', line 19

def Types
  @Types
end

Class Method Details

.filterData(data, toTime, fromTime, type, regions) ⇒ Object



95
96
97
98
99
100
101
102
103
# File 'lib/timezone_parser/data.rb', line 95

def self.filterData(data, toTime, fromTime, type, regions)
    entries = []
    data.each do |entry|
        next if type and entry['Types'] and not entry['Types'].include?(type)
        next if not regions.empty? and entry['Countries'] and (entry['Countries'] & regions).empty?
        entries << entry
    end
    loadEntries(entries, toTime, fromTime, true)
end

.loadEntries(data, toTime, fromTime, offsets = false) ⇒ Array<Hash>

Load data entries which match specified time

Parameters:

  • data (Array<Hash>)

    array of entries

  • toTime (DateTime)

    entries before this date, exclusive

  • fromTime (DateTime)

    entries after/at this date, inclusive

Returns:

  • (Array<Hash>)

    resulting array containing filtered entries



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/timezone_parser/data.rb', line 72

def self.loadEntries(data, toTime, fromTime, offsets = false)
    result = []
    data.each do |entry|
        result << entry if (entry['From'] && entry['To'] and toTime > entry['From'] and fromTime < entry['To']) or
        (entry['From'] && !entry['To'] and toTime > entry['From']) or
        (!entry['From'] && entry['To'] and fromTime < entry['To']) or
        (!entry['From'] && !entry['To'])
    end
    result.each do |entry|
        return result if not offsets or (offsets and entry['Offset'])
    end
    data.each_index do |i|
        entry = data[i]
        nextentry = offsets ? getNextEntry(data, i) : data[i+1]
        result << entry if ( entry['From'] && entry['To'] and toTime > entry['From'] and fromTime < entry['To'] ) or
        ( entry['To'] and ( (nextentry.nil? and fromTime >= entry['To']) or
        (nextentry and nextentry['From'] and fromTime >= entry['To'] and toTime <= nextentry['From']) or
        (!entry['From'] and fromTime < entry['To']) ) ) or
        ( entry['From'] and ( (i.zero? and toTime <= entry['From']) or (!entry['To'] and toTime > entry['From']) ) )
    end
    result
end

Instance Method Details

#findOffsets(toTime, fromTime, regions = nil, types = nil) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/timezone_parser/data.rb', line 41

def findOffsets(toTime, fromTime, regions = nil, types = nil)
    types = @Types.to_a unless types
    types = [:daylight, :standard] if types.empty?
    @Timezones.each do |timezone|
        if regions and not regions.empty?
            timezoneRegions = Data::Storage.TimezoneCountries[timezone]
            next if timezoneRegions and (timezoneRegions & regions).empty?
        end
        begin
            tz = TZInfo::Timezone.get(timezone)
        rescue TZInfo::InvalidTimezoneIdentifier
            tz = nil
        end
        next unless tz
        offsets = []
        ts = false
        tz.transitions_up_to(toTime, fromTime).each do |transition|
            ts = true
            self.class.addOffset(offsets, transition.offset, types)
        end
        self.class.addOffset(offsets, tz.period_for_utc(toTime - 0.001).offset, types) unless ts
        @Offsets += offsets
    end
    @Offsets
end

#processEntry(entry, toTime, fromTime, regions = []) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/timezone_parser/data.rb', line 28

def processEntry(entry, toTime, fromTime, regions = [])
    @Timezones += entry['Timezones'] if entry['Timezones']
    @Offsets << entry['Offset'] if entry['Offset']
    @Types += entry['Types'].map(&:to_sym) if entry['Types']
    if entry.has_key?('Metazones')
        entry['Metazones'].each do |zone|
            @Metazones << zone
            @Timezones += Storage.getTimezones(zone, toTime, fromTime, regions)
        end
    end
    self
end