# Dates, Times, and Timestamps in Python
# datetime, date, timedelta — the essentials
# redhorndev.com

from datetime import datetime, date, timedelta

# ─────────────────────────────────────────────
# Current date and time
# ─────────────────────────────────────────────

now = datetime.now()
print(now)              # 2024-05-06 14:32:45.123456

print(now.year)         # 2024
print(now.month)        # 5
print(now.day)          # 6
print(now.hour)         # 14
print(now.minute)       # 32
print(now.second)       # 45

# ─────────────────────────────────────────────
# strftime() — datetime to string
# ─────────────────────────────────────────────

now = datetime.now()

print(now.strftime("%Y-%m-%d"))             # 2024-05-06
print(now.strftime("%d/%m/%Y"))             # 06/05/2024
print(now.strftime("%Y-%m-%d %H:%M"))       # 2024-05-06 14:32
print(now.strftime("%A, %d %B %Y"))         # Monday, 06 May 2024
print(now.strftime("%H:%M:%S"))             # 14:32:45

# common format codes
# %Y — 4-digit year       %y — 2-digit year
# %m — month (01-12)      %B — full month name
# %d — day (01-31)        %A — full weekday name
# %H — hour 24h (00-23)   %I — hour 12h (01-12)
# %M — minute (00-59)     %S — second (00-59)

# ─────────────────────────────────────────────
# strptime() — string to datetime
# ─────────────────────────────────────────────

date_str = "2024-05-06"
dt = datetime.strptime(date_str, "%Y-%m-%d")
print(dt.year)          # 2024
print(dt.month)         # 5
print(dt.day)           # 6

timestamp = "2024-05-06 14:32"
dt = datetime.strptime(timestamp, "%Y-%m-%d %H:%M")
print(dt.hour)          # 14

# ─────────────────────────────────────────────
# date.today() — date only
# ─────────────────────────────────────────────

today = date.today()
print(today)            # 2024-05-06
print(today.year)       # 2024
print(today.weekday())  # 0=Monday, 1=Tuesday ... 6=Sunday

# ─────────────────────────────────────────────
# timedelta — durations and differences
# ─────────────────────────────────────────────

today     = date.today()
tomorrow  = today + timedelta(days=1)
yesterday = today - timedelta(days=1)
next_week = today + timedelta(weeks=1)

print(tomorrow)         # 2024-05-07
print(yesterday)        # 2024-05-05
print(next_week)        # 2024-05-13

# subtract two dates
start = date(2024, 1, 1)
end   = date(2024, 5, 6)
diff  = end - start
print(diff.days)        # 126

# ─────────────────────────────────────────────
# Practical — timestamp for logs
# ─────────────────────────────────────────────

timestamp = datetime.now().strftime("%Y-%m-%d %H:%M")
print(f"[{timestamp}] Mission log entry.")
# [2024-05-06 14:32] Mission log entry.

# ─────────────────────────────────────────────
# Quick reference
# ─────────────────────────────────────────────

# from datetime import datetime, date, timedelta
#
# datetime.now()              — current date and time
# date.today()                — current date only
# dt.strftime(fmt)            — datetime → string
# datetime.strptime(s, fmt)   — string → datetime
# timedelta(days=n)           — duration of n days
# date1 - date2               — timedelta between two dates
#
# strftime → to string   |   strptime → parse string
# %M = minutes           |   %m = month  (case-sensitive)
