Skip to content
C++: Choose ISO weekday numbers explicitly

C++: Choose ISO weekday numbers explicitly

November 12, 2026

Weekday numbers depend on the convention at an interface. C++20 provides both Sunday-zero numbering and ISO numbering, where Monday is 1 and Sunday is 7.

Prepare a scratch directory

Tested on Linux with GCC 14.2.0 in C++20 mode. You need Bash, a C++20 compiler and standard library, a text editor, and the mktemp, rm, and rmdir utilities. Keep the same terminal open. Stop if setup fails, before saving the source:

start_directory="$PWD"
work_directory="$(mktemp -d)" || exit 1
cd -- "$work_directory" || exit 1

Run the example

Save as cpp-chrono-weekday-iso-numbering.cpp:

#include <cassert>
#include <chrono>
#include <iostream>

int main() {
    using namespace std::chrono;
    const year_month_day date{year{2026}, November, day{15}};
    if (!date.ok()) return 1;
    const weekday value{sys_days{date}};
    assert(value == Sunday);
    std::cout << "C numbering: " << value.c_encoding() << '\n';
    std::cout << "ISO numbering: " << value.iso_encoding() << '\n';
    std::cout << "next day ISO: " << (value + days{1}).iso_encoding() << '\n';
    assert((value + days{1}) == Monday);
}

Compile and run:

g++ -std=c++20 -Wall -Wextra -Werror -pedantic cpp-chrono-weekday-iso-numbering.cpp -o cpp-chrono-weekday-iso-numbering &&
./cpp-chrono-weekday-iso-numbering

Observed output:

C numbering: 0
ISO numbering: 7
next day ISO: 1

How it works

The date is checked before conversion to sys_days. Both encodings describe the same Sunday. Adding one day wraps around the weekly cycle, so Monday has ISO value 1. No locale-dependent weekday name formatting is involved.

Limits and choices

This example starts with a civil calendar date, not a timestamp. For an event instant, first determine the date in the intended time zone; the UTC date can differ from the local date. iso_encoding() is a weekday number, not an ISO week-of-year calculation.

The C++ working draft specifies the library operation used here. The example uses the C++20 interface tested above; newer draft additions are outside its scope.

Clean up

rm -- cpp-chrono-weekday-iso-numbering.cpp cpp-chrono-weekday-iso-numbering
cd -- "$start_directory" || exit 1
rmdir -- "$work_directory"
unset start_directory work_directory

If the directory is not empty, inspect the remaining files before removing anything else.

Acknowledgements

This post was created with assistance from AI.

Last updated on