cheatah
Module

datetime

Practical date/time helpers over epoch seconds. Times are epoch seconds (double, from time/timestamp); formatting and component extraction use the C library calendar (local time, except utcnow).

import datetime

stamp = datetime.timestamp()
print(datetime.now())                    # "YYYY-MM-DD HH:MM:SS"
print(datetime.format(stamp, "%Y-%m-%d"))
print(datetime.year(stamp), datetime.weekday(stamp))

Functions

Current time / formatted strings:

  • timestamp() — current time as epoch seconds.

  • now() — current local time, "YYYY-MM-DD HH:MM:SS".

  • utcnow() — current UTC time, "YYYY-MM-DDTHH:MM:SSZ".

  • today() — current local date, "YYYY-MM-DD".

  • format(epoch, fmt) — strftime-style formatting of an epoch (local time).

Local-time components of an epoch:

  • year(epoch) / month(epoch) / day(epoch) — calendar date parts.

  • hour(epoch) / minute(epoch) / second(epoch) — time-of-day parts.

  • weekday(epoch) — Monday=0 .. Sunday=6 (Python convention).

Per-function docs (parameters, runtime complexity, heap behavior) are in datetime.hpp. Tested in ../tests/datetime_test.cpp; ASan + Valgrind clean via the QA gate (security/run-valgrind.sh).

Functions

fn double timestamp() source#

Current time.

Reads the wall clock and returns fractional seconds since the Unix epoch (1970-01-01 UTC); the value tracks real time and can jump if the system clock is adjusted (NTP, manual changes).

Returns

seconds since the Unix epoch (system_clock).

Complexity

O(1) time.

Allocation

none.

Performance20.05 ns/call in cheatah · 331 ns/call in CPython 3.12.3 · ≈16.4× faster
fn std::string now() source#

Current local time as "YYYY-MM-DD HH:MM:SS".

Formats the current epoch in the host's local timezone (subject to DST), so the result differs from utcnow by the local UTC offset.

Returns

the formatted string.

Complexity

O(1) time.

Allocation

allocates the result string.

Compile-run testDatetimeCompileRun.Now
Performance116 ns/call in cheatah · 688 ns/call in CPython 3.12.3 · ≈6.1× faster
fn std::string utcnow() source#

Current UTC time as "YYYY-MM-DDTHH:MM:SSZ".

Formats the current epoch in UTC (never local time) using ISO 8601 with a T separator and trailing Z, independent of the host timezone.

Returns

the formatted string.

Complexity

O(1) time.

Allocation

allocates the result string.

Performance92.90 ns/call in cheatah · 914 ns/call in CPython 3.12.3 · ≈9.8× faster
fn std::string today() source#

Current local date as "YYYY-MM-DD".

Formats just the calendar date of the current epoch in the host's local timezone; near midnight this may differ by a day from the UTC date.

Returns

the formatted string.

Complexity

O(1) time.

Allocation

allocates the result string.

Compile-run testDatetimeCompileRun.Today
Performance81.45 ns/call in cheatah · 725 ns/call in CPython 3.12.3 · ≈9.2× faster
fn std::string format(double epoch, std::string_view fmt) source#

strftime-style formatting of an epoch in local time (e.g.

fmt = "Y-m-d").

Converts epoch to local time and expands C strftime format codes (Y, m, d, H, etc.); the output is truncated to a 128-byte internal buffer, so very long expansions are cut short.

Parameters
epoch

epoch seconds.

fmt

a strftime format string.

Returns

the formatted string.

Complexity

O(fmt.size()) — the expansion is capped by the 128-byte internal buffer.

Allocation

allocates a temporary std::string for fmt plus the result.

Performance118 ns/call in cheatah · 1500 ns/call in CPython 3.12.3 · ≈12.6× faster
fn int year(double epoch) source#

Local-time year.

Converts epoch to the host's local timezone and returns the calendar year; the fractional part of epoch is truncated toward the epoch.

Parameters
epoch

epoch seconds.

Returns

the 4-digit year.

Complexity

O(1) time.

Allocation

none.

Compile-run testDatetimeCompileRun.Year
Performance34.52 ns/call in cheatah · 269 ns/call in CPython 3.12.3 · ≈7.9× faster
fn int month(double epoch) source#

Local-time month.

Returns the calendar month of epoch in the host's local timezone, already shifted to a 1-based value (January is 1, not the C tm_mon 0).

Parameters
epoch

epoch seconds.

Returns

the month, 1..12.

Complexity

O(1) time.

Allocation

none.

Compile-run testDatetimeCompileRun.Month
Performance34.46 ns/call in cheatah · 266 ns/call in CPython 3.12.3 · ≈7.7× faster
fn int day(double epoch) source#

Local-time day of month.

Returns the day-of-month of epoch in the host's local timezone; this is the calendar day, not the day-of-year or weekday.

Parameters
epoch

epoch seconds.

Returns

the day, 1..31.

Complexity

O(1) time.

Allocation

none.

Compile-run testDatetimeCompileRun.Day
Performance35.97 ns/call in cheatah · 264 ns/call in CPython 3.12.3 · ≈7.3× faster
fn int hour(double epoch) source#

Local-time hour.

Returns the hour of epoch in the host's local timezone on a 24-hour clock, so DST transitions can make hours repeat or be skipped.

Parameters
epoch

epoch seconds.

Returns

the hour, 0..23.

Complexity

O(1) time.

Allocation

none.

Compile-run testDatetimeCompileRun.Hour
Performance34.03 ns/call in cheatah · 257 ns/call in CPython 3.12.3 · ≈7.6× faster
fn int minute(double epoch) source#

Local-time minute.

Returns the minute-within-the-hour of epoch in the host's local timezone.

Parameters
epoch

epoch seconds.

Returns

the minute, 0..59.

Complexity

O(1) time.

Allocation

none.

Performance35.14 ns/call in cheatah · 257 ns/call in CPython 3.12.3 · ≈7.5× faster
fn int second(double epoch) source#

Local-time second.

Returns the whole second-within-the-minute of epoch (the fractional part of epoch is discarded); the value can reach 60 to represent a leap second.

Parameters
epoch

epoch seconds.

Returns

the second, 0..60 (leap).

Complexity

O(1) time.

Allocation

none.

Performance34.73 ns/call in cheatah · 255 ns/call in CPython 3.12.3 · ≈7.5× faster
fn int weekday(double epoch) source#

Local-time weekday.

Returns the day of the week for epoch in the host's local timezone, remapped from the C tm_wday (Sunday=0) to Python's Monday=0 convention.

Parameters
epoch

epoch seconds.

Returns

Monday=0 .. Sunday=6 (Python convention).

Complexity

O(1) time.

Allocation

none.

Performance34.62 ns/call in cheatah · 266 ns/call in CPython 3.12.3 · ≈7.6× faster