Convert Time Ago Format to Date Format
import sys from datetime import datetime, timedelta def parse_time_ago(time_ago_str): number, unit, _ = time_ago_str.split() number = int(number) if "hour" in unit: return timedelta(hours=number) elif "day" in unit: return timedelta(days=number) elif "week" in unit: return timedelta(weeks=number) elif "month" in unit: # Approximate month as 30.44 days return timedelta(days=number * 30.44) elif "year" in unit: # Approximate year as 365.25 days return timedelta(days=number * 365.25) else: raise ValueError(f"Unknown time unit: {unit}") def main(input_file, output_file): now = datetime.now() with open(input_file, 'r') as f: time_ago_list = f.readlines() result = [] for time_ago in time_ago_list: time_ago = time_ago.strip() try: delta = parse_time_ago(time_ago) past_date = now - delta result.append(f"{time_ago}: {past_date.strftime('%Y-%m-%d %H:%M:%S')}\n") except ValueError as e: result.append(f"{time_ago}: Error - {str(e)}\n") with open(output_file, 'w') as f: f.writelines(result) if __name__ == "__main__": if len(sys.argv) != 3: print("Usage: python datescript.py ago.txt date.txt") else: input_file = sys.argv[1] output_file = sys.argv[2] main(input_file, output_file)