0Pricing
C++ Academy · Lesson

Calendar and Time Zones

Use C++20 calendar types.

Calendar and Time Zones is a free C++ Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

C++20 Calendar Types

C++20 added rich calendar support to <chrono>. You can now express years, months, and days directly as types rather than juggling integers.

  • year, month, day
  • year_month_day for a full date
#include <iostream>
#include <chrono>

int main() {
    using namespace std::chrono;
    year y{2026};
    std::cout << "Year value: " << static_cast<int>(y) << '\n';
    return 0;
}

Calendar Literals

Calendar literals like 2026y and 15d plus named months such as std::chrono::May make dates read naturally.

#include <iostream>
#include <chrono>

int main() {
    using namespace std::chrono;
    auto y = 2026y;
    auto d = 15d;
    std::cout << static_cast<int>(y) << " / " << static_cast<unsigned>(d) << '\n';
    return 0;
}

Building a Date

Combine the three pieces with the / operator to form a year_month_day. The order reads year, month, day.

#include <iostream>
#include <chrono>

int main() {
    using namespace std::chrono;
    year_month_day ymd = 2026y/May/30;
    std::cout << static_cast<int>(ymd.year()) << "-"
              << static_cast<unsigned>(ymd.month()) << "-"
              << static_cast<unsigned>(ymd.day()) << '\n';
    return 0;
}

Validating Dates

A year_month_day knows whether it is a real calendar date. Use ok() to reject impossible values like February 30.

#include <iostream>
#include <chrono>

int main() {
    using namespace std::chrono;
    year_month_day good = 2026y/February/28;
    year_month_day bad = 2026y/February/30;
    std::cout << std::boolalpha << good.ok() << " " << bad.ok() << '\n';
    return 0;
}

sys_days

Convert a date to sys_days, a count of days since the epoch. This lets you do day arithmetic and compute differences between dates.

#include <iostream>
#include <chrono>

int main() {
    using namespace std::chrono;
    sys_days a = sys_days{2026y/May/30};
    sys_days b = sys_days{2026y/May/20};
    std::cout << "Days apart: " << (a - b).count() << '\n';
    return 0;
}

Day Arithmetic

Adding days{n} to a sys_days moves forward by n days, then you can convert back to a calendar date.

#include <iostream>
#include <chrono>

int main() {
    using namespace std::chrono;
    sys_days start = sys_days{2026y/May/30};
    sys_days later = start + days{10};
    year_month_day ymd = later;
    std::cout << static_cast<unsigned>(ymd.day()) << "/"
              << static_cast<unsigned>(ymd.month()) << '\n';
    return 0;
}

Day of the Week

Convert a sys_days to a weekday to find which day of the week a date falls on.

#include <iostream>
#include <chrono>

int main() {
    using namespace std::chrono;
    weekday wd{sys_days{2026y/May/30}};
    std::cout << "Weekday index (Sun=0): " << wd.c_encoding() << '\n';
    return 0;
}

Last Day of a Month

The special last tag computes the final day of any month, handling leap years automatically.

#include <iostream>
#include <chrono>

int main() {
    using namespace std::chrono;
    year_month_day_last eom = 2024y/February/last;
    year_month_day ymd = eom;
    std::cout << "Feb 2024 ends on day " << static_cast<unsigned>(ymd.day()) << '\n';
    return 0;
}

Leap Year Check

The year type exposes is_leap() so you do not have to remember the divisibility rules yourself.

#include <iostream>
#include <chrono>

int main() {
    using namespace std::chrono;
    std::cout << std::boolalpha;
    std::cout << "2024 leap: " << year{2024}.is_leap() << '\n';
    std::cout << "2026 leap: " << year{2026}.is_leap() << '\n';
    return 0;
}

From a Time Point to a Date

Floor a system_clock time point to days and convert it to a year_month_day to get today's calendar date.

#include <iostream>
#include <chrono>

int main() {
    using namespace std::chrono;
    auto today = floor<days>(system_clock::now());
    year_month_day ymd = sys_days{today};
    std::cout << "Year is in range: " << (static_cast<int>(ymd.year()) > 2000) << '\n';
    return 0;
}

Time Zones

C++20 also adds time zones via std::chrono::zoned_time and the IANA database. Availability depends on your standard library, but the model wraps a time point with a named zone for correct local-time display.

#include <iostream>
#include <chrono>

int main() {
    using namespace std::chrono;
    auto now = system_clock::now();
    auto days_part = floor<days>(now);
    std::cout << "Have a UTC time point: " << (days_part.time_since_epoch().count() > 0) << '\n';
    return 0;
}

Quick Check

Test your understanding of C++20 calendar types.

Recap

You learned about C++20 calendar support:

  • year, month, day, and year_month_day with the / builder
  • ok() validation and is_leap() checks
  • sys_days for day arithmetic and weekday lookup
  • last for end-of-month, plus an introduction to zoned_time

That completes the Time and the Chrono Library course.

Frequently asked questions

Is the “Calendar and Time Zones” lesson free?

Yes — the full text of “Calendar and Time Zones” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.

What will I learn in “Calendar and Time Zones”?

Use C++20 calendar types. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C++ Academy?

No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Calendar and Time Zones” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C++ Academy lesson?

Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Durations and Clocks
  2. Time Points
  3. Measuring Code Performance
  4. Calendar and Time Zones
← Back to C++ Academy