Spaces:
Sleeping
Sleeping
| from datetime import datetime | |
| from bisect import bisect_left | |
| def find_surrounding_times(time_list: list, time: str) -> list: | |
| """ | |
| Find the surrounding times in a sorted list of times. | |
| :param time_list: List of time strings in "HH:mm" format. | |
| :param time: Time string to find the surrounding times for. | |
| :return: List of surrounding times in "HH:mm" format. | |
| """ | |
| times = [item["time"] for item in time_list] | |
| index = bisect_left(times, time) | |
| start = max(0, index - 2) | |
| end = min(len(time_list), index + 2) | |
| return time_list[start:end] | |
| def extra_time_dialogflow(time: dict) -> str | list: | |
| """ | |
| Format time dictionary to string. | |
| Nếu time có ambiguous (past, future) => trả về list [past_time_str, future_time_str] | |
| Nếu time rõ ràng => trả về string "HH:mm" | |
| """ | |
| def is_time_ambiguous(time_obj: dict) -> bool: | |
| return any(time_obj.get(key) for key in ['past', 'future', 'partial']) | |
| if time is None: | |
| return None | |
| if is_time_ambiguous(time): | |
| past_time = time.get("past", {}) | |
| future_time = time.get("future", {}) | |
| past_hours = int(past_time.get("hours", 0)) | |
| past_minutes = int(past_time.get("minutes", 0)) | |
| past_time_str = f"{past_hours:02d}:{past_minutes:02d}" | |
| future_hours = int(future_time.get("hours", 0)) | |
| future_minutes = int(future_time.get("minutes", 0)) | |
| future_time_str = f"{future_hours:02d}:{future_minutes:02d}" | |
| return sorted([past_time_str, future_time_str]) | |
| else: | |
| hours = int(time["hours"]) | |
| minutes = int(time["minutes"]) | |
| return f"{hours:02d}:{minutes:02d}" | |
| def get_weekday_name(date: datetime) -> tuple: | |
| """ | |
| Get the name of the weekday from its number. | |
| :param weekday: Weekday number (0-6). | |
| :return: Name of the weekday. | |
| """ | |
| vietnam_weekdays = { | |
| 0: "Thứ hai", | |
| 1: "Thứ ba", | |
| 2: "Thứ tư", | |
| 3: "Thứ năm", | |
| 4: "Thứ sáu", | |
| 5: "Thứ bảy", | |
| 6: "Chủ nhật" | |
| } | |
| weekday_ids = date.weekday() | |
| weekday = vietnam_weekdays.get(weekday_ids) if vietnam_weekdays.get(weekday_ids) else "" | |
| return date.strftime("%d-%m-%Y"), weekday | |
| def get_time_now_json() -> dict: | |
| """ | |
| Get the current time in JSON format. | |
| :return: Dictionary with keys 'hours' and 'minutes', 'second'. | |
| """ | |
| now = datetime.now() | |
| return { | |
| "hours": now.strftime("%H"), | |
| "minutes": now.strftime("%M"), | |
| "seconds": now.strftime("%S") | |
| } |