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



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

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

.getMetazonesObject



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/timezone_parser/data/cldr.rb', line 143

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

.getTimezonesObject



40
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/timezone_parser/data/cldr.rb', line 40

def self.getTimezones
    timezones = { }
    Cldr::Export::Data.locales.sort.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



31
32
33
34
35
36
37
38
# File 'lib/timezone_parser/data/cldr.rb', line 31

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].to_i if data
    @@Version
end

.getWindowsZonesObject



169
170
171
172
173
# File 'lib/timezone_parser/data/cldr.rb', line 169

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

.updateAbbreviations(abbreviations) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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
# File 'lib/timezone_parser/data/cldr.rb', line 86

def self.updateAbbreviations(abbreviations)
    Cldr::Export::Data.locales.sort.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



23
24
25
26
27
28
29
# File 'lib/timezone_parser/data/cldr.rb', line 23

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