PYTHON Reference Guide
Revision Time: 3 mins

Datetime Reference

Timestamp parsing, formatting, and operations.

datetime.now()
Return: datetime

Get the current local date and time.

Used In: Auditing transaction times, logging offsets.

Syntax signature:datetime.now()
Code snippet:
python
from datetime import datetime
print(type(datetime.now()))
Expected Output:<class 'datetime.datetime'>

Time Complexity: O(1)

Related Methods: datetime.now(timezone.utc)

Comparison:

datetime.now() vs datetime.now(timezone.utc): now() creates a naive datetime; now(timezone.utc) creates an aware datetime using universal standard time.

Remember: Defaults to local timezone if no tz parameter is passed.
datetime.now(timezone.utc)
Return: datetime

Get the current timezone-aware UTC date and time.

Used In: Production audit stamps.

Syntax signature:from datetime import datetime, timezone datetime.now(timezone.utc)
Code snippet:
python
from datetime import datetime, timezone
print(datetime.now(timezone.utc).tzinfo)
Expected Output:UTC

Time Complexity: O(1)

Related Methods: datetime.now()

Remember: Always prefer UTC-aware datetimes in production data pipelines to avoid daylight-saving anomalies.
date.today()
Return: date

Get the current local date (excluding hours, minutes, seconds).

Used In: Partitioning folders by date, daily reporting.

Syntax signature:from datetime import date date.today()
Code snippet:
python
from datetime import date
print(type(date.today()))
Expected Output:<class 'datetime.date'>

Time Complexity: O(1)

Related Methods: datetime.now()

Remember: Returns a date object containing year, month, and day fields.
timedelta
Return: timedelta

Represent duration offsets for calendar dates calculations.

Used In: Calculating file expiration thresholds, checking query history ranges.

Syntax signature:timedelta(days=0, seconds=0, weeks=0)
Code snippet:
python
from datetime import datetime, timedelta
today = datetime(2026, 7, 12)
yesterday = today - timedelta(days=1)
print(yesterday.day)
Expected Output:11

Time Complexity: O(1)

Remember: Enables easy datetime offset additions/subtractions.
strftime()
Return: str

Format a datetime object into a custom string representation.

Used In: Formatting timestamps for output files names, log headers.

Syntax signature:dt.strftime(format_code)
Code snippet:
python
from datetime import datetime
dt = datetime(2026, 7, 12)
print(dt.strftime('%d/%m/%Y'))
Expected Output:'12/07/2026'

Time Complexity: O(1)

Common Mistakes: Using system-dependent codes (like %-#d on Windows or %-d on Linux) which can break across operating systems.

Related Methods: strptime()

Comparison:

strftime() vs isoformat(): strftime() allows custom formats; isoformat() prints standard YYYY-MM-DDTHH:MM:SS format.

Remember: Common format codes: %Y (4-digit year), %m (month), %d (day).
strptime()
Return: datetime

Parse a date text string into a datetime object using matching codes.

Used In: Timestamp parsing in ETL pipelines, validating dates.

Syntax signature:datetime.strptime(date_string, format)
Code snippet:
python
from datetime import datetime
dt = datetime.strptime('2026-07-12', '%Y-%m-%d')
print(dt.year)
Expected Output:2026

Time Complexity: O(1)

Common Mistakes: Confusing %m (month index) with %M (minute index).

Related Methods: strftime()

Remember: Raises ValueError if the format string does not match the input.
Unix Timestamp
Return: float

Convert a datetime object into raw float epoch seconds.

Used In: Tracking timestamp offsets in databases.

Syntax signature:dt.timestamp()
Code snippet:
python
from datetime import datetime
dt = datetime(2026, 7, 12)
print(type(dt.timestamp()))
Expected Output:<class 'float'>

Time Complexity: O(1)

Related Methods: datetime.fromtimestamp()

Remember: Epoch seconds are highly convenient for integer-based database sorting.
Timezone Conversion
Return: datetime

Translate timezone-aware datetime values into target timezone regions.

Used In: Localizing raw server logs timestamps.

Syntax signature:dt.astimezone(timezone_obj)
Code snippet:
python
from datetime import datetime, timezone, timedelta
utc_time = datetime.now(timezone.utc)
ist_time = utc_time.astimezone(timezone(timedelta(hours=5, minutes=30)))
print(ist_time.tzinfo is not None)
Expected Output:True

Time Complexity: O(1)

Related Methods: datetime.astimezone()

Remember: Use zoneinfo module (standard library in 3.9+) to target regional timezone keys (like 'America/New_York').