summaryrefslogtreecommitdiffstats
path: root/src/VBox/Runtime/common/time/timezoneinfo-gen.py
blob: e710aceb27a110ebd947c89dfbdf3e15f8dd7f65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
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
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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
# -*- coding: utf-8 -*-
# $Id: timezoneinfo-gen.py $

"""
Generates timezone mapping info from public domain tz data and
simple windows tables.
"""
from __future__ import print_function;

__copyright__ = \
"""
Copyright (C) 2017-2019 Oracle Corporation

This file is part of VirtualBox Open Source Edition (OSE), as
available from http://www.virtualbox.org. This file is free software;
you can redistribute it and/or modify it under the terms of the GNU
General Public License (GPL) as published by the Free Software
Foundation, in version 2 as it comes in the "COPYING" file of the
VirtualBox OSE distribution. VirtualBox OSE is distributed in the
hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.

The contents of this file may alternatively be used under the terms
of the Common Development and Distribution License Version 1.0
(CDDL) only, as it comes in the "COPYING.CDDL" file of the
VirtualBox OSE distribution, in which case the provisions of the
CDDL are applicable instead of those of the GPL.

You may elect to license modified versions of this file under the
terms and conditions of either the GPL or the CDDL or both.
"""
__version__ = "$Revision: 127855 $"

import os;
import sys;
import xml.etree.ElementTree as ElementTree;


class TzWinZoneEntry(object):
    def __init__(self):
        self.sWinName      = None;
        self.sWinTerritory = None;
        self.fWinGolden    = False;
        self.idxWin        = 0;

class TzLinkEntry(TzWinZoneEntry):
    def __init__(self, sLinkNm, sTarget):
        TzWinZoneEntry.__init__(self);
        self.sLinkNm = sLinkNm;
        self.sTarget = sTarget;

class TzZoneOffset(object):
    def __init__(self, asFields):
        self.sOffset = asFields[0]; # GMT offset expression
        self.sRules  = asFields[1] if len(asFields) > 1 and asFields[1] not in [ '-', '' ] else None;
        self.sFormat = asFields[2] if len(asFields) > 2 and asFields[2] not in [ '-', '' ] else None;
        self.sUntil  = asFields[3] if len(asFields) > 3 and asFields[3] not in [ '-', '' ] else None;

class TzZoneEntry(TzWinZoneEntry):
    def __init__(self, sName):
        TzWinZoneEntry.__init__(self);
        self.sName         = sName;
        self.sTerritory    = 'ZZ';
        self.aOffsets      = []; # type: list(TzZoneOffset)

class TzZoneRule(object):
    def __init__(self, sName, sFrom, sTo, sType, sIn, sOn, sAt, sSave, sLetter):
        self.sName   = sName;
        self.sFrom   = sFrom   if sFrom   not in [ '-', '' ] else None;
        self.sTo     = sTo     if sFrom   not in [ '-', '' ] else None;
        self.sType   = sType   if sType   not in [ '-', '' ] else None;
        self.sIn     = sIn     if sIn     not in [ '-', '' ] else None;
        self.sAt     = sAt     if sAt     not in [ '-', '' ] else None;
        self.sSave   = sSave   if sSave   not in [ '-', '' ] else None;
        self.sLetter = sLetter if sLetter not in [ '-', '' ] else None;

def info(sMsg):
    """
    Outputs an informational message to stderr.
    """
    print('info: ' + sMsg, file=sys.stderr);

def warning(sMsg):
    """
    Outputs a warning (to stderr).
    """
    print('warning: ' + sMsg, file=sys.stderr);

def error(sMsg):
    """
    Outputs a warning (to stderr).
    """
    print('error: ' + sMsg, file=sys.stderr);

def readTzDataFile(sFile):
    """ Reads the given data file into memory, stripping comments. """
    oInFile = open(sFile, 'r');
    asLines = oInFile.readlines();
    oInFile.close();
    iLine = 0;
    while iLine < len(asLines):
        offHash = asLines[iLine].find('#');
        if offHash >= 0:
            asLines[iLine] = asLines[iLine][:offHash].rstrip();
        else:
            asLines[iLine] = asLines[iLine].rstrip();
        iLine += 1;
    return asLines;

#
# tzdata structures.
#
g_dZones = {};
g_dRules = {};
g_dLinks = {};

