From a5a9805a91502d392cd927f4a368a187145bfd9a Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sun, 25 Nov 2018 16:10:05 -0800 Subject: [PATCH] First stub at the datetime format parser --- include/fmt/time.h | 51 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/include/fmt/time.h b/include/fmt/time.h index c5bacfe4..96b07225 100644 --- a/include/fmt/time.h +++ b/include/fmt/time.h @@ -1,6 +1,6 @@ // Formatting library for C++ - time formatting // -// Copyright (c) 2012 - 2016, Victor Zverovich +// Copyright (c) 2012 - present, Victor Zverovich // All rights reserved. // // For the license information refer to format.h. @@ -22,7 +22,56 @@ inline null<> localtime_r FMT_NOMACRO(...) { return null<>(); } inline null<> localtime_s(...) { return null<>(); } inline null<> gmtime_r(...) { return null<>(); } inline null<> gmtime_s(...) { return null<>(); } + +// Parses a put_time-like format string and invokes handler actions. +template +FMT_CONSTEXPR void parse_datetime_format( + basic_string_view format_str, Handler &&handler) { + auto begin = format_str.data(); + auto end = begin + format_str.size(); + auto ptr = begin; + while (ptr != end) { + auto c = *ptr; + if (c == '}') break; + if (c != '%') { + ++ptr; + continue; + } + if (begin != ptr) + handler.on_text(begin, ptr); + c = *++ptr; + begin = ptr; + switch (c) { + case '%': + handler.on_text(ptr, ptr + 1); + break; + // Day of the week: + case 'a': + handler.on_abbr_weekday(); + break; + case 'A': + handler.on_full_weekday(); + break; + case 'w': + handler.on_dec0_weekday(); + break; + case 'u': + handler.on_dec1_weekday(); + break; + // Month: + case 'b': case 'h': + handler.on_abbr_month(); + break; + case 'B': + handler.on_full_month(); + break; + // TODO: parse more format specifiers + } + } + if (begin != ptr) + handler.on_text(begin, ptr); } +} // namespace internal // Thread-safe replacement for std::localtime inline std::tm localtime(std::time_t time) {