datetime
Part of Kronuz C++ Libraries, a collection of small, reusable C++ libraries. Read its story in The Bag of Holding, a Familiars post.
Date and time parsing for C++20: ISO 8601, Elasticsearch-style date math, times and durations,
and ISO 8601 output. Hand-written scanners read fixed-width digits directly and validate every
field, and iso8601 writes into a stack buffer, so a parse or a format costs tens of
nanoseconds and reports exactly what was wrong. Reach for it in query languages and APIs that
take dates as text.
✨ Highlights
- ISO 8601, date-only through fractional seconds, with
Z or ±HH:MM offsets applied to a
UTC instant; loose forms such as 2020/01/02 03:04:05 too.
- Date math after
||: add and subtract y M w d h m s, round up to the end of a unit
with /M or down to its start with //M, leap years and month lengths included.
- Times and durations:
"15:09:26.5" and "+01:30:00", to and from seconds.
- Years 1 to
INT_MAX. Date-math terms are 64-bit, and a result outside the range throws.
- Clear errors:
DatetimeError and its subclasses, all std::runtime_error.
- Predicates:
isDate, isDatetime, isTime, isTimedelta.
🚀 Quick start
#include "datetime.h"
#include <cstdio>
#include <string_view>
int main() {
auto tm = Datetime::DatetimeParser(std::string_view("2021-03-14T15:09:26.5Z"));
std::printf("%.1f\n", Datetime::timestamp(tm)); // 1615734566.5
std::puts(Datetime::iso8601(tm).c_str()); // 2021-03-14T15:09:26.5Z
// Date math: one month later, then round up to the end of that month.
auto end_of_month = Datetime::DatetimeParser(std::string_view("2020-01-10||+1M/M"));
std::puts(Datetime::iso8601(end_of_month).c_str()); // 2020-02-29T23:59:59.999999
}
examples/demo.cc walks every input form and the rounding rules.
📦 Installation
Requires a C++20 compiler and CMake 3.14 or newer; it fetches
strict-stox at a pinned commit and compiles one source
file. CI builds it on Linux (x86_64, arm64), macOS (arm64, x86_64), and FreeBSD, plus ASan,
UBSan, and TSan builds.
include(FetchContent)
FetchContent_Declare(datetime
GIT_REPOSITORY https://github.com/Kronuz/datetime.git
GIT_TAG f2541338c2d689f3ab83c1734f096a6a51a1e6cf) # main tracks the tip
FetchContent_MakeAvailable(datetime)
target_link_libraries(my_app PRIVATE datetime::datetime)
Then #include "datetime.h"; everything is in the Datetime namespace.
🎯 When to use it
It fits dates typed by users or carried in query strings, including relative date math.
Not for:
- Named time zones (
America/Mexico_City): it applies numeric offsets only.
- Calendars other than the proleptic Gregorian.
- "now". Resolving expressions relative to the current time is the caller's job.
⚡ Performance
datetime_compare times each entry point against independent implementations of the same work:
hand is a hand-written ISO 8601 parser and formatter (in bench/compare.cc) that reads one
grammar and reports failure with a bool; days-from-civil and civil-from-days are the
calendar algorithms of
Howard Hinnant, written out in the
benchmark; strptime+timegm is libc's; absl is
abseil 20260817.0
(ParseTime, FromCivil, FormatTime, Homebrew). datetime_compare --check (CTest
datetime_compare) requires each comparator to return the library's values on the benchmark's
inputs; abseil's FormatTime writes no fraction, so its row compares the time only. ns per call,
one thread, 4,096 random inputs, best of two runs of seven passes, Apple clang 17 with -O3, on
an Apple M4 Pro (macOS 26, arm64) and an Intel Core i9-9980HK (macOS 15, x86-64). The machine
columns hold datetime's ns and the comparators columns the comparators' ns; lower is faster.
| Operation |
Apple M4 Pro |
comparators |
Intel Core i9-9980HK |
comparators |
parse 2020-01-02 |
11.2 |
hand 6.1 |
25.8 |
hand 14.3 |
parse 2020-01-02T03:04:05Z |
16.6 |
hand 7.9, absl 98.6 |
39.5 |
hand 19.9, absl 257.9 |
parse 2020-01-02T03:04:05.123456Z |
20.3 |
hand 10.1, absl 133.7 |
48.9 |
hand 26.9, absl 346.2 |
parse 2020-01-02T03:04:05+05:30 |
29.5 |
hand 9.0 |
61.7 |
hand 23.2 |
parse 2020/01/02 03:04:05 |
20.2 |
strptime+timegm 2178, hand 9.4 |
52.0 |
strptime+timegm 6199, hand 19.1 |
timestamp(tm_t) |
2.8 |
days-from-civil 1.7, absl 26.6 |
6.4 |
days-from-civil 4.1, absl 58.6 |
to_tm_t(time_t) |
8.9 |
civil-from-days 6.0 |
20.6 |
civil-from-days 12.4 |
iso8601(tm_t) |
22.5 |
hand 8.7, absl 1102 |
42.5 |
hand 22.9, absl 1898 |
isDatetime |
13.6 |
hand 9.9 |
27.3 |
hand 22.9 |
datetime takes 1.2x to 3.3x the hand-written parser's time, for the loose forms, offsets, date
math, field validation and error reports the hand parser lacks, and about a seventh to a sixth of
abseil's ParseTime. Repeated runs spread under 3% on the M4 Pro and, on the i9, under 3% for all
rows but the loose-form parse and iso8601, which spread up to 21%.
cmake -B build -DDATETIME_BUILD_BENCH=ON -DCMAKE_BUILD_TYPE=Release && cmake --build build
./build/datetime_compare
📚 Documentation
🛠️ Building and testing
cmake -B build && cmake --build build && ctest --test-dir build
./build/datetime_demo
cmake -B build-bench -DDATETIME_BUILD_BENCH=ON -DCMAKE_BUILD_TYPE=Release && cmake --build build-bench
ctest runs datetime_diff_test, which compares every entry point with a frozen reference
implementation in test/old/ on random, mutated and exhaustive inputs, and
datetime_datemath_test, which checks date math against independent references. The input
classes whose result differs from the reference are listed at the top of test/diff_test.cc.
With -DDATETIME_BUILD_BENCH=ON, ctest also checks that every comparator in
datetime_compare returns the library's results, and datetime_bench times the library against
the reference.
🤝 Contributing
Issues and pull requests are welcome. Read AGENTS.md and the catalog's
rules of engagement
first; CI must pass on every platform.
📄 License
MIT, Copyright (c) 2026 Germán Méndez Bravo and (c) 2015-2020 Dubalu LLC. See
LICENSE. Extracted from Xapiand.