🛠️ToolsShed

Convertitore di fuso orario

Converti l'ora tra diversi fusi orari in tutto il mondo.

Ora corrente (il tuo browser)
UTC

Domande Frequenti

Implementazione del Codice

from datetime import datetime
import pytz  # pip install pytz

def convert_timezone(dt_str: str, from_tz: str, to_tz: str) -> str:
    """Convert a datetime string from one timezone to another."""
    fmt = "%Y-%m-%d %H:%M"
    source_tz = pytz.timezone(from_tz)
    target_tz = pytz.timezone(to_tz)

    # Parse and localize
    naive_dt = datetime.strptime(dt_str, fmt)
    aware_dt = source_tz.localize(naive_dt)

    # Convert
    converted = aware_dt.astimezone(target_tz)
    offset = converted.strftime("%z")
    offset_str = f"UTC{offset[:3]}:{offset[3:]}"
    return f"{converted.strftime(fmt)} ({offset_str})"

def current_time_in(tz_name: str) -> str:
    """Get the current time in a given timezone."""
    tz = pytz.timezone(tz_name)
    now = datetime.now(tz)
    return now.strftime("%Y-%m-%d %H:%M:%S %Z%z")

# Examples
print(convert_timezone("2024-06-15 14:30", "America/New_York", "Asia/Tokyo"))
print(convert_timezone("2024-06-15 14:30", "Europe/London", "Australia/Sydney"))
print(current_time_in("Asia/Seoul"))

Comments & Feedback

Comments are powered by Giscus. Sign in with GitHub to leave a comment.