44 lines
1.3 KiB
Python
44 lines
1.3 KiB
Python
from fastapi import FastAPI
|
|
from datetime import datetime
|
|
import calendar
|
|
from typing import Any
|
|
|
|
app = FastAPI()
|
|
|
|
@app.get(
|
|
"/datetime",
|
|
summary="Get Current Date and Time Information",
|
|
description="""Returns the current date, time, weekday, and calendar week.
|
|
This endpoint provides time-related information in a structured JSON format
|
|
that can be used for various applications including logging, timestamps,
|
|
and scheduling.
|
|
|
|
**Response Fields:**
|
|
- `date` (string): Current date in YYYY-MM-DD format
|
|
- `time` (string): Current time in HH:MM:SS format
|
|
- `weekday` (string): Full weekday name (e.g., "Monday")
|
|
- `calendar_week` (integer): ISO calendar week number
|
|
""",
|
|
tags=["datetime"],
|
|
response_model=dict,
|
|
response_description="Current date and time information"
|
|
)
|
|
async def get_datetime() -> dict[str, Any]:
|
|
"""
|
|
Get current date and time information.
|
|
|
|
Returns:
|
|
dict: A dictionary containing current date, time, weekday, and calendar week
|
|
"""
|
|
now = datetime.now()
|
|
return {
|
|
"date": now.strftime("%Y-%m-%d"),
|
|
"time": now.strftime("%H:%M:%S"),
|
|
"weekday": now.strftime("%A"),
|
|
"calendar_week": now.isocalendar().week
|
|
}
|
|
|
|
if __name__ == "__main__":
|
|
import uvicorn
|
|
uvicorn.run(app, host="0.0.0.0", port=8000)
|