def readTzData(sTzDataDir):
    """
    Reads in the bits we want from tz data.  Assumes 2017b edition.
    """

    #
    # Parse the tzdata files.
    #
    for sFile in [ 'africa', 'antarctica', 'asia', 'australasia', 'europe', 'northamerica', 'southamerica',
                   'pacificnew', 'etcetera', 'backward', 'systemv', 'factory', #'backzone'
                   ]:
        sIn = 'none';
        asLines = readTzDataFile(os.path.join(sTzDataDir, sFile));
        iLine = 0;
        while iLine < len(asLines):
            sLine = asLines[iLine];
            sStrippedLine = sLine.strip(); # Fully stripped version.
            if sStrippedLine:
                asFields = sLine.split();
                try:
                    if sLine.startswith('Zone'): # 'Rule' NAME FROM TO TYPE IN ON AT SAVE LETTER/S
                        sIn = 'Zone';
                        oZone = TzZoneEntry(asFields[1]);
                        if oZone.sName in g_dZones: raise Exception('duplicate: %s' % (oZone.sName,));
                        g_dZones[oZone.sName] = oZone;
                        oZone.aOffsets.append(TzZoneOffset(asFields[2:]));
                    elif sLine.startswith('Rule'): # 'Rule' NAME FROM TO TYPE IN ON AT SAVE LETTER/S
                        oRule = TzZoneRule(asFields[1], asFields[2], asFields[3], asFields[4], asFields[5],
                                           asFields[6], asFields[7], asFields[8], asFields[9]);
                        if oRule.sName not in g_dRules:
                            g_dRules[oRule] = [oRule,];
                        else:
                            g_dRules[oRule].append(oRule);
                    elif sLine.startswith('Link'):
                        if len(asFields) != 3: raise Exception("malformed link: len(asFields) = %d" % (len(asFields)));
                        oLink = TzLinkEntry(asFields[2].strip(), asFields[1].strip());
                        if oLink.sLinkNm not in g_dLinks:
                            g_dLinks[oLink.sLinkNm] = oLink;
                        elif g_dLinks[oLink.sLinkNm].sTarget != oLink.sTarget:
                            warning('duplicate link for %s: new target %s, previous %s'
                                    % (oLink.sLinkNm, oLink.sTarget, g_dLinks[oLink.sLinkNm].sTarget,));
                    elif sIn == 'Zone':
                        oZone.aOffsets.append(TzZoneEntry(asFields[3:]));
                    else:
                        raise Exception('what is this?')
                except Exception as oXcpt:
                    error("line %u in %s: '%s'" % (iLine + 1, sFile, type(oXcpt) if not str(oXcpt) else str(oXcpt),));
                    info("'%s'" % (asLines[iLine],));
                    return 1;
            iLine += 1;

    #
    # Process the country <-> zone mapping file.
    #
    asLines = readTzDataFile(os.path.join(sTzDataDir, 'zone.tab'));
    iLine = 0;
    while iLine < len(asLines):
        sLine = asLines[iLine];
        if sLine and sLine[0] != ' ':
            asFields = sLine.split('\t');
            try:
                sTerritory = asFields[0];
                if len(sTerritory) != 2: raise Exception('malformed country: %s' % (sTerritory,));
                sZone = asFields[2];
                oZone = g_dZones.get(sZone);
                if oZone:
                    if oZone.sTerritory and oZone.sTerritory != 'ZZ':
                        raise Exception('zone %s already have country %s associated with it (setting %s)'
                                        % (sZone, oZone.sTerritory, sTerritory));
                    oZone.sTerritory = sTerritory;
                else:
                    oLink = g_dLinks.get(sZone);
                    if oLink:
                        pass; # ignore country<->link associations for now.
                    else: raise Exception('country zone not found: %s' % (sZone,));

            except Exception as oXcpt:
                error("line %u in %s: '%s'" % (iLine + 1, 'zone.tab', type(oXcpt) if not str(oXcpt) else str(oXcpt),));
                info("'%s'" % (asLines[iLine],));
                return 1;
        iLine += 1;
    return 0


def readWindowsToTzMap(sMapXml):
    """
    Reads the 'common/supplemental/windowsZones.xml' file from http://cldr.unicode.org/.
    """
    oXmlDoc = ElementTree.parse(sMapXml);
    oMap = oXmlDoc.getroot().find('windowsZones').find('mapTimezones');
    # <mapZone other="Line Islands Standard Time" territory="001" type="Pacific/Kiritimati"/>
    for oChild in oMap.findall('mapZone'):
        sTerritory  = oChild.attrib['territory'];
        sWinZone    = oChild.attrib['other'];
        asUnixZones = oChild.attrib['type'].split();
        for sZone in asUnixZones:
            oZone = g_dZones.get(sZone);
            if oZone:
                if oZone.sWinName is None or (oZone.sWinTerritory == '001' and oZone.sWinName == sWinZone):
                    oZone.sWinName      = sWinZone;
                    oZone.sWinTerritory = sTerritory;
                    if sTerritory == '001':
                        oZone.fWinGolden = True;
                else:
                    warning('zone "%s" have more than one windows mapping: %s (%s) and now %s (%s)'
                            % (sZone, oZone.sWinName, oZone.sWinTerritory, sWinZone, sTerritory));
            else:
                oLink = g_dLinks.get(sZone);
                if oLink:
                    if oLink.sWinName is None or (oLink.sWinTerritory == '001' and oLink.sWinName == sWinZone):
                        oLink.sWinName      = sWinZone;
                        oLink.sWinTerritory = sTerritory;
                        if sTerritory == '001':
                            oLink.fWinGolden = True;
                    else:
                        warning('zone-link "%s" have more than one windows mapping: %s (%s) and now %s (%s)'
                                % (sZone, oLink.sWinName, oLink.sWinTerritory, sWinZone, sTerritory));
                else:
                    warning('could not find zone "%s" (for mapping win zone "%s" to) - got the right data sets?'
                            % (sZone, sWinZone));
    return 0;


def readWindowsIndexes(sFile):
    """
    Reads the windows time zone index from the table in the given file and sets idxWin.

    Assumes format: index{tab}name{tab}(GMT{offset}){space}{cities}

    For instance: https://support.microsoft.com/en-gb/help/973627/microsoft-time-zone-index-values
    """
    # Read the file.
    oInFile = open(sFile, "r");
    asLines = oInFile.readlines();
    oInFile.close();

    # Check the header.
    if not asLines[0].startswith('Index'):
        error('expected first line of "%s" to start with "Index"' % (sFile,));
        return 1;
    fHexIndex = asLines[0].find('hex') > 0;
    iLine     = 1;
    while iLine < len(asLines):
        # Parse.
        asFields = asLines[iLine].split('\t');
        try:
            idxWin     = int(asFields[0].strip(), 16 if fHexIndex else 10);
            sWinName   = asFields[1].strip();
            sLocations = ' '.join(asFields[2].split());
            if sWinName.find('(GMT') >= 0:          raise Exception("oops #1");
            if not sLocations.startswith('(GMT'):   raise Exception("oops #2");
            sStdOffset = sLocations[sLocations.find('(') + 1 : sLocations.find(')')].strip().replace(' ','');
            sLocations = sLocations[sLocations.find(')') + 1 : ].strip();
        except Exception as oXcpt:
            error("line %u in %s: '%s'" % (iLine + 1, sFile, type(oXcpt) if not str(oXcpt) else str(oXcpt),));
            info("'%s'" % (asLines[iLine],));
            return 1;

        # Some name adjustments.
        sWinName = sWinName.lower();
        if sWinName.startswith('a.u.s.'):
            sWinName = 'aus' + sWinName[6:];
        elif sWinName.startswith('u.s. '):
            sWinName = 'us ' + sWinName[5:];
        elif sWinName.startswith('s.a. '):
            sWinName = 'sa ' + sWinName[5:];
        elif sWinName.startswith('s.e. '):
            sWinName = 'se ' + sWinName[5:];
        elif sWinName.startswith('pacific s.a. '):
            sWinName = 'pacific sa ' + sWinName[13:];

        # Update zone entries with matching windows names.
        cUpdates = 0;
        for sZone in g_dZones:
            oZone = g_dZones[sZone];
            if oZone.sWinName and oZone.sWinName.lower() == sWinName:
                oZone.idxWin = idxWin;
                cUpdates += 1;
                #info('idxWin=%#x - %s / %s' % (idxWin, oZone.sName, oZone.sWinName,));
        if cUpdates == 0:
            warning('No matching zone found for index zone "%s" (%#x, %s)' % (sWinName, idxWin, sLocations));

        # Advance.
        iLine += 1;
    return 0;

def getPadding(sField, cchWidth):
    """ Returns space padding for the given field string. """
    if len(sField) < cchWidth:
        return ' ' * (cchWidth - len(sField));
    return '';

def formatFields(sName, oZone, oWinZone):
    """ Formats the table fields. """

    # RTTIMEZONEINFO:
    #    const char     *pszUnixName;
    #    const char     *pszWindowsName;
    #    uint8_t         cchUnixName;
    #    uint8_t         cchWindowsName;
    #    char            szCountry[3];
    #    char            szWindowsCountry[3];
    #    uint32_t        idxWindows;
    #    uint32_t        uReserved;

    asFields = [ '"%s"' % sName, ];
    if oWinZone.sWinName:
        asFields.append('"%s"' % oWinZone.sWinName);
    else:
        asFields.append('NULL');

    asFields.append('%u' % (len(sName),));
    if oWinZone.sWinName:
        asFields.append('%u' % (len(oWinZone.sWinName),));
    else:
        asFields.append('0');

    asFields.append('"%s"' % (oZone.sTerritory,));
    if oWinZone.sWinTerritory:
        asFields.append('"%s"' % (oWinZone.sWinTerritory,));
    else:
        asFields.append('""');
    asFields.append('%#010x' % (oWinZone.idxWin,));

    asFlags = [];
    if oWinZone.fWinGolden:
        asFlags.append('RTTIMEZONEINFO_F_GOLDEN');
    if asFlags:
        asFields.append(' | '.join(asFlags));
    else:
        asFields.append('0');
    return asFields;

def produceCode(oDst):
    """
    Produces the tables.
    """

    #
    # Produce the info table.
    #
    aasEntries = [];

    # The straight zones.
    for sZone in g_dZones:
        asFields = formatFields(sZone, g_dZones[sZone], g_dZones[sZone]);
        aasEntries.append(asFields);

    # The links.
    for sZone in g_dLinks:
        oLink = g_dLinks[sZone];
        asFields = formatFields(sZone, g_dZones[oLink.sTarget], oLink);
        aasEntries.append(asFields);

    # Figure field lengths.
    acchFields = [ 2, 2, 2, 2, 4, 4, 10, 1 ];
    for asFields in aasEntries:
        assert len(asFields) == len(acchFields);
        for iField, sField in enumerate(asFields):
            if len(sField) > acchFields[iField]:
                acchFields[iField] = len(sField);

    # Sort the data on zone name.
    aasEntries.sort();

    # Do the formatting.
    oDst.write('/**\n'
               ' * Static time zone mapping info.  Sorted by pszUnixName.\n'
               ' */\n'
               'static const RTTIMEZONEINFO g_aTimeZones[] =\n'
               '{\n');
    for iEntry, asFields in enumerate(aasEntries):
        sLine = '    { ';
        for iField, sField in enumerate(asFields):
            sLine += sField;
            sLine += ', ';
            sLine += getPadding(sField, acchFields[iField]);
        sLine += ' }, /* %#05x */\n' % (iEntry,);
        oDst.write(sLine);
    oDst.write('};\n'
               '\n');

    #
    # Now produce a lookup table for windows time zone names, with indexes into
    # the g_aTimeZone table.
    #
    aasLookup = [];
    for iEntry, asFields in enumerate(aasEntries):
        if asFields[1] != 'NULL':
            aasLookup.append([ asFields[1],   # sWinName
                               -1 if asFields[7].find('RTTIMEZONEINFO_F_GOLDEN') >= 0 else 1,
                               asFields[5],   # sWinTerritory
                               iEntry,
                               asFields[0]]); # sZone
    aasLookup.sort();

    oDst.write('/**\n'
               ' * Windows time zone lookup table.  Sorted by name, golden flag and territory.\n'
               ' */\n'
               'static const uint16_t g_aidxWinTimeZones[] = \n'
               '{\n');
    for asFields in aasLookup:
        sLine  = '    %#05x, /* %s' % (asFields[3], asFields[0][1:-1]);
        sLine += getPadding(asFields[0], acchFields[1]);
        sLine += ' / %s%s' % (asFields[2][1:-1], '+' if asFields[1] < 0 else ' ');
        if len(asFields[2]) == 2:
            sLine += '  ';
        sLine += ' ==>  %s */\n' % (asFields[4][1:-1],)
        oDst.write(sLine);

    oDst.write('};\n'
               '\n');

    return 0;


def main(asArgs):
    """
    C-like main function.
    """
    if len(asArgs) != 4:
        error("Takes exacty three arguments: <ms-index-file> <ms-key-file> <tz-data-dir>");
        return 1;
    sTzDataDir     = asArgs[1];
    sWinToTzMap    = asArgs[2];
    sWinIndexTable = asArgs[3];

    #
    # Read in the data first.
    #
    iRc = readTzData(sTzDataDir);
    if iRc == 0:
        iRc = readWindowsToTzMap(sWinToTzMap);
    if iRc == 0:
        iRc = readWindowsIndexes(sWinIndexTable);
    if iRc == 0:
        #
        # Produce the C table.
        #
        iRc = produceCode(sys.stdout);
    return iRc;

if __name__ == '__main__':
    sys.exit(main(sys.argv));