Module: TimezoneParser::CLDR

Defined in:
lib/timezone_parser/data/cldr.rb

Overview

CLDR module

Constant Summary collapse

DataDir =

Data directory

Pathname.new(Cldr::Export::Data.dir)

Class Method Summary collapse

Class Method Details

.download(source = 'http://unicode.org/Public/cldr/latest/core.zip', target = nil) ⇒ Object



22
23
24
# File 'lib/timezone_parser/data/cldr.rb', line 22

def self.download(source = 'http://unicode.org/Public/cldr/latest/core.zip', target = nil)
    Cldr.download(source, target)
end

.getLocalesObject



43
44
45
46
47
48
# File 'lib/timezone_parser/data/cldr.rb', line 43

def self.getLocales
    unless @@Locales
        @@Locales = Cldr::Export::Data.locales.sort
    end
    @@Locales
end

.getMetazonesObject



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/timezone_parser/data/cldr.rb', line 163

def self.getMetazones
    zones = {}
    Cldr::Export::Data::Metazones.new[:timezones].each do |timezone, zonedata|
        zonedata.each do |data|
            entry = {}
            add = true
            zones[data['metazone']] ||= []
            zones[data['metazone']].each_index do |i|
                next if zones[data['metazone']][i]['From'].to_s != data['from'].to_s or zones[data['metazone']][i]['To'].to_s != data['to'].to_s
                entry = zones[data['metazone']][i]
                add = false
                break
            end
            self.updateHash(entry, 'Timezones', timezone)
            if add
                entry['From'] = data['from'].to_s if data['from']
                entry['To'] = data['to'].to_s if data['to']
                zones[data['metazone']] << entry
            end
            zones[data['metazone']].sort_by! { |d| [d['To'] ? d['To'] : 'zzzz', d['From'] ? d['From'] : ''] }
        end
    end
    zones = Hash[zones.to_a.sort_by { |d| d.first } ]
    zones
end

.getTerritoriesObject



50
51
52
53
54
55
56
57
58
# File 'lib/timezone_parser/data/cldr.rb', line 50

def self.getTerritories
    unless @@Territories
        @@Territories = {}
        Cldr::Export::Data::TerritoriesContainment.new.territories.each do |territory, info|
            @@Territories[territory] = info[:contains]
        end
    end
    @@Territories
end

.getTimezonesObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/timezone_parser/data/cldr.rb', line 60

def self.getTimezones
    timezones = { }
    getLocales.each do |locale|
        tz = Cldr::Export::Data::Timezones.new(locale)
        next if tz.timezones.empty? and tz.metazones.empty?

        tz.timezones.each do |timezone, data|
            next if timezone == :'Etc/Unknown' or data[:city].nil?
            city = data[:city].to_s.encode(Encoding::UTF_8).chomp.strip
            timezones[locale] ||= {}
            timezones[locale][city] ||= {}
            self.updateHash(timezones[locale][city], 'Timezones', timezone)
            data[:long].to_a.each do |type, name|
                name = name.to_s.encode(Encoding::UTF_8).chomp.strip
                type = type.to_s.encode(Encoding::UTF_8)
                timezones[locale][name] ||= {}
                if type == 'generic'
                    self.updateHash(timezones[locale][name], 'Types', 'standard')
                    self.updateHash(timezones[locale][name], 'Types', 'daylight')
                else
                    self.updateHash(timezones[locale][name], 'Types', type)
                end
                self.updateHash(timezones[locale][name], 'Timezones', timezone.to_s.encode(Encoding::UTF_8))
            end
        end
        tz.metazones.each do |metazone, data|
            data[:long].to_a.each do |type, name|
                name = name.to_s.encode(Encoding::UTF_8).chomp.strip
                next if name.empty?
                type = type.to_s.encode(Encoding::UTF_8)
                timezones[locale] ||= {}
                timezones[locale][name] ||= {}
                if type == 'generic'
                    self.updateHash(timezones[locale][name], 'Types', 'standard')
                    self.updateHash(timezones[locale][name], 'Types', 'daylight')
                else
                    self.updateHash(timezones[locale][name], 'Types', type)
                end
                self.updateHash(timezones[locale][name], 'Metazones', metazone.to_s.encode(Encoding::UTF_8))
            end
        end
        timezones[locale] = Hash[timezones[locale].to_a.sort_by { |d| d.first } ] if timezones[locale]
    end
    timezones
end

.getVersion(source = DataDir) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/timezone_parser/data/cldr.rb', line 34

def self.getVersion(source = DataDir)
    return @@Version if @@Version
    content = File.read(source + 'dtd' + 'ldml.dtd')
    content.gsub!(/<!--.*?-->/, '')
    data = content.match(/\s+cldrVersion\s+[\#\w\s]+\s+"([\d\.]+)"\s*\>/)
    @@Version = data[1] if data
    @@Version
end

.getWindowsZonesObject



189
190
191
192
193
# File 'lib/timezone_parser/data/cldr.rb', line 189

def self.getWindowsZones
    zones = Cldr::Export::Data::WindowsZones.new
    zones = Hash[zones.to_a.sort_by { |d| d.first } ]
    zones
end

.updateAbbreviations(abbreviations) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/timezone_parser/data/cldr.rb', line 106

def self.updateAbbreviations(abbreviations)
    getLocales.each do |locale|
        tz = Cldr::Export::Data::Timezones.new(locale)
        next if tz.timezones.empty? and tz.metazones.empty?
        tz.timezones.each do |timezone, data|
            data[:short].to_a.each do |type, name|
                next if name == '∅∅∅'
                name = name.chomp.strip
                type = type.to_s.encode(Encoding::UTF_8)
                abbreviations[name] ||= []
                data = {}
                add = true
                abbreviations[name].each_index do |i|
                    next unless abbreviations[name][i]['Offset'].nil?
                    data = abbreviations[name][i]
                    add = false
                    break
                end
                if type == 'generic'
                    self.updateHash(data, 'Types', 'standard')
                    self.updateHash(data, 'Types', 'daylight')
                else
                    self.updateHash(data, 'Types', type)
                end
                self.updateHash(data, 'Timezones', timezone)
                abbreviations[name] << data if add
            end
        end
        tz.metazones.each do |metazone, data|
            data[:short].to_a.each do |type, name|
                next if name == '∅∅∅'
                name = name.chomp.strip
                type = type.to_s.encode(Encoding::UTF_8)
                abbreviations[name] ||= []
                data = {}
                add = true
                abbreviations[name].each_index do |i|
                    next unless abbreviations[name][i]['Offset'].nil?
                    data = abbreviations[name][i]
                    add = false
                    break
                end
                if type == 'generic'
                    self.updateHash(data, 'Types', 'standard')
                    self.updateHash(data, 'Types', 'daylight')
                else
                    self.updateHash(data, 'Types', type)
                end
                self.updateHash(data, 'Metazones', metazone)
                abbreviations[name] << data if add
            end
        end
    end
    abbreviations = Hash[abbreviations.to_a.sort_by { |d| d.first } ]
    abbreviations
end

.updateHash(hash, name, data) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/timezone_parser/data/cldr.rb', line 26

def self.updateHash(hash, name, data)
    hash[name] ||= []
    hash[name] << data.to_s
    hash[name].uniq!
    hash[name].sort!
    hash
end