Simplify UTC offset ID data by computing the offsets

It's trivial to do - and done when generating our compiled data
tables, so makes no difference to users - but makes the offset list
table simpler. Reformat the list so that the fragment-of-hour offsets
are clearly distinguished from the whole-hour ones.

Change-Id: I6e0ea23dc317542b3256e88492e4073faedef1d7
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
This commit is contained in:
Edward Welbourne 2024-03-11 19:42:14 +01:00
parent 08c877a703
commit b48b4f4d3b
2 changed files with 46 additions and 50 deletions

View File

@ -111,9 +111,21 @@ class ZoneIdWriter (SourceFileEditor):
pair[1], pair[0])) pair[1], pair[0]))
out('};\n\n') out('};\n\n')
def offsetOf(utcName):
"Maps a UTC±HH:mm name to its offset in seconds"
assert utcName.startswith('UTC')
if len(utcName) == 3:
return 0
assert utcName[3] in '+-', utcName
sign = -1 if utcName[3] == '-' else 1
assert len(utcName) == 9 and utcName[6] == ':', utcName
hour, mins = int(utcName[4:6]), int(utcName[-2:])
return sign * (hour * 60 + mins) * 60
offsetMap = {} offsetMap = {}
for pair in utcIdList: for name in utcIdList:
offsetMap[pair[1]] = offsetMap.get(pair[1], ()) + (pair[0],) offset = offsetOf(name)
offsetMap[offset] = offsetMap.get(offset, ()) + (name,)
# Write UTC ID key table # Write UTC ID key table
out('// IANA ID Index, UTC Offset\n') out('// IANA ID Index, UTC Offset\n')
out('static constexpr UtcData utcDataTable[] = {\n') out('static constexpr UtcData utcDataTable[] = {\n')
@ -192,7 +204,7 @@ def main(out, err):
f'UTC+{h:02}:{m:02}' f'UTC+{h:02}:{m:02}'
for h, m in (divmod(o, 60) for o in winOff if o > 0)) for h, m in (divmod(o, 60) for o in winOff if o > 0))
# All such offsets should be represented by entries in utcIdList: # All such offsets should be represented by entries in utcIdList:
newUtc = winUtc.difference(n for n, o in utcIdList) newUtc = winUtc.difference(utcIdList)
if newUtc: if newUtc:
err.write(f'Please add {", ".join(newUtc)} to zonedata.utcIdList\n') err.write(f'Please add {", ".join(newUtc)} to zonedata.utcIdList\n')
return 1 return 1

View File

@ -21,8 +21,7 @@ When adding an entry to windowsIdList, check whether its offset
corresponds to that of some entry in utcIdList; if not, add such an corresponds to that of some entry in utcIdList; if not, add such an
entry. entry.
The utcIdList is again a list of tuples (name, offset), associating The utcIdList is a simple list of various UTC-offset names. Aside from
various UTC-offset names with their offsets in seconds. Aside from
'UTC' itself, shared with windowsIdList, these include minutes in 'UTC' itself, shared with windowsIdList, these include minutes in
their offsets even when they are whole hour offsets. The list contains their offsets even when they are whole hour offsets. The list contains
the UTC-equivalents of all offsets seen in the windowsIdList, plus the the UTC-equivalents of all offsets seen in the windowsIdList, plus the
@ -50,52 +49,37 @@ backwards compatibility.
# Do not remove IDs, as each entry is part of the API/behavior guarantee. # Do not remove IDs, as each entry is part of the API/behavior guarantee.
# IDs for the same offset shall be space-joined; list the preferred ID first. # IDs for the same offset shall be space-joined; list the preferred ID first.
# ( UTC Id, Offset Seconds )
utcIdList = ( utcIdList = (
('UTC-14:00', -50400), 'UTC-14:00',
('UTC-13:00', -46800), 'UTC-13:00',
('UTC-12:00', -43200), 'UTC-12:00',
('UTC-11:00', -39600), 'UTC-11:00',
('UTC-10:00', -36000), 'UTC-10:00', 'UTC-09:30',
('UTC-09:30', -34200), 'UTC-09:00',
('UTC-09:00', -32400), 'UTC-08:00',
('UTC-08:00', -28800), 'UTC-07:00',
('UTC-07:00', -25200), 'UTC-06:00',
('UTC-06:00', -21600), 'UTC-05:00', 'UTC-04:30',
('UTC-05:00', -18000), 'UTC-04:00', 'UTC-03:30',
('UTC-04:30', -16200), 'UTC-03:00',
('UTC-04:00', -14400), 'UTC-02:00',
('UTC-03:30', -12600), 'UTC-01:00',
('UTC-03:00', -10800), # UTC Goes first (among zero-offset) to be default:
('UTC-02:00', -7200), 'UTC', 'UTC+00:00', 'UTC-00:00',
('UTC-01:00', -3600), 'UTC+01:00',
('UTC', 0), # Goes first (among zero-offset) to be default 'UTC+02:00',
('UTC+00:00', 0), 'UTC+03:00', 'UTC+03:30',
('UTC-00:00', 0), # Should recognize, but avoid using (see Note above). 'UTC+04:00', 'UTC+04:30',
('UTC+01:00', 3600), 'UTC+05:00', 'UTC+05:30', 'UTC+05:45',
('UTC+02:00', 7200), 'UTC+06:00', 'UTC+06:30',
('UTC+03:00', 10800), 'UTC+07:00',
('UTC+03:30', 12600), 'UTC+08:00', 'UTC+08:30', 'UTC+08:45',
('UTC+04:00', 14400), 'UTC+09:00', 'UTC+09:30',
('UTC+04:30', 16200), 'UTC+10:00', 'UTC+10:30',
('UTC+05:00', 18000), 'UTC+11:00',
('UTC+05:30', 19800), 'UTC+12:00', 'UTC+12:45',
('UTC+05:45', 20700), 'UTC+13:00',
('UTC+06:00', 21600), 'UTC+14:00',
('UTC+06:30', 23400),
('UTC+07:00', 25200),
('UTC+08:00', 28800),
('UTC+08:30', 30600),
('UTC+08:45', 31500),
('UTC+09:00', 32400),
('UTC+09:30', 34200),
('UTC+10:00', 36000),
('UTC+10:30', 37800),
('UTC+11:00', 39600),
('UTC+12:00', 43200),
('UTC+12:45', 45900),
('UTC+13:00', 46800),
('UTC+14:00', 50400),
) )
# ( Windows Id, Offset Seconds ) # ( Windows Id, Offset Seconds )