Skip to content

Instantly share code, notes, and snippets.

@dshadow
Created May 30, 2023 07:57
Show Gist options
  • Save dshadow/3a4f9f26bc8cd5e4488213f826753f67 to your computer and use it in GitHub Desktop.
Save dshadow/3a4f9f26bc8cd5e4488213f826753f67 to your computer and use it in GitHub Desktop.
Ability to parse date w/o libfmt
#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;
}
@dshadow
Copy link
Author

dshadow commented May 30, 2023

g++ -std=c++20 test_date_parser_wo_fmt.cpp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment