25 lines
689 B
Python
25 lines
689 B
Python
from typing import Optional
|
|
from fastapi import APIRouter, Depends, HTTPException
|
|
from pydantic import BaseModel
|
|
from datetime import datetime, timezone
|
|
|
|
router = APIRouter()
|
|
|
|
class TimezoneRequest(BaseModel):
|
|
timezone: str
|
|
|
|
class TimezoneResponse(BaseModel):
|
|
current_time: str
|
|
|
|
@router.post("/timezone", response_model=TimezoneResponse)
|
|
async def get_current_time(request: TimezoneRequest):
|
|
"""
|
|
Get the current time for a given timezone
|
|
"""
|
|
try:
|
|
tz = timezone(request.timezone)
|
|
except ValueError as e:
|
|
raise HTTPException(status_code=400, detail=str(e))
|
|
|
|
current_time = datetime.now(tz).isoformat()
|
|
return {"current_time": current_time} |