Top Related Projects
The fastest JSON library in C
A modern formatting library
JSON for Modern C++
Fast C++ logging library.
A fast JSON parser/generator for C++ with both SAX/DOM style API
Efficient binary-decimal and decimal-binary conversion routines for IEEE doubles.
Quick Overview
HowardHinnant/date is a C++ library that provides a robust and efficient implementation of date and time functionality. It offers a modern alternative to the C++11/14/17
Pros
- Highly efficient and optimized for performance
- Comprehensive support for various calendar systems and time zones
- Easy-to-use API with intuitive function names and overloads
- Compliant with the C++20 standard for date and time operations
Cons
- Requires C++11 or later, which may not be available on all platforms
- Learning curve for developers accustomed to traditional C++ date/time libraries
- Limited built-in support for parsing date strings in custom formats
Code Examples
- Creating and manipulating dates:
#include "date/date.h"
#include <iostream>
int main() {
using namespace date;
auto today = year_month_day{floor<days>(std::chrono::system_clock::now())};
auto future = today + months{3};
std::cout << "Today: " << today << "\n";
std::cout << "3 months from now: " << future << "\n";
}
- Working with time zones:
#include "date/tz.h"
#include <iostream>
int main() {
using namespace date;
using namespace std::chrono;
auto now = system_clock::now();
auto tz = locate_zone("America/New_York");
auto nyc_time = make_zoned(tz, now);
std::cout << "Current time in New York: " << nyc_time << "\n";
}
- Calculating durations between dates:
#include "date/date.h"
#include <iostream>
int main() {
using namespace date;
auto start = 2023_y/March/1;
auto end = 2023_y/June/15;
auto duration = sys_days{end} - sys_days{start};
std::cout << "Days between " << start << " and " << end << ": " << duration.count() << "\n";
}
Getting Started
To use the HowardHinnant/date library in your C++ project:
- Clone the repository or download the source files.
- Add the
include
directory to your project's include path. - Include the necessary headers in your code (e.g.,
#include "date/date.h"
for basic date functionality). - If you need time zone support, compile and link against the
libtz.a
orlibtz.so
library.
Example CMake configuration:
add_subdirectory(path/to/date)
target_link_libraries(your_target date::date)
Make sure your compiler supports C++11 or later, and you're ready to use the library in your project.
Competitor Comparisons
The fastest JSON library in C
Pros of yyjson
- Focuses on JSON parsing and serialization, offering high performance
- Provides both mutable and immutable APIs for flexibility
- Supports JSON pointer for easy data access and modification
Cons of yyjson
- Limited to JSON-related functionality
- Lacks date and time manipulation features
- May require additional libraries for non-JSON operations
Code Comparison
date:
#include "date/date.h"
using namespace date;
auto d = 2015_y/March/22;
auto t = make_time(13, 30, 0);
auto dt = d + t;
yyjson:
yyjson_doc *doc = yyjson_read(json, len, 0);
yyjson_val *root = yyjson_doc_get_root(doc);
const char *name = yyjson_get_str(yyjson_obj_get(root, "name"));
yyjson_doc_free(doc);
Key Differences
- date focuses on date and time manipulation, while yyjson specializes in JSON processing
- date is a C++ library, whereas yyjson is written in C
- yyjson offers more JSON-specific features, while date provides comprehensive date/time utilities
- date is part of the C++ standard proposal, potentially becoming a built-in feature in future C++ versions
A modern formatting library
Pros of fmt
- Broader scope: fmt is a general-purpose formatting library, while date focuses specifically on date and time handling
- More active development: fmt has more recent commits and a larger community of contributors
- Extensive formatting options: Supports a wide range of formatting styles for various data types
Cons of fmt
- Larger library size: fmt covers more functionality, potentially increasing binary size
- Steeper learning curve: More features may require more time to master compared to date's focused approach
- Not specialized for date/time: May lack some specific date and time manipulation features found in date
Code Comparison
date:
#include "date/date.h"
auto d = date::year{2023}/date::month{3}/date::day{14};
std::cout << d << '\n';
fmt:
#include <fmt/chrono.h>
auto d = std::chrono::year{2023}/std::chrono::month{3}/std::chrono::day{14};
fmt::print("{:%Y-%m-%d}\n", d);
Both libraries provide ways to work with dates, but fmt offers a more flexible formatting approach while date provides more specialized date/time functionality.
JSON for Modern C++
Pros of json
- Wider scope: Handles JSON parsing, serialization, and manipulation
- More extensive documentation and examples
- Larger community and more frequent updates
Cons of json
- Larger library size and potential performance overhead
- Less specialized, may not be as optimized for specific use cases
Code Comparison
date:
#include "date/date.h"
auto d = date::year{2023}/4/1;
std::cout << d << '\n';
json:
#include <nlohmann/json.hpp>
nlohmann::json j = {{"name", "John"}, {"age", 30}};
std::cout << j.dump(4) << '\n';
Key Differences
- Scope: date focuses on date and time handling, while json is for JSON data
- Usage: date is primarily for temporal calculations, json for data serialization
- Integration: json may be easier to integrate into projects dealing with web APIs or data exchange
Conclusion
Both libraries serve different purposes. date is specialized for date and time operations, while json offers a comprehensive solution for working with JSON data. The choice between them depends on the specific requirements of your project.
Fast C++ logging library.
Pros of spdlog
- Broader functionality: spdlog is a comprehensive logging library, while date focuses solely on date and time handling
- Performance-oriented: spdlog is designed for high-performance logging with minimal overhead
- Thread-safe: spdlog supports concurrent logging from multiple threads
Cons of spdlog
- Larger codebase: spdlog has a more extensive codebase, which may increase compilation times
- Steeper learning curve: spdlog's wider range of features may require more time to master
Code Comparison
spdlog example:
#include "spdlog/spdlog.h"
int main() {
spdlog::info("Welcome to spdlog!");
spdlog::error("An error message with format: {}", 42);
}
date example:
#include "date/date.h"
#include <iostream>
int main() {
auto today = date::year_month_day{date::floor<date::days>(std::chrono::system_clock::now())};
std::cout << "Today is " << today << '\n';
}
While both libraries serve different purposes, they demonstrate clean and intuitive APIs for their respective domains. spdlog provides a straightforward logging interface, while date simplifies date and time operations in modern C++.
A fast JSON parser/generator for C++ with both SAX/DOM style API
Pros of rapidjson
- Focuses on JSON parsing and serialization, offering more specialized features for JSON handling
- Generally faster performance for JSON operations
- Supports in-situ parsing, reducing memory allocation
Cons of rapidjson
- Limited to JSON-related functionality
- Larger codebase and potentially more complex API
- May require more setup and configuration for basic usage
Code Comparison
date:
#include "date/date.h"
auto d = date::year{2023}/4/1;
rapidjson:
#include "rapidjson/document.h"
rapidjson::Document d;
d.Parse("{\"date\":\"2023-04-01\"}");
Key Differences
- date focuses on date and time manipulation, while rapidjson specializes in JSON handling
- date provides a more intuitive API for working with dates, while rapidjson offers powerful JSON parsing capabilities
- date is header-only and easier to integrate, while rapidjson may require more setup but offers more JSON-specific features
Use Cases
- Choose date for projects primarily dealing with date and time calculations
- Opt for rapidjson when working extensively with JSON data or requiring high-performance JSON parsing
Community and Support
- Both projects have active communities and are well-maintained
- date has fewer contributors but is widely used in the C++ community
- rapidjson has a larger contributor base and is backed by Tencent
Efficient binary-decimal and decimal-binary conversion routines for IEEE doubles.
Pros of double-conversion
- Focused on efficient and accurate conversion between binary and decimal floating-point numbers
- Extensively tested and optimized for performance
- Widely used in various projects, including V8 JavaScript engine
Cons of double-conversion
- Limited scope, only handles floating-point conversions
- Less comprehensive date and time functionality
- Requires more manual integration for complex date/time operations
Code Comparison
date:
#include "date/date.h"
using namespace date;
auto d = 2023_y/3/14;
auto t = make_time(12, 30, 0);
auto dt = sys_days{d} + t;
double-conversion:
#include "double-conversion/double-conversion.h"
double_conversion::DoubleToStringConverter converter(flags, infinity_symbol, nan_symbol, exponent_character, decimal_in_shortest_low, decimal_in_shortest_high, max_leading_padding_zeroes_in_precision_mode, max_trailing_padding_zeroes_in_precision_mode);
char buffer[128];
double_conversion::StringBuilder builder(buffer, sizeof(buffer));
converter.ToShortest(3.14159, &builder);
Summary
date provides comprehensive date and time functionality, while double-conversion focuses on efficient floating-point conversions. date offers easier-to-use APIs for date/time operations, whereas double-conversion requires more setup but excels in its specific domain. Choose based on your project's primary needs: date/time manipulation or precise floating-point conversions.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Date
Summary
This is actually several separate C++11/C++14/C++17 libraries:
-
"date.h"
is a header-only library which builds upon<chrono>
. It adds some newduration
types, and newtime_point
types. It also adds "field" types such asyear_month_day
which is a struct{year, month, day}
. And it provides convenient means to convert between the "field" types and thetime_point
types. -
"tz.h"
/"tz.cpp"
are a timezone library built on top of the"date.h"
library. This timezone library is a complete parser of the IANA timezone database. It provides for an easy way to access all of the data in this database, using the types from"date.h"
and<chrono>
. The IANA database also includes data on leap seconds, and this library provides utilities to compute with that information as well. -
"iso_week.h"
is a header-only library built on top of the"date.h"
library which implements the ISO week date calendar.- Documentation: http://howardhinnant.github.io/date/iso_week.html
-
"julian.h"
is a header-only library built on top of the"date.h"
library which implements a proleptic Julian calendar which is fully interoperable with everything above.- Documentation: http://howardhinnant.github.io/date/julian.html
-
"islamic.h"
is a header-only library built on top of the"date.h"
library which implements a proleptic Islamic calendar which is fully interoperable with everything above.- Documentation: http://howardhinnant.github.io/date/islamic.html
Standardization
Slightly modified versions of "date.h"
and "tz.h"
were voted into the C++20 working draft at the Jacksonville FL meeting on 2018-03-17:
Build & Test
The recommended way to use any of these libraries besides "tz.h"
is to just include it. These are header-only libraries (except "tz.h"
).
To use "tz.h"
, there is a single source file (src/tz.cpp
) that needs to be compiled. Here are the recommended directions: https://howardhinnant.github.io/date/tz.html#Installation.
One can run tests by cd'ing into the test
subdirectory and running testit
. There are known failures on all platforms except for macOS. And even on macOS if C++11 is used. If any of these failures present problems for you, there exist workarounds.
Additionally there is unsupported support for vcpkg and CMake. I don't personally use or maintain these systems as for me they cause more problems than they solve (for this small project). If you would like to contribute to these build systems please feel free to file a PR.
You can download and install Date using the vcpkg dependency manager:
git clone https://github.com/Microsoft/vcpkg.git
cd vcpkg
./bootstrap-vcpkg.sh
./vcpkg integrate install
vcpkg install date
The Date port in vcpkg is updated by Microsoft team members and community contributors. If the version falls behind, please create an issue or pull request on the vcpkg repository.
You can optionally build using CMake. Here is a guide of how to build and test using the CMake Makefile generator.
mkdir build
cd build
cmake -DENABLE_DATE_TESTING=ON -DBUILD_TZ_LIB=ON ../
cmake --build . --target testit #Â Consider '-- -j4' for multithreading
Projects using this library
- www.safe.com
- www.webtoolkit.eu/wt
- https://github.com/ViewTouch/viewtouch
- https://routinghub.com
- https://github.com/valhalla
- https://github.com/siodb/siodb
- https://github.com/KomodoPlatform/atomicDEX-Pro
- https://github.com/Kotlin/kotlinx-datetime
- https://github.com/royalbee/jewish_date
- https://github.com/apache/arrow/
If you would like your project (or product) on this list, just let me know.
Top Related Projects
The fastest JSON library in C
A modern formatting library
JSON for Modern C++
Fast C++ logging library.
A fast JSON parser/generator for C++ with both SAX/DOM style API
Efficient binary-decimal and decimal-binary conversion routines for IEEE doubles.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot