def _generate_link_item(self, link_name, zone_name): return self.ZONE_INFOS_CPP_LINK_ITEM.format( scope=self.scope, linkFullName=link_name, linkNormalizedName=normalize_name(link_name), zoneFullName=zone_name, zoneNormalizedName=normalize_name(zone_name))
def generate_infos_h(self): info_items = '' for zone_name, eras in sorted(self.zones_map.items()): info_items += self.ZONE_INFOS_H_INFO_ITEM.format( scope=self.scope, zoneNormalizedName=normalize_name(zone_name), zoneFullName=zone_name) removed_info_items = '' for zone_name, reason in sorted(self.removed_zones.items()): removed_info_items += self.ZONE_INFOS_H_REMOVED_INFO_ITEM.format( zoneFullName=zone_name, reason=reason) notable_info_items = '' for zone_name, reason in sorted(self.notable_zones.items()): notable_info_items += self.ZONE_INFOS_H_NOTABLE_INFO_ITEM.format( zoneFullName=zone_name, reason=reason) link_items = '' for link_name, zone_name in sorted(self.links_map.items()): link_items += self.ZONE_INFOS_H_LINK_ITEM.format( scope=self.scope, linkNormalizedName=normalize_name(link_name), linkFullName=link_name, zoneFullName=zone_name) removed_link_items = '' for link_name, reason in sorted(self.removed_links.items()): removed_link_items += self.ZONE_INFOS_H_REMOVED_LINK_ITEM.format( linkFullName=link_name, reason=reason) notable_link_items = '' for link_name, reason in sorted(self.notable_links.items()): notable_link_items += self.ZONE_INFOS_H_NOTABLE_LINK_ITEM.format( linkFullName=link_name, reason=reason) return self.ZONE_INFOS_H_FILE.format( invocation=self.invocation, tz_version=self.tz_version, scope=self.scope, dbNamespace=self.db_namespace, dbHeaderNamespace=self.db_header_namespace, tz_files=', '.join(self.tz_files), numInfos=len(self.zones_map), infoItems=info_items, numLinks=len(self.links_map), linkItems=link_items, numRemovedInfos=len(self.removed_zones), removedInfoItems=removed_info_items, numNotableInfos=len(self.notable_zones), notableInfoItems=notable_info_items, numRemovedLinks=len(self.removed_links), removedLinkItems=removed_link_items, numNotableLinks=len(self.notable_links), notableLinkItems=notable_link_items)
def _get_validation_map_items(self, test_data: TestData) -> str: s = '' for zone_name, test_items in sorted(test_data.items()): s += self.VALIDATION_MAP_ITEM.format( zoneFullName=zone_name, zoneNormalizedName=normalize_name(zone_name)) return s
def _generate_info_item(self, zone_name, eras): era_items = '' string_length = 0 for era in eras: (era_item, length) = self._generate_era_item(zone_name, era) era_items += era_item string_length += length string_length += len(zone_name) + 1 num_eras = len(eras) memory8 = (string_length + num_eras * self.SIZEOF_ZONE_ERA_8 + 1 * self.SIZEOF_ZONE_INFO_8) memory32 = (string_length + num_eras * self.SIZEOF_ZONE_ERA_32 + 1 * self.SIZEOF_ZONE_INFO_32) transition_buf_size = self.buf_sizes[zone_name] info_item = self.ZONE_INFOS_CPP_INFO_ITEM.format( scope=self.scope, zoneFullName=zone_name, zoneNormalizedName=normalize_name(zone_name), zoneNameHash=hash_name(zone_name), transitionBufSize=transition_buf_size, numEras=num_eras, stringLength=string_length, memory8=memory8, memory32=memory32, eraItems=era_items, progmem='ACE_TIME_PROGMEM') return (info_item, string_length)
def generate_policies_h(self): policy_items = '' for name, rules in sorted(self.rules_map.items()): policy_items += self.ZONE_POLICIES_H_POLICY_ITEM.format( policyName=normalize_name(name), scope=self.scope) removed_policy_items = '' for name, reason in sorted(self.removed_policies.items()): removed_policy_items += \ self.ZONE_POLICIES_H_REMOVED_POLICY_ITEM.format( policyName=name, policyReason=reason) notable_policy_items = '' for name, reason in sorted(self.notable_policies.items()): notable_policy_items += \ self.ZONE_POLICIES_H_NOTABLE_POLICY_ITEM.format( policyName=name, policyReason=reason) return self.ZONE_POLICIES_H_FILE.format( invocation=self.invocation, tz_version=self.tz_version, dbNamespace=self.db_namespace, dbHeaderNamespace=self.db_header_namespace, tz_files=', '.join(self.tz_files), numPolicies=len(self.rules_map), policyItems=policy_items, numRemovedPolicies=len(self.removed_policies), removedPolicyItems=removed_policy_items, numNotablePolicies=len(self.notable_policies), notablePolicyItems=notable_policy_items)
def _get_validation_items(self, test_data: TestData) -> str: s = '' for zone_name, test_items in sorted(test_data.items()): test_items_str = self._get_test_items(test_items) s += self.VALIDATION_ITEM.format( zoneNormalizedName=normalize_name(zone_name), testItems=test_items_str) return s
def generate_registry_cpp(self): zone_registry_items = '' for zone_name, eras in sorted(self.zones_map.items()): name = normalize_name(zone_name) zone_registry_items += f' &kZone{name}, // {zone_name}\n' return self.ZONE_REGISTRY_CPP_FILE.format( invocation=self.invocation, tz_version=self.tz_version, scope=self.scope, dbNamespace=self.db_namespace, dbHeaderNamespace=self.db_header_namespace, numZones=len(self.zones_map), zoneRegistryItems=zone_registry_items, progmem='ACE_TIME_PROGMEM')
def _generate_era_item(self, zone_name, era): policy_name = era.rules if policy_name == '-' or policy_name == ':': zone_policy = 'nullptr' delta_seconds = era.rulesDeltaSecondsTruncated else: zone_policy = '&kPolicy%s' % normalize_name(policy_name) delta_seconds = 0 if self.scope == 'extended': offset_code, delta_code = _to_extended_offset_and_delta( era.offsetSecondsTruncated, delta_seconds) else: offset_code = div_to_zero(era.offsetSecondsTruncated, 900) delta_code = div_to_zero(delta_seconds, 900) until_year = era.untilYear if until_year == MAX_UNTIL_YEAR: until_year_tiny = MAX_UNTIL_YEAR_TINY else: until_year_tiny = until_year - EPOCH_YEAR until_month = era.untilMonth if not until_month: until_month = 1 until_day = era.untilDay if not until_day: until_day = 1 until_time_code, until_time_modifier = _to_code_and_modifier( era.untilSecondsTruncated, era.untilTimeSuffix, self.scope) # Replace %s with just a % for C++ format = era.format.replace('%s', '%') string_length = len(format) + 1 era_item = self.ZONE_INFOS_CPP_ERA_ITEM.format( rawLine=normalize_raw(era.rawLine), offsetCode=offset_code, deltaCode=delta_code, zonePolicy=zone_policy, format=format, untilYearTiny=until_year_tiny, untilMonth=until_month, untilDay=until_day, untilTimeCode=until_time_code, untilTimeModifier=until_time_modifier) return (era_item, string_length)
def _generate_policy_item(self, name, rules, indexed_letters): # Generate kZoneRules*[] rule_items = '' for rule in rules: at_time_code = div_to_zero(rule.atSecondsTruncated, 15 * 60) delta_code = div_to_zero(rule.deltaSecondsTruncated, 15 * 60) from_year = rule.fromYear from_year_tiny = to_tiny_year(from_year) to_year = rule.toYear to_year_tiny = to_tiny_year(to_year) if len(rule.letter) == 1: letter = "'%s'" % rule.letter letterComment = '' elif len(rule.letter) > 1: index = indexed_letters.get(rule.letter) if index == None: raise Exception('Could not find index for letter (%s)' % rule.letter) if index >= 32: raise Exception('Number of indexed letters >= 32') letter = index letterComment = ('; "%s"' % rule.letter) else: raise Exception('len(%s) == 0; should not happen' % rule.letter) rule_items += self.ZONE_POLICIES_CPP_RULE_ITEM.format( rawLine=normalize_raw(rule.rawLine), fromYearTiny=from_year_tiny, toYearTiny=to_year_tiny, inMonth=rule.inMonth, onDayOfWeek=rule.onDayOfWeek, onDayOfMonth=rule.onDayOfMonth, atTimeCode=at_time_code, atTimeModifier=rule.atTimeModifier, deltaCode=delta_code, letter=letter, letterComment=letterComment) # Generate kLetters*[] policyName = normalize_name(name) numLetters = len(indexed_letters) if indexed_letters else 0 memoryLetters8 = 0 memoryLetters32 = 0 if numLetters: letterArrayRef = 'kLetters%s' % policyName letterItems = '' for name, index in indexed_letters.items(): letterItems += (' /*%d*/ "%s",\n' % (index, name)) memoryLetters8 += len(name) + 1 + 2 # NUL terminated memoryLetters32 += len(name) + 1 + 4 # NUL terminated letterArray = self.ZONE_POLICIES_LETTER_ARRAY.format( policyName=policyName, letterItems=letterItems, progmem='ACE_TIME_PROGMEM') else: letterArrayRef = 'nullptr' letterArray = '' # Calculate the memory consumed by structs and arrays num_rules = len(rules) memory8 = (1 * self.SIZEOF_ZONE_POLICY_8 + num_rules * self.SIZEOF_ZONE_RULE_8 + memoryLetters8) memory32 = (1 * self.SIZEOF_ZONE_POLICY_32 + num_rules * self.SIZEOF_ZONE_RULE_32 + memoryLetters32) policy_item = self.ZONE_POLICIES_CPP_POLICY_ITEM.format( scope=self.scope, policyName=policyName, numRules=num_rules, memory8=memory8, memory32=memory32, ruleItems=rule_items, numLetters=numLetters, letterArrayRef=letterArrayRef, letterArray=letterArray, progmem='ACE_TIME_PROGMEM') return (policy_item, memory8, memory32)