Spaces:
Sleeping
Sleeping
| from datetime import datetime, timedelta | |
| import re | |
| from datetime import datetime | |
| import pytz | |
| vietnam_tz = pytz.timezone("Asia/Ho_Chi_Minh") | |
| from datetime import datetime, timedelta | |
| def chuan_hoa_time(time_str): | |
| days_map = { | |
| "thứ bảy": "Saturday", | |
| "chủ nhật": "Sunday", | |
| "thứ hai": "Monday", | |
| "thứ ba": "Tuesday", | |
| "thứ tư": "Wednesday", | |
| "thứ năm": "Thursday", | |
| "thứ sáu": "Friday", | |
| } | |
| month_map = { | |
| "tháng 1": "January", "tháng 2": "February", "tháng 3": "March", | |
| "tháng 4": "April", "tháng 5": "May", "tháng 6": "June", | |
| "tháng 7": "July", "tháng 8": "August", "tháng 9": "September", | |
| "tháng 10": "October", "tháng 11": "November", "tháng 12": "December" | |
| } | |
| if isinstance(time_str, datetime): | |
| time_str = time_str.strftime('%Y-%m-%d %H:%M:%S') | |
| elif not isinstance(time_str, str): | |
| return "" | |
| time_str = time_str.lower().strip() | |
| now = datetime.now() | |
| if re.match(r"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$", time_str): | |
| try: | |
| # Remove 'Z' and parse the datetime in UTC | |
| time_str = time_str.replace('z', '+00:00') | |
| dt = datetime.strptime(time_str, "%Y-%m-%dT%H:%M:%S%z") | |
| return dt | |
| except ValueError: | |
| raise ValueError(f"Không thể chuẩn hóa chuỗi thời gian: {time_str}") | |
| # Các trường hợp khác (như "phút trước", "giờ trước", ...) | |
| if "phút trước" in time_str: | |
| minutes = int(re.search(r"(\d+)", time_str).group(1)) | |
| time_result = now - timedelta(minutes=minutes) | |
| return time_result.strftime("%d/%m/%Y %H:%M") | |
| elif "giờ trước" in time_str: | |
| hours = int(re.search(r"(\d+)", time_str).group(1)) | |
| time_result = now - timedelta(hours=hours) | |
| return time_result.strftime("%d/%m/%Y %H:%M") | |
| elif "hôm qua" in time_str: | |
| time_result = now - timedelta(days=1) | |
| return time_result.strftime("%d/%m/%Y %H:%M") | |
| elif "ngày trước" in time_str: | |
| days = int(re.search(r"(\d+)", time_str).group(1)) | |
| time_result = now - timedelta(days=days) | |
| return time_result.strftime("%d/%m/%Y %H:%M") | |
| time_str = re.sub(r"\s?\(gmt[+\-]\d{1,2}\)", "", time_str).strip() # Loại bỏ múi giờ | |
| time_str = time_str.replace("|", "") # Loại bỏ ký tự phân cách '|' | |
| time_str = re.sub(r"\s+", " ", time_str).strip() # Loại bỏ khoảng trắng thừa | |
| if re.match(r"^\d{8} \d{2}:\d{2}$", time_str): | |
| time_str = f"{time_str[:4]}/{time_str[4:6]}/{time_str[6:8]} {time_str[9:]}" | |
| if re.match(r"^[a-z]+ \d{2}/\d{2}/\d{4} \d{2}:\d{2}$", time_str): | |
| time_str = time_str.replace(" ", ", ", 1) # Thêm dấu phẩy sau ngày | |
| for vietnamese_month, english_month in month_map.items(): | |
| time_str = time_str.replace(vietnamese_month, english_month) | |
| for vietnamese_day, english_day in days_map.items(): | |
| time_str = time_str.replace(vietnamese_day, english_day) | |
| formats = [ | |
| "%A, %d/%m/%Y %H:%M", # Monday, 2/12/2024 11:33 | |
| "%Y-%m-%d %H:%M:%S", # 2024-11-29 16:01:23 | |
| "%Y-%m-%dT%H:%M:%S", # 2024-11-30T10:20:00 | |
| "%A, %d/%m/%Y - %H:%M", # Monday, 2/12/2024 - 11:33 | |
| "%A, %d/%m/%Y, %H:%M", # Monday, 2/12/2024, 11:33 | |
| "%Y-%m-%d %H:%M", # 2024-12-01 08:00 | |
| "%H:%M, %d/%m/%Y", # 10:45, 01/12/2024 | |
| "%d %B %Y %H:%M", # 2 December 2024 11:33 | |
| "%d/%m/%Y %H:%M", # 02/12/2024 15:22 | |
| "%d %b %Y %H:%M", # 2 Dec 2024 11:33 | |
| "%d/%m/%Y %H:%M:%S", # 02/12/2024 15:22:00 | |
| "%A, %d/%m/%Y %H:%M %z", # Monday, 02/12/2024 10:08 +0700 | |
| "%b %d, %Y %H:%M", # Dec 2, 2024 15:22 | |
| "%d tháng %m, %Y | %H:%M", # 2 tháng 12, 2024 | 15:10 | |
| "%A, %d/%m/%Y - %H:%M", # thứ hai, 02/12/2024 - 15:40 | |
| "%Y,%m,%d %H:%M", # 2024,12,02 15:44 | |
| "%A, %d/%m/%Y , %H:%M" # Monday, 02/12/2024 , 16:01 | |
| ] | |
| for fmt in formats: | |
| try: | |
| dt = datetime.strptime(time_str, fmt) | |
| dt_vietnam = vietnam_tz.localize(dt) | |
| return dt_vietnam | |
| except ValueError: | |
| continue | |
| raise ValueError(f"Không thể chuẩn hóa chuỗi thời gian: {time_str}") |