🛠️ToolsShed

Daylight Saving Time Checker

Check if daylight saving time is currently active for any country.

Häufig gestellte Fragen

Code-Implementierung

from datetime import datetime, timezone, timedelta
import zoneinfo

def check_dst(date_str: str, tz_name: str) -> dict:
    """Check DST status for a date in a timezone."""
    tz = zoneinfo.ZoneInfo(tz_name)
    dt = datetime.strptime(date_str, "%Y-%m-%d").replace(tzinfo=tz)

    is_dst = bool(dt.dst())
    utc_offset = dt.utcoffset()

    return {
        "date": date_str,
        "timezone": tz_name,
        "is_dst": is_dst,
        "utc_offset": str(utc_offset),
        "local_time": dt.strftime("%Y-%m-%d %H:%M %Z")
    }

# Examples
timezones = [
    ("2024-07-15", "America/New_York"),   # US Summer - DST active
    ("2024-01-15", "America/New_York"),   # US Winter - no DST
    ("2024-06-15", "Europe/London"),       # UK Summer - BST active
    ("2024-12-15", "Asia/Tokyo"),          # Japan - no DST ever
]

for date, tz in timezones:
    result = check_dst(date, tz)
    dst_str = "DST ACTIVE" if result['is_dst'] else "Standard Time"
    print(f"{tz}: {result['utc_offset']} ({dst_str})")

Comments & Feedback

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