Created
May 30, 2023 07:57
-
-
Save dshadow/3a4f9f26bc8cd5e4488213f826753f67 to your computer and use it in GitHub Desktop.
Ability to parse date w/o libfmt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iomanip> | |
#include <iostream> | |
#include <string> | |
#include <sstream> | |
#include <chrono> | |
using namespace std; | |
const char * time_format = "%Y-%m-%d"; | |
auto string2date(string src) { | |
tm tm_p = {}; | |
stringstream buf(src); | |
buf >> get_time(&tm_p, time_format); | |
return chrono::system_clock::from_time_t(mktime(&tm_p)); | |
} | |
auto date2string(chrono::system_clock::time_point t) { | |
auto t_raw = chrono::system_clock::to_time_t(t); | |
stringstream buf; | |
buf << put_time(localtime(&t_raw), time_format); | |
return buf.str(); | |
} | |
int main() { | |
string test = "2023-05-03"; | |
auto time_p = string2date(test); | |
auto to_str = date2string(time_p); | |
if (test == to_str) cout << "Passed" << endl; | |
else cerr << "Error" << endl; | |
return 0; | |
} |
Author
dshadow
commented
May 30, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment