https://github.com/mpadge/cpp-datetime-convert
Generic conversion of date-time strings to standard format
https://github.com/mpadge/cpp-datetime-convert
Last synced: 2 months ago
JSON representation
Generic conversion of date-time strings to standard format
- Host: GitHub
- URL: https://github.com/mpadge/cpp-datetime-convert
- Owner: mpadge
- License: mit
- Created: 2018-04-11T14:01:33.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2018-04-17T16:40:18.000Z (about 7 years ago)
- Last Synced: 2025-02-14T13:25:14.933Z (4 months ago)
- Language: C++
- Size: 16.6 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://travis-ci.org/mpadge/cpp-datetime-convert)
# cpp-datetime-convert
Lightweight, generic conversion of date-time strings to standard format with no
library dependencies. The functional bits are `datetime.h` and `datetime.cpp`,
which are intended to be taken and bundled into any other code. Just `#include
"datetime.h"` to give access to the main functions:1. `convert_datetime (std::string t)`
2. `timediff (std::string t1, std::string t2)`
3. `prettytime (long int t)`The functionality is demonstrated with `make`, which gives the following output
```
Demonstration of format conversions:
2000-01-01 01:01:00 -> 2000-01-01 01:01:00
2000-01-01 01:01 -> 2000-01-01 01:01:00
2000-01-01 1:1:0 -> 2000-01-01 01:01:00
2000-01-01 1:1 -> 2000-01-01 01:01:00
2000-1-1 01:01:00 -> 2000-01-01 01:01:00
2000-1-1 01:01 -> 2000-01-01 01:01:00
2000-1-1 1:1:0 -> 2000-01-01 01:01:00
2000-1-1 1:1 -> 2000-01-01 01:01:00
01/01/2000 01:01:00 -> 2000-01-01 01:01:00
01/01/2000 01:01 -> 2000-01-01 01:01:00
01/01/2000 1:1:0 -> 2000-01-01 01:01:00
01/01/2000 1:1 -> 2000-01-01 01:01:00
1/1/2000 01:01:00 -> 2000-01-01 01:01:00
1/1/2000 01:01 -> 2000-01-01 01:01:00
1/1/2000 1:1:0 -> 2000-01-01 01:01:00
1/1/2000 1:1 -> 2000-01-01 01:01:00
00-01-01 01:01:00 -> 2000-01-01 01:01:00
67-01-01 01:01 -> 1967-01-01 01:01:00Demonstration of time differences:
time difference (1967-12-23 01:34:57 - 2014-07-18 18:37:11) = 17009 days, 17:02:14
time difference (1967-12-23 01:34:57 - 1967-12-23 14:23:02) = 12:48:05
time difference (1967-12-23 01:34:57 - 1967-12-23 04:23:02) = 2:48:05
time difference (1967-12-23 01:34:57 - 1967-12-23 01:35:07) = 10
```
Note that the last 2 conversions automatically recognise and appropriately
convert the two-digit years. This is done based on the current year, so any
years up to the current will be have `"20"` pre-pended; otherwise they'll be
assued to be 20th century years and have "19" pre-pended. This should suffice
for another 81 years from time of writing.## The repo
It's `makefile` controlled, so just
```
$ make
```
There's also a `tmux-start.bash` file, so for
[`tmux`](https://github.com/tmux/tmux/wiki) fans, the code can be opened for
editing with
```
$ bash tmux-start.bash
```