Module: TimezoneParser::Windows

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

Overview

Windows module

Constant Summary collapse

TimeZonePath =

Windows Registry path to Time Zone data

'SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones'
LCIDToLocaleName =

function int LCIDToLocaleName ( In LCID Locale, Out_opt LPWSTR lpName, In int cchName, In DWORD dwFlags );

Fiddle::Function.new( kernel32['LCIDToLocaleName'],
[Fiddle::TYPE_LONG, Fiddle::TYPE_VOIDP, Fiddle::TYPE_INT, Fiddle::TYPE_LONG], Fiddle::TYPE_INT )
LOCALE_NAME_MAX_LENGTH =

Max locale length

85

Class Method Summary collapse

Class Method Details

.collectMUIOffsets(metazoneList, locales) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/timezone_parser/data/windows.rb', line 103

def self.collectMUIOffsets(metazoneList, locales)
    enUS = locales.key('en-US')
    baseMetazone = metazoneList[enUS]
    types = ['display', 'daylight', 'standard']
    type_bases = [0, 0, 0, 3, 3, 3, nil, 7, 7, 7]
    offsets = {}
    baseMetazone.each do |id, name|
        data = {}
        type = id % 10
        type_base = type_bases[type]
        data['Type'] = types[type - type_base]
        data['Name'] = baseMetazone[(id / 10) * 10 + type_base + types.index('standard')]
        offsets[id] = data unless data['Name'].nil?
    end
    Hash[offsets.to_a.sort_by { |o| o.first }]
end

.correctMUIOffsetNames(offsets, metazoneList, locales) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/timezone_parser/data/windows.rb', line 120

def self.correctMUIOffsetNames(offsets, metazoneList, locales)
    enUS = locales.key('en-US')
    baseMetazone = metazoneList[enUS]
    offsets.each do |id, data|
        actualMetazone = nil
        baseMetazone.each do |zoneid, name|
            if id != zoneid and name == data['Name']
                actualMetazone = offsets[zoneid]['Name']
                break
            end
        end
        data['Name'] = actualMetazone if actualMetazone
    end
    offsets
end

.errorsObject



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

def self.errors
    @@Errors
end

.getLocales(lcids) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/timezone_parser/data/windows.rb', line 88

def self.getLocales(lcids)
    locales = {}
    lcids.each do |lcid|
        localeMem = Fiddle::Pointer.malloc(LOCALE_NAME_MAX_LENGTH)
        chars = LCIDToLocaleName.call(lcid, localeMem, LOCALE_NAME_MAX_LENGTH, 0)
        if chars.zero?
            puts "Warning: Failed to translate LCID (#{lcid}) to locale name!"
            next
        end
        locale = localeMem.to_s((chars-1)*2).force_encoding(Encoding::UTF_16LE).encode(Encoding::UTF_8)
        locales[lcid] = locale
    end
    locales
end

.getMUIOffsets(path = TimeZonePath) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/timezone_parser/data/windows.rb', line 61

def self.getMUIOffsets(path = TimeZonePath)
    offsets = {}
    begin
        Win32::Registry::HKEY_LOCAL_MACHINE.open(path, Win32::Registry::KEY_READ).each_key do |key, wtime|
            Win32::Registry::HKEY_LOCAL_MACHINE.open(path + '\\' + key, Win32::Registry::KEY_READ) do |reg|
                muiDisplay = reg.read_s('MUI_Display')
                muiDlt = reg.read_s('MUI_Dlt')
                muiStd = reg.read_s('MUI_Std')

                offsets[self.parseMUI(muiDisplay)] = { 'Type' => 'display', 'Name' => key }
                offsets[self.parseMUI(muiDlt)] = { 'Type' => 'daylight', 'Name' => key }
                offsets[self.parseMUI(muiStd)] = { 'Type' => 'standard', 'Name' => key }
            end
        end
    rescue Win32::Registry::Error => e
        @@Errors << e.message
    end
    puts @@Errors unless @@Errors.empty?
    Hash[offsets.to_a.sort_by { |o| o.first }]
