C++ date parsing and formatting | chrono, std::format, and time zones
이 글의 핵심
Practical patterns for ISO8601-style strings, chrono parse, std::format with zoned_time, precision (ms vs s), and locale-aware output.
Date parsing and formatting (C++20)
String conversion for calendar types.
#include <chrono>
#include <format>
using namespace std::chrono;
auto str = std::format("{:%Y-%m-%d}", 2026y/March/11);
std::istringstream iss{"2026-03-11"};
year_month_day ymd;
iss >> parse("%Y-%m-%d", ymd);
Formatting dates
using namespace std::chrono;
year_month_day ymd = 2026y / March / 11;
auto str1 = std::format("{:%Y-%m-%d}", ymd);
auto str2 = std::format("{:%Y년 %m월 %d일}", ymd);
auto str3 = std::format("{:%F}", ymd);
Practical examples
Example 1: Several styles (with zoned time)
using namespace std::chrono;
auto now = system_clock::now();
zoned_time seoul{"Asia/Seoul", now};
std::cout << std::format("{:%FT%T%z}", seoul) << std::endl;
std::cout << std::format("{:%Y년 %m월 %d일 %H시 %M분}", seoul) << std::endl;
std::cout << std::format("{:%A, %B %d, %Y}", seoul) << std::endl;
Example 2: Parsing
using namespace std::chrono;
std::string dateStr = "2026-03-11";
std::istringstream iss{dateStr};
year_month_day ymd;
iss >> parse("%Y-%m-%d", ymd);
if (ymd.ok()) {
std::cout << "Parse OK: " << ymd << std::endl;
} else {
std::cout << "Parse failed" << std::endl;
}
Example 3: Time-of-day formatting
using namespace std::chrono;
auto now = system_clock::now();
auto dp = floor<days>(now);
auto time = now - dp;
hh_mm_ss hms{time};
std::cout << std::format("{:%H:%M:%S}", hms) << std::endl;
Example 4: Log timestamp helper
using namespace std::chrono;
class Logger {
public:
void log(const std::string& msg) {
auto now = system_clock::now();
zoned_time local{current_zone(), now};
std::cout << std::format("[{:%Y-%m-%d %H:%M:%S}] {}",
local, msg) << std::endl;
}
};
int main() {
Logger logger;
logger.log("application start");
}
Common format specifiers
// Date
%Y // year (4 digits)
%m // month (01-12)
%d // day (01-31)
%F // %Y-%m-%d
// Time
%H // hour (00-23)
%M // minute (00-59)
%S // second (00-59)
%T // %H:%M:%S
// Weekday
%A // full weekday
%a // abbreviated
// Month name
%B // full month
%b // abbreviated
// Zone
%z // +0900
%Z // abbreviation
Common pitfalls
Pitfall 1: Locale
std::cout << std::format("{:%A}", 2026y/March/11) << std::endl;
std::cout << std::format(std::locale("ko_KR"), "{:%A}", 2026y/March/11);
Pitfall 2: Pattern mismatch
std::string dateStr = "2026/03/11";
std::istringstream iss{dateStr};
year_month_day ymd;
iss >> parse("%Y-%m-%d", ymd);
if (iss.fail()) {
std::cout << "parse failed" << std::endl;
}
iss.clear();
iss.str("2026-03-11");
iss >> parse("%Y-%m-%d", ymd);
Pitfall 3: Time zones
auto now = system_clock::now();
zoned_time local{current_zone(), now};
std::cout << std::format("{:%F %T}", local) << std::endl;
Pitfall 4: Precision
auto now = system_clock::now();
std::cout << std::format("{:%T}", now) << std::endl;
auto seconds = floor<std::chrono::seconds>(now);
std::cout << std::format("{:%T}", seconds) << std::endl;
Usage patterns
std::format("[{:%F %T}] {}", now, msg);
std::format("backup_{:%Y%m%d_%H%M%S}.db", now);
std::format("{:%Y년 %m월 %d일}", ymd);
std::format("{:%FT%T%z}", zoned_time);
FAQ
Q1: Formatting API?
A: std::format with chrono types (C++20).
Q2: Parsing?
A: std::chrono::parse on a stream.
Q3: Specifiers?
A: strftime-like set; see standard docs.
Q4: Locale?
A: Pass std::locale to std::format when needed.
Q5: Time zones?
A: Use zoned_time and time_zone facilities.
Q6: Learning resources?
A: C++20 – The Complete Guide, C++ Primer, cppreference — chrono.
Related posts (internal)
- C++ Calendar & Timezone
- C++ Barrier & Latch
- C++ subrange
Practical tips
Debugging
- Fix warnings first.
- Reproduce minimally.
Performance
- Profile before optimizing.
Code review
- Follow conventions.
Production checklist
Before coding
- Right approach?
- Maintainable?
- Meets requirements?
While coding
- Warnings cleared?
- Edge cases covered?
At review
- Intent clear?
- Tests sufficient?
Keywords
C++, date, parsing, formatting, C++20
Related posts
- C++ Barrier & Latch |
- C++ Branch Prediction |
- C++ Calendar & Timezone |
- Modern C++ cheatsheet
- C++ Concepts and Constraints |