A single-header C++11 library for parsing, formatting, and managing Unix timestamps-including negative values (dates before 1970).
- Handles both positive and negative Unix timestamps (dates before and after 1970)
- Customizable date/time formatting
Choose order (MDY, DMY, YMD), delimiter, show/hide day of week, time, UTC offset, use 12/24-hour time, month as number or name, and zero-padding - Timezone offset support
- Operator overloading for date comparison and arithmetic
- Zero dependencies beyond the C++11 standard library
#include "datepp.hpp"#include
#include "datepp.hpp"
int main() {
beliumgl::DateTime d("1700000000"); // Unix timestamp as string
std::cout << d.toString() << std::endl; // Default format
}beliumgl::DateTimeFormat fmt("W, DD.MM.YYYY HH:II:SS O UTC");
std::cout << dt.toString(fmt) << std::endl;- Case-insensitive: You can use upper or lower case letters; it doesn't matter.
- Unknown characters are ignored (except when used as delimiters).
- Delimiter: The delimiter will be the last character separating D, M (or A), and Y in your format string.
The format string uses the following tokens:
| Token | Meaning | Note |
|---|---|---|
| W | Day of the week | Use WW for full names |
| D | Day | Use DD for zero-padded day |
| M | Month (number) | Use MM for zero-padded month |
| Y | Year | Use YY for zero-padded year |
| H | Hour | |
| I | Minute | 'I' is used because 'M' is for month |
| S | Second | |
| O | UTC offset | |
| _ | 12-hour format | |
| A | Alphabetical month |
-
WW- Show the full name of the day of the week (e.g., "Thursday"). -
DD,MM,YY- Fill with zeros (e.g., "01", "09", "05"). -
Single token (e.g.,
D,M,Y) - Short names or no leading zeros. -
NOTES: If there are at least one DD or MM or HH, etc., then date and time will also be filled with zeros. If any of H, I, or S is specified, the resulting string will still include all hours, minutes, and seconds.
- The order of D, M (or A), and Y is important!
- For example,
MM/DD/YYandDD/MM/YYwill be interpreted differently.
- For example,
H,I,S,W,O,_, andAare not required in the format.- Omitting them will result in the loss of some information (e.g., time, UTC offset, time format, etc.).
Example format:
"W, DD/MM/YY, HH:II:SS O"
- This would display something like:
Thu, 01/01/1970, 00:00:00 +00 UTC
- Customize how dates are displayed
- Example:
beliumgl::DateTimeFormat fmt("DD.MM.YYYY");
- Construct from Unix timestamp (string or char*)
- Convert to string in your chosen format
- Get individual components: year, month, day, hour, minute, second, day of week, UTC offset
- Operator overloads: compare, add, subtract, multiply, divide dates
beliumgl::DateTime d("1700000000", 2.0); // timestamp, UTC+2
std::cout << d.toString("W, DD/MM/YY HH:II:SS O UTC") << std::endl;- Month and day are zero-based internally (January = 0, first day = 0), but formatted output is 1-based.