Spaces:
Sleeping
Sleeping
Update utils.py
Browse files
utils.py
CHANGED
|
@@ -1,42 +1,15 @@
|
|
| 1 |
-
# utils.py
|
| 2 |
from datetime import datetime
|
| 3 |
|
| 4 |
def normalize_date(date_str):
|
| 5 |
-
"""
|
| 6 |
-
Tries to convert ANY date string into Zoho's required 'YYYY-MM-DD'.
|
| 7 |
-
If it fails, it defaults to Today.
|
| 8 |
-
"""
|
| 9 |
if not date_str or str(date_str).lower() in ["null", "none", "unknown"]:
|
| 10 |
return datetime.today().strftime("%Y-%m-%d")
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
"%Y-%m-%d", # 2025-11-21 (Target)
|
| 15 |
-
"%d-%m-%Y", # 21-11-2025
|
| 16 |
-
"%d/%m/%Y", # 21/11/2025
|
| 17 |
-
"%m/%d/%Y", # 11/21/2025
|
| 18 |
-
"%Y/%m/%d", # 2025/11/21
|
| 19 |
-
"%d %b %Y", # 21 Nov 2025
|
| 20 |
-
"%B %d, %Y" # November 21, 2025
|
| 21 |
-
]
|
| 22 |
-
|
| 23 |
-
clean_str = str(date_str).strip()
|
| 24 |
|
| 25 |
for fmt in formats:
|
| 26 |
try:
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
except ValueError:
|
| 30 |
-
continue
|
| 31 |
|
| 32 |
-
|
| 33 |
-
print(f"⚠️ Date Parsing Failed for '{date_str}'. Using Today.")
|
| 34 |
-
return datetime.today().strftime("%Y-%m-%d")
|
| 35 |
-
|
| 36 |
-
def clean_amount(amount_str):
|
| 37 |
-
"""Removes currency symbols and commas."""
|
| 38 |
-
try:
|
| 39 |
-
clean = str(amount_str).replace(",", "").replace("INR", "").replace("$", "").strip()
|
| 40 |
-
return float(clean)
|
| 41 |
-
except:
|
| 42 |
-
return 0.0
|
|
|
|
|
|
|
| 1 |
from datetime import datetime
|
| 2 |
|
| 3 |
def normalize_date(date_str):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
if not date_str or str(date_str).lower() in ["null", "none", "unknown"]:
|
| 5 |
return datetime.today().strftime("%Y-%m-%d")
|
| 6 |
|
| 7 |
+
formats = ["%Y-%m-%d", "%d-%m-%Y", "%d/%m/%Y", "%m/%d/%Y", "%Y/%m/%d", "%d %b %Y", "%B %d, %Y"]
|
| 8 |
+
clean = str(date_str).strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
for fmt in formats:
|
| 11 |
try:
|
| 12 |
+
return datetime.strptime(clean, fmt).strftime("%Y-%m-%d")
|
| 13 |
+
except ValueError: continue
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
return datetime.today().strftime("%Y-%m-%d")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|