AzLearn

تمرين إعادة البناء

تحويل Timestamp إلى تاريخ

أعد كتابة أداة Python تعرض Unix timestamp كتاريخ مقروء مع خيار UTC.

python ~10 دقيقة مبتدئ
أعد بناء الكود Rebuild

هذا هو الكود. اكتبه بنفسك.

الكود المرجعي
import argparse
from datetime import datetime, timezone


def format_timestamp(timestamp: float, use_utc: bool) -> str:
    zone = timezone.utc if use_utc else None
    moment = datetime.fromtimestamp(timestamp, tz=zone)
    return moment.strftime("%Y-%m-%d %H:%M:%S %Z").strip()


def main() -> None:
    parser = argparse.ArgumentParser(description="Convert a Unix timestamp to a readable date.")
    parser.add_argument("timestamp", type=float)
    parser.add_argument("--utc", action="store_true", help="Show the time in UTC.")
    args = parser.parse_args()

    print(format_timestamp(args.timestamp, args.utc))


if __name__ == "__main__":
    main()
اكتب هنا