end

.getTimezones(path = TimeZonePath) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/timezone_parser/data/windows.rb', line 33

def self.getTimezones(path = TimeZonePath)
    timezones = {}
    begin
        Win32::Registry::HKEY_LOCAL_MACHINE.open(path, Win32::Registry::KEY_READ).each_key do |key, wtime|
            Win32::Registry::HKEY_LOCAL_MACHINE.open(path + '\\' + key, Win32::Registry::KEY_READ) do |reg|
                timezones[key] ||= {}
                tzi = reg.read('TZI', Win32::Registry::REG_BINARY).last
                # TZI Structure (http://msdn.microsoft.com/en-us/library/windows/desktop/ms725481.aspx)
                # typedef struct _REG_TZI_FORMAT
                # {
                #    LONG Bias;
                #    LONG StandardBias;
                #    LONG DaylightBias;
                #    SYSTEMTIME StandardDate;
                #    SYSTEMTIME DaylightDate;
                # } REG_TZI_FORMAT;
                unpacked = tzi.unpack('lllSSSSSSSSSSSSSSSS')
                timezones[key]['standard'] = (0 - unpacked[0] - unpacked[1]) * 60
                timezones[key]['daylight'] = (0 - unpacked[0] - unpacked[2]) * 60
            end
        end
    rescue Win32::Registry::Error => e
        @@Errors << e.message
    end
    timezones = Hash[timezones.to_a.sort_by { |d| d.first } ]
    timezones
end

.getVersion(path = TimeZonePath) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/timezone_parser/data/windows.rb', line 21

def self.getVersion(path = TimeZonePath)
    return @@Version if @@Version
    begin
        Win32::Registry::HKEY_LOCAL_MACHINE.open(path, Win32::Registry::KEY_READ) do |reg|
            @@Version = reg['TzVersion', Win32::Registry::REG_DWORD].to_s(16)
        end
    rescue Win32::Registry::Error => e
        @@Errors << e.message
    end
    @@Version
end

.parseMetazones(metazoneList, offsets, locales) ⇒ Object



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
162
163
164
165
166
167
168
# File 'lib/timezone_parser/data/windows.rb', line 136

def self.parseMetazones(metazoneList, offsets, locales)
    metazones = {}
    metazoneList.each do |lcid, data|
        locale = locales[lcid]
        if locale.nil?
            puts "Warning: No translation to locale name from LCID (#{lcid}), skipping!"
            next
        end
        metazones[locale] = {}
        offsets.each do |id, info|
            unless data.has_key?(id)
                puts "Warning: Didn't found timezone name for #{id} for #{locale} locale, skipping!"
                next
            end
            name = data[id]
            metazones[locale][name] ||= {}
            metazones[locale][name]['Types'] ||= []
            metazones[locale][name]['Metazones'] ||= []
            types = []
            types << info['Type'] if info.has_key?('Type')
            if info['Type'] == 'display'
                types = ['daylight', 'standard']
            end
            metazones[locale][name]['Types'] += types
            metazones[locale][name]['Metazones'] << info['Name']
            metazones[locale][name]['Types'].uniq!
            metazones[locale][name]['Metazones'].uniq!
        end
        metazones[locale] = Hash[metazones[locale].to_a.sort_by { |d| d.first } ]
    end
    metazones = Hash[metazones.to_a.sort_by { |d| d.first } ]
    metazones
end

.parseMUI(str) ⇒ Object



82
83
84
85
86
# File 'lib/timezone_parser/data/windows.rb', line 82

def self.parseMUI(str)
    parts = str.split(',')
    puts "Warning: Unexpected dll name #{parts.first}" if parts.first != '@tzres.dll'
    parts.last.to_i.abs
end