std::chrono...rono::make24(3)
π NAME π
okay bestie, welcome to std::chrono...rono::make24(3) from the C++ Standard Libary manual β yes, the header spells it Libary, and we're keeping the lore. π
This is the C++ chrono page for the four time-format besties, and it's all since C++20.
std::chrono::is_am,std::chrono::is_pm,std::chrono::make12,std::chrono::make24 β that's the exact NAME-line squad, no cap. π«Ά
β¨ Synopsis β¨
Defined in header <chrono>.
(1) (since C++20)βconstexpr bool is_am( const std::chrono::hours& h ) noexcept;(2) (since C++20)βconstexpr bool is_pm( const std::chrono::hours& h ) noexcept;(3) (since C++20)βconstexpr std::chrono::hours make12( const std::chrono::hours& h ) noexcept;(4) (since C++20)βconstexpr std::chrono::hours make24( const std::chrono::hours& h, bool is_pm ) noexcept;
These functions aid in translating between a 12-hour format time of day and a 24-hour format time of day. Literally clock math, but make it cute. π°οΈβ¨
- Detects whether the 24-hour format time is a.m. (ante meridiem, before midday).
- Detects whether the 24-hour format time is p.m. (post meridiem, after midday).
- Returns the 12-hour equivalent of a 24-hour format time.
- Returns the 24-hour equivalent of a 12-hour format time
h, whereis_pmdetermines whether the time is p.m.
π Parameters π
hβ 12-hour or 24-hour format time to detect.is_pmβ whether the 12-hour format time is p.m.
πΈ Return value πΈ
0h <= h && h <= 11h12h <= h && h <= 23h- If
his in range[0h, 23h], returns the 12-hour equivalent in range[1h, 12h]. Otherwise, the return value is unspecified. - If
his in range[1h, 12h], returns the 24-hour equivalent in range[0h, 11h]ifis_pmisfalse, or in range[12h, 23h]otherwise. Otherwise, the return value is unspecified.
π¨ out-of-range h gives an unspecified return value in (3) and (4) β that's the manual's words, bestie, so don't roll those dice. π
π¦ Example π¦
// Run this code
#include <chrono>
#include <iomanip>
#include <iostream>
#include <utility>
int main()
{
using namespace std::chrono;
static_assert(
is_am(10h) && is_am(11h) && !is_am(12h) && !is_am(23h) &&
!is_pm(10h) && !is_pm(11h) && is_pm(12h) && is_pm(23h)
);
std::cout << "make12():\n";
for (const hours hh : {0h, 1h, 11h, 12h, 13h, 23h})
{
const hours am{make12(hh)};
std::cout << std::setw(2) << hh.count() << "h == "
<< std::setw(2) << am.count() << (is_am(hh) ? "h a.m.\n" : "h p.m.\n");
}
std::cout << "\nmake24():\n";
using p = std::pair<hours, bool>;
for (const auto& [hh, pm] : {p{1h, 0}, p{12h, 0}, p{1h, 1}, p{12h, 1}})
{
std::cout << std::setw(2) << hh.count()
<< (pm ? "h p.m." : "h a.m.")
<< " == " << std::setw(2)
<< make24(hh, pm).count() << "h\n";
}
}
Output:
make12():
0h == 12h a.m.
1h == 1h a.m.
11h == 11h a.m.
12h == 12h p.m.
13h == 1h p.m.
23h == 11h p.m.
make24():
1h a.m. == 1h
12h a.m. == 0h
1h p.m. == 13h
12h p.m. == 12h
xoxo, your Unix bestie β source/version http://cppreference.com, dated 2024.06.10. π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms