CKTIS / C++
Dates and time with std::chrono
std::chrono separates durations, time points, and calendar dates. Learn the %F and %R formats and the distinction between a fixed UTC offset and a named time zone.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
std::chrono::sys_days{std::chrono::year{2024}/2/29} + std::chrono::hours{9} + std::chrono::minutes{5}; std::format("{:%F %R}", tp)std::chrono separates a length of time (duration), a point in time (time_point), and calendar fields (year_month_day). sys_days represents a point on the system_clock timeline with day precision; adding hours and minutes produces a finer-grained point. year_month_day::ok() checks date validity, including leap-day validity.
In std::format("{:%F %R}", tp), %F is equivalent to %Y-%m-%d, while %R is equivalent to %H:%M: a 24-hour clock hour and minutes. %R omits seconds without changing tp. This format prints neither an offset nor a zone name, so the text alone does not uniquely identify an instant without an agreed convention.
Fixed UTC offset versus named time zone: +02:00 specifies a constant difference from UTC, not regional rules. A named zone such as America/New_York uses a rules database and can have different offsets on different dates. zoned_time associates a zone with a time point. When converting local time, offset changes can create nonexistent or ambiguous clock times; these cases require handling.
The example uses a fixed date and reads neither the clock nor the time zone database. tp is system time, not local time. This entry targets C++23; eel.is is an evolving working draft, not a frozen C++23 publication. The repository compiler flag does not establish standard-library or production runtime support.
Examples
#include <chrono>
#include <format>
#include <iostream>
int main() {
using namespace std::chrono;
constexpr year_month_day date = year{2024}/2/29;
static_assert(date.ok());
constexpr auto tp = sys_days{date} + hours{9} + minutes{5};
std::cout << std::format("{:%F %R}\n", tp);
}
Result
2024-02-29 09:05
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.