date module update

This commit is contained in:
Hash Borgir 2024-02-17 07:04:35 -07:00
parent e9a853c31a
commit 784409cef6

View File

@ -23,10 +23,39 @@ async def handle_command(room, message, bot, prefix, config):
if match.is_not_from_this_bot() and match.prefix() and match.command("date"): if match.is_not_from_this_bot() and match.prefix() and match.command("date"):
logging.info("Fetching current date and time") logging.info("Fetching current date and time")
current_datetime = datetime.datetime.now() current_datetime = datetime.datetime.now()
# Extract individual date components
day_of_week = current_datetime.strftime("%A") day_of_week = current_datetime.strftime("%A")
date_of_month = current_datetime.strftime("%d") # Day with leading zero
month = current_datetime.strftime("%B") month = current_datetime.strftime("%B")
year = current_datetime.strftime("%Y") year = current_datetime.strftime("%Y")
time = current_datetime.strftime("%I:%M:%S %p") time = current_datetime.strftime("%I:%M:%S %p")
date_message = f"📅 Today is **{day_of_week}** of **{month}** in **{year}**. The time is **⏰ {time}**"
# Format date with ordinal suffix
date_with_ordinal = f"{date_of_month}{get_ordinal_suffix(date_of_month)}"
# Construct the message
date_message = f"Date: **{day_of_week}** the {date_with_ordinal}, **{month} {year}**. \nTime: **⏰ {time}**"
await bot.api.send_markdown_message(room.room_id, date_message) await bot.api.send_markdown_message(room.room_id, date_message)
logging.info("Sent current date and time to the room") logging.info("Sent current date and time to the room")
def get_ordinal_suffix(day_of_month):
"""
Helper function to get the ordinal suffix for a day number.
Args:
day_of_month (str): The day number as a string (e.g., "1", "13")
Returns:
str: The ordinal suffix (e.g., "st", "th", "nd")
"""
if day_of_month.endswith("11") or day_of_month.endswith("12") or day_of_month.endswith("13"):
return "th"
elif day_of_month.endswith("1") or day_of_month.endswith("3"):
return "st"
elif day_of_month.endswith("2"):
return "nd"
else:
return "th"