Spaces:
Sleeping
Sleeping
File size: 2,591 Bytes
1c87f53 a9c1303 df0d1ca a9c1303 b7a31e3 a9c1303 df0d1ca a9c1303 df0d1ca 57d122f a9c1303 57d122f a9c1303 df0d1ca fa3f9b5 1c87f53 bad2587 1c87f53 a9c1303 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
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")
} |