in runtime_scripts/replay.py [0:0]
def replace_dates(self, content):
"""
Replace dates in the content.
Args:
content (str): The content to modify.
Returns:
str: The modified content.
"""
# Run regex on the content to find dates
matches = re.findall(DATES_REGEX, content)
matches_2 = re.findall(DATES_REGEX_NO_MILLIS, content)
if not matches and not matches_2:
return content
def convert_timestamp_to_string(timestamp):
timestamp = str(timestamp)[:14]
if timestamp.endswith(".0"):
timestamp = timestamp[:-2]
else:
timestamp = timestamp.replace(".", "")
return timestamp
def perform_dates_replacement(matches, format, content):
smallest_date = self.smallest_date
matches = list(set(matches))
for match in matches:
match_date = datetime.strptime(match, format)
match_timestamp = (
calendar.timegm(match_date.timetuple())
+ match_date.microsecond / 1_000_000
)
match_timestamp = convert_timestamp_to_string(match_timestamp)
# Check if the date is already cached
if match in REPLACEMENT_DATES:
new_date = REPLACEMENT_DATES[match]
new_timestamp = REPLACEMENT_TIMESTAMPS[match_timestamp]
else:
# Calculate the difference and the new date
diff = match_date - smallest_date
if diff < timedelta(0):
continue
new_date = (self.current_date + diff).strftime(format)[: len(match)]
new_timestamp_value = (
calendar.timegm((self.current_date + diff).timetuple())
+ (self.current_date + diff).microsecond / 1_000_000
)
new_timestamp = convert_timestamp_to_string(new_timestamp_value)
# Cache the replacements
REPLACEMENT_DATES[match] = new_date
REPLACEMENT_TIMESTAMPS[
convert_timestamp_to_string(match_timestamp)
] = new_timestamp
# Replace in the content
content = content.replace(match, new_date)
content = content.replace(match_timestamp, new_timestamp)
return content
content = perform_dates_replacement(matches, "%Y-%m-%d %H:%M:%S.%f", content)
content = perform_dates_replacement(matches_2, "%Y-%m-%d %H:%M:%S", content)
return content