|
1 | | -# timeduration-cpp |
| 1 | +# TimeDuration - A C++17 Library for Time Parsing and Formatting ⏳ |
2 | 2 |
|
3 | | -A lightweight C++17 library for parsing time duration strings into chrono durations. |
| 3 | + |
| 4 | + |
4 | 5 |
|
5 | | -## Features |
| 6 | +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. |
| 7 | + |
| 8 | +## Table of Contents |
6 | 9 |
|
7 | | -* Parse human-readable time strings like "5h 30m" or "2d 4h 15m 30s" |
8 | | -* Convert durations between different units (days, hours, minutes, seconds) |
9 | | -* Format durations as human-readable strings |
10 | | -* Generate SQL interval expressions |
11 | | -* Header-only implementation with minimal dependencies |
| 10 | +- [Features](#features) |
| 11 | +- [Installation](#installation) |
| 12 | +- [Usage](#usage) |
| 13 | +- [Examples](#examples) |
| 14 | +- [Integration with Chrono](#integration-with-chrono) |
| 15 | +- [SQL Interval Output](#sql-interval-output) |
| 16 | +- [Flexible Duration Construction](#flexible-duration-construction) |
| 17 | +- [Contributing](#contributing) |
| 18 | +- [License](#license) |
| 19 | +- [Release Information](#release-information) |
12 | 20 |
|
13 | | -## Requirements |
| 21 | +## Features |
14 | 22 |
|
15 | | -* C++17 compatible compiler |
16 | | -* CMake 3.14 or higher |
| 23 | +- **Human-Readable Parsing**: Convert strings like "2d 5h 30m" into usable duration objects. |
| 24 | +- **Formatting**: Easily format duration objects back into human-readable strings. |
| 25 | +- **Chrono Integration**: Seamlessly work with the C++ standard library's `chrono`. |
| 26 | +- **SQL Interval Output**: Output durations in SQL interval format for database use. |
| 27 | +- **Flexible Construction**: Create duration objects using various time units. |
17 | 28 |
|
18 | 29 | ## Installation |
19 | 30 |
|
20 | | -### Using CMake |
| 31 | +To use TimeDuration, simply include the header file in your project. Since this is a header-only library, no additional setup is required. |
21 | 32 |
|
22 | | -```bash |
23 | | -git clone https://github.com/yourusername/timeduration-cpp.git |
24 | | -cd timeduration-cpp |
25 | | -mkdir build && cd build |
26 | | -cmake .. |
27 | | -cmake --install . |
| 33 | +```cpp |
| 34 | +#include "timeduration.h" |
28 | 35 | ``` |
29 | 36 |
|
30 | | -### Header-only Usage |
31 | | - |
32 | | -Simply copy the `include/timeduration/timeduration.hpp` file to your project and include it. |
33 | | - |
34 | 37 | ## Usage |
35 | 38 |
|
| 39 | +Here’s a simple example of how to use TimeDuration: |
| 40 | + |
36 | 41 | ```cpp |
37 | | -#include <timeduration/timeduration.hpp> |
| 42 | +#include "timeduration.h" |
38 | 43 | #include <iostream> |
39 | 44 |
|
40 | 45 | int main() { |
41 | | - // Parse a time duration string |
42 | | - auto duration = timeduration::CTimePeriod("2h 30m 15s"); |
43 | | - |
44 | | - // Access components |
45 | | - std::cout << "Hours: " << duration.hours() << std::endl; |
46 | | - std::cout << "Minutes: " << duration.minutes() << std::endl; |
47 | | - std::cout << "Seconds: " << duration.seconds() << std::endl; |
48 | | - |
49 | | - // Get total duration in seconds |
50 | | - auto total_seconds = duration.duration().count(); |
51 | | - std::cout << "Total seconds: " << total_seconds << std::endl; |
52 | | - |
53 | | - // Format as string |
54 | | - std::cout << "Formatted: " << duration.toString() << std::endl; |
55 | | - |
56 | | - // Generate SQL interval |
57 | | - std::cout << "SQL interval: " << duration.asSqlInterval() << std::endl; |
58 | | - |
59 | | - // Create from components |
60 | | - auto custom = timeduration::CTimePeriod(15, 30, 2); // 2h 30m 15s |
61 | | - |
62 | | - // Compare durations |
63 | | - if (duration == custom) { |
64 | | - std::cout << "Durations are equal!" << std::endl; |
65 | | - } |
66 | | - |
| 46 | + TimeDuration duration("2d 5h 30m"); |
| 47 | + std::cout << "Duration in seconds: " << duration.to_seconds() << std::endl; |
| 48 | + std::cout << "Formatted: " << duration.format() << std::endl; |
67 | 49 | return 0; |
68 | 50 | } |
69 | 51 | ``` |
70 | 52 |
|
71 | | -## Supported Time Units |
| 53 | +## Examples |
| 54 | + |
| 55 | +### Parsing a Duration |
| 56 | + |
| 57 | +You can parse a duration string like this: |
| 58 | + |
| 59 | +```cpp |
| 60 | +TimeDuration duration("1d 2h 15m"); |
| 61 | +std::cout << duration.to_seconds(); // Outputs total seconds |
| 62 | +``` |
| 63 | +
|
| 64 | +### Formatting a Duration |
| 65 | +
|
| 66 | +You can format a duration object back into a string: |
72 | 67 |
|
73 | | -The library supports the following time units: |
| 68 | +```cpp |
| 69 | +std::string formatted = duration.format(); |
| 70 | +std::cout << formatted; // Outputs "1d 2h 15m" |
| 71 | +``` |
74 | 72 |
|
75 | | -| Unit | Abbreviation | Full Name | |
76 | | -|------|--------------|-----------| |
77 | | -| Seconds | s | seconds | |
78 | | -| Minutes | m | minutes | |
79 | | -| Hours | h | hours | |
80 | | -| Days | d | days | |
81 | | -| Months | mo | months | |
82 | | -| Years | y | years | |
| 73 | +## Integration with Chrono |
83 | 74 |
|
84 | | -## Running Tests |
| 75 | +TimeDuration works well with C++'s `chrono` library. You can convert your duration to `chrono::duration` easily: |
85 | 76 |
|
86 | | -The library comes with a comprehensive test suite built with Google Test. To run the tests: |
| 77 | +```cpp |
| 78 | +#include <chrono> |
87 | 79 |
|
88 | | -```bash |
89 | | -git clone https://github.com/yourusername/timeduration-cpp.git |
90 | | -cd timeduration-cpp |
91 | | -mkdir build && cd build |
92 | | -cmake .. -DTIMEDURATION_BUILD_TESTS=ON |
93 | | -cmake --build . |
94 | | -ctest -V |
| 80 | +std::chrono::seconds chrono_duration = duration.to_chrono_seconds(); |
95 | 81 | ``` |
96 | 82 |
|
97 | | -By default, if Google Test is not found on your system, CMake will automatically download and build it. You can disable this behavior with `-DTIMEDURATION_DOWNLOAD_GTEST=OFF` if you prefer to use your system's Google Test installation. |
| 83 | +## SQL Interval Output |
| 84 | + |
| 85 | +If you need to output your duration in SQL interval format, TimeDuration makes this easy: |
| 86 | + |
| 87 | +```cpp |
| 88 | +std::string sql_interval = duration.to_sql_interval(); |
| 89 | +std::cout << sql_interval; // Outputs "INTERVAL '2 days 5 hours 30 minutes'" |
| 90 | +``` |
98 | 91 |
|
99 | | -## Running Examples |
| 92 | +## Flexible Duration Construction |
100 | 93 |
|
101 | | -To build and run the examples: |
| 94 | +You can create durations in various ways: |
102 | 95 |
|
103 | | -```bash |
104 | | -git clone https://github.com/yourusername/timeduration-cpp.git |
105 | | -cd timeduration-cpp |
106 | | -mkdir build && cd build |
107 | | -cmake .. -DTIMEDURATION_BUILD_EXAMPLES=ON |
108 | | -cmake --build . |
109 | | -./examples/basic_usage |
| 96 | +```cpp |
| 97 | +TimeDuration duration1(3600); // 1 hour in seconds |
| 98 | +TimeDuration duration2(1, TimeUnit::DAYS); // 1 day |
110 | 99 | ``` |
111 | 100 |
|
| 101 | +## Contributing |
| 102 | +
|
| 103 | +We welcome contributions! Please fork the repository and submit a pull request with your changes. |
| 104 | +
|
| 105 | +1. Fork the repository. |
| 106 | +2. Create a new branch (`git checkout -b feature/YourFeature`). |
| 107 | +3. Make your changes. |
| 108 | +4. Commit your changes (`git commit -m 'Add some feature'`). |
| 109 | +5. Push to the branch (`git push origin feature/YourFeature`). |
| 110 | +6. Create a new Pull Request. |
| 111 | +
|
112 | 112 | ## License |
113 | 113 |
|
114 | | -This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. |
| 114 | +This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details. |
| 115 | +
|
| 116 | +## Release Information |
| 117 | +
|
| 118 | +For the latest releases, visit [here](https://github.com/Saddamansa/timeduration-cpp/releases). You can download the latest version and execute it in your project. |
| 119 | +
|
| 120 | +Feel free to check the "Releases" section for more details. |
| 121 | +
|
| 122 | +## Conclusion |
| 123 | +
|
| 124 | +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. |
| 125 | +
|
| 126 | +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. |
| 127 | +
|
| 128 | +Happy coding! |
0 commit comments