zdgtl 10 months ago
zdgtl #coding

Time Ago to Date Format Converter

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)
0
299
How to move the post title below content wordpress

How to move the post title below content wordpress

1743468932.jpg
zdgtl
9 months ago
Cara Menghapus baris duplikat atau baris ganda

Cara Menghapus baris duplikat atau baris ganda

1743468932.jpg
zdgtl
10 months ago
Kumpulan Perintah PHP dari Internet

Kumpulan Perintah PHP dari Internet

1743468932.jpg
zdgtl
10 months ago
Domain WHOIS by Python3

Domain WHOIS by Python3

1743468932.jpg
zdgtl
10 months ago
Woodmart Content modify

Woodmart Content modify

1743468932.jpg
zdgtl
9 months ago