CKTIS / C++
std::chrono::weekday — day of the week and c_encoding()
std::chrono::weekday represents a day of the week. Its c_encoding() method returns unsigned: for valid values, 0 means Sunday and 6 means Saturday. Check ok() before interpreting input values.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
std::chrono::weekday{unsigned_value}
constexpr unsigned std::chrono::weekday::c_encoding() const noexcept;std::chrono::weekday represents a day of the week, not a complete date. The method std::chrono::weekday::c_encoding() has the signature constexpr unsigned c_encoding() const noexcept; and returns the stored value: for a valid object, Sunday is 0, Monday is 1, and Saturday is 6.
The constructor taking unsigned converts 7 to 0, but does not apply general modulo 7. Thus weekday{8} is invalid: ok() returns false, and c_encoding() returns 8. For arguments outside [0, 255], the stored value is unspecified. Check ok() before treating the result as a valid weekday.
Do not confuse this encoding with iso_encoding(): for valid weekdays, ISO encoding uses 1 for Monday and 7 for Sunday. The example demonstrates both Sunday encodings and an invalid object, without reading a clock or using time zones.
Examples
#include <chrono>
#include <iostream>
int main() {
constexpr std::chrono::weekday sunday{7};
constexpr std::chrono::weekday monday{1};
constexpr std::chrono::weekday invalid{8};
static_assert(sunday.ok() && sunday.c_encoding() == 0);
static_assert(monday.ok() && monday.c_encoding() == 1);
static_assert(!invalid.ok() && invalid.c_encoding() == 8);
std::cout << sunday.c_encoding() << ' '
<< sunday.iso_encoding() << '\n';
std::cout << monday.c_encoding() << '\n';
std::cout << std::boolalpha << invalid.ok() << ' '
<< invalid.c_encoding() << '\n';
}
Result
0 7 1 false 8
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.