[2026] C++ date parsing and formatting | chrono, std::format, and time zones

[2026] C++ date parsing and formatting | chrono, std::format, and time zones

이 글의 핵심

Format and parse calendar dates with C++20 chrono, std::format, parse, zoned_time, locale-aware weekday names, and common pitfalls for wire formats vs display.

Date parsing and formatting (C++20)

String conversion for calendar types. 아래 코드는 cpp를 사용한 구현 예제입니다. 필요한 모듈을 import하고. 각 부분의 역할을 이해하면서 코드를 살펴보시기 바랍니다.

#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

아래 코드는 cpp를 사용한 구현 예제입니다. 필요한 모듈을 import하고. 코드를 직접 실행해보면서 동작을 확인해보세요.

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)

아래 코드는 cpp를 사용한 구현 예제입니다. 필요한 모듈을 import하고. 각 부분의 역할을 이해하면서 코드를 살펴보시기 바랍니다.

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

아래 코드는 cpp를 사용한 구현 예제입니다. 필요한 모듈을 import하고, 조건문으로 분기 처리를 수행합니다. 각 부분의 역할을 이해하면서 코드를 살펴보시기 바랍니다.

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

아래 코드는 cpp를 사용한 구현 예제입니다. 필요한 모듈을 import하고. 코드를 직접 실행해보면서 동작을 확인해보세요.

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

다음은 cpp를 활용한 상세한 구현 코드입니다. 필요한 모듈을 import하고, 클래스를 정의하여 데이터와 기능을 캡슐화하며. 각 부분의 역할을 이해하면서 코드를 살펴보시기 바랍니다.

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

다음은 cpp를 활용한 상세한 구현 코드입니다. 각 부분의 역할을 이해하면서 코드를 살펴보시기 바랍니다.

// 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

아래 코드는 cpp를 사용한 구현 예제입니다. 조건문으로 분기 처리를 수행합니다. 각 부분의 역할을 이해하면서 코드를 살펴보시기 바랍니다.

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

아래 코드는 cpp를 사용한 구현 예제입니다. 코드를 직접 실행해보면서 동작을 확인해보세요.

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

다음은 간단한 cpp 코드 예제입니다. 코드를 직접 실행해보면서 동작을 확인해보세요.

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.

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

... 996 lines not shown ... Token usage: 63706/1000000; 936294 remaining Start-Sleep -Seconds 3