def test_div_to_zero(self): self.assertEqual(1, div_to_zero(3, 3)) self.assertEqual(0, div_to_zero(2, 3)) self.assertEqual(0, div_to_zero(1, 3)) self.assertEqual(0, div_to_zero(0, 3)) self.assertEqual(0, div_to_zero(-1, 3)) self.assertEqual(0, div_to_zero(-2, 3)) self.assertEqual(-1, div_to_zero(-3, 3)) self.assertEqual(-1, div_to_zero(-4, 3)) self.assertEqual(-1, div_to_zero(-5, 3)) self.assertEqual(-2, div_to_zero(-6, 3))
def _get_test_item(self, test_item): return self.TEST_ITEM.format(epochSeconds=test_item.epoch, total=div_to_zero(test_item.total_offset, 60), dst=div_to_zero(test_item.dst_offset, 60), y=test_item.y, M=test_item.M, d=test_item.d, h=test_item.h, m=test_item.m, s=test_item.s, type=test_item.type)
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_validation_data_cpp_test_items(self, zone_name, test_items): """Generate the {testItems} value. """ s = '' for test_item in test_items: total_offset_minutes = div_to_zero(test_item.total_offset, 60) delta_offset_minutes = div_to_zero(test_item.dst_offset, 60) s += self.TEST_ITEM.format(epochSeconds=test_item.epoch, totalOffsetMinutes=total_offset_minutes, deltaOffsetMinutes=delta_offset_minutes, year=test_item.y, month=test_item.M, day=test_item.d, hour=test_item.h, minute=test_item.m, second=test_item.s, type=test_item.type) return s
def _to_code_and_modifier(seconds, suffix, scope): """Return the packed (code, modifier) uint8_t integers that hold the AT or UNTIL timeCode, timeMinute and the suffix. """ timeCode = div_to_zero(seconds, 15 * 60) timeMinute = seconds % 900 // 60 modifier = _to_modifier(suffix, scope) if timeMinute > 0: modifier += f' + {timeMinute}' return timeCode, modifier
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)