Welcome to the TimeDuration repository! This lightweight C++17 header-only library allows you to parse and format human-readable time durations with ease. Whether you're working with durations like "2d 5h 30m" or need to integrate with chrono
or SQL intervals, this library has you covered.
- Features
- Installation
- Usage
- Examples
- Integration with Chrono
- SQL Interval Output
- Flexible Duration Construction
- Contributing
- License
- Release Information
- Human-Readable Parsing: Convert strings like "2d 5h 30m" into usable duration objects.
- Formatting: Easily format duration objects back into human-readable strings.
- Chrono Integration: Seamlessly work with the C++ standard library's
chrono
. - SQL Interval Output: Output durations in SQL interval format for database use.
- Flexible Construction: Create duration objects using various time units.
To use TimeDuration, simply include the header file in your project. Since this is a header-only library, no additional setup is required.
#include "timeduration.h"
Here’s a simple example of how to use TimeDuration:
#include "timeduration.h"
#include <iostream>
int main() {
TimeDuration duration("2d 5h 30m");
std::cout << "Duration in seconds: " << duration.to_seconds() << std::endl;
std::cout << "Formatted: " << duration.format() << std::endl;
return 0;
}
You can parse a duration string like this:
TimeDuration duration("1d 2h 15m");
std::cout << duration.to_seconds(); // Outputs total seconds
You can format a duration object back into a string:
std::string formatted = duration.format();
std::cout << formatted; // Outputs "1d 2h 15m"
TimeDuration works well with C++'s chrono
library. You can convert your duration to chrono::duration
easily:
#include <chrono>
std::chrono::seconds chrono_duration = duration.to_chrono_seconds();
If you need to output your duration in SQL interval format, TimeDuration makes this easy:
std::string sql_interval = duration.to_sql_interval();
std::cout << sql_interval; // Outputs "INTERVAL '2 days 5 hours 30 minutes'"
You can create durations in various ways:
TimeDuration duration1(3600); // 1 hour in seconds
TimeDuration duration2(1, TimeUnit::DAYS); // 1 day
We welcome contributions! Please fork the repository and submit a pull request with your changes.
- Fork the repository.
- Create a new branch (
git checkout -b feature/YourFeature
). - Make your changes.
- Commit your changes (
git commit -m 'Add some feature'
). - Push to the branch (
git push origin feature/YourFeature
). - Create a new Pull Request.
This project is licensed under the MIT License. See the LICENSE file for details.
For the latest releases, visit here. You can download the latest version and execute it in your project.
Feel free to check the "Releases" section for more details.
TimeDuration is designed to simplify working with time durations in C++. Whether you are developing applications that require time calculations or need to format durations for display, this library provides the necessary tools in a lightweight package.
We hope you find this library useful for your projects. For any issues or suggestions, please feel free to reach out or open an issue in the repository.
Happy coding!