88 lines
1.9 KiB
Python
88 lines
1.9 KiB
Python
from datetime import datetime
|
|
from dateutil.relativedelta import relativedelta
|
|
import pandas as pd
|
|
|
|
# ------------------------------------------------------------
|
|
# Configuration
|
|
# ------------------------------------------------------------
|
|
|
|
INPUT_FILE = "CalendarDifferenceCases.csv"
|
|
OUTPUT_FILE = "PythonResults.csv"
|
|
|
|
# ------------------------------------------------------------
|
|
# Helpers
|
|
# ------------------------------------------------------------
|
|
|
|
def parse_datetime(s: str) -> datetime:
|
|
return datetime.fromisoformat(s)
|
|
|
|
|
|
def decompose(a: datetime, b: datetime):
|
|
"""Return normalized calendar difference."""
|
|
|
|
if a > b:
|
|
a, b = b, a
|
|
|
|
rd = relativedelta(b, a)
|
|
|
|
reconstructed = a + rd
|
|
|
|
if reconstructed != b:
|
|
raise RuntimeError(
|
|
f"Python reconstruction failed:\n"
|
|
f"From : {a}\n"
|
|
f"To : {b}\n"
|
|
f"Recon: {reconstructed}\n"
|
|
f"Diff : {rd}"
|
|
)
|
|
|
|
return (
|
|
rd.years,
|
|
rd.months,
|
|
rd.days,
|
|
rd.hours,
|
|
rd.minutes,
|
|
rd.seconds,
|
|
)
|
|
|
|
|
|
# ------------------------------------------------------------
|
|
# Main
|
|
# ------------------------------------------------------------
|
|
|
|
df = pd.read_csv(INPUT_FILE)
|
|
|
|
results = []
|
|
|
|
count = len(df)
|
|
|
|
for i, (_, row) in enumerate(df.iterrows(), start=1):
|
|
|
|
a = parse_datetime(row["from"])
|
|
b = parse_datetime(row["to"])
|
|
|
|
y, m, d, hh, mm, ss = decompose(a, b)
|
|
|
|
results.append({
|
|
"from": row["from"],
|
|
"to": row["to"],
|
|
"years": y,
|
|
"months": m,
|
|
"days": d,
|
|
"hours": hh,
|
|
"minutes": mm,
|
|
"seconds": ss,
|
|
})
|
|
|
|
if i % 100000 == 0:
|
|
print(f"{i:,} / {count:,}")
|
|
|
|
pd.DataFrame(results).to_csv(
|
|
OUTPUT_FILE,
|
|
index=False
|
|
)
|
|
|
|
print()
|
|
print("Finished.")
|
|
print(f"Output written to '{OUTPUT_FILE}'")
|
|
|