Skip to content

Instantly share code, notes, and snippets.

@typelogic
Forked from CaptainJH/Iterate days
Created January 9, 2021 14:27
Show Gist options
  • Save typelogic/41359a0ff7491f35886b20b896c90da4 to your computer and use it in GitHub Desktop.
Save typelogic/41359a0ff7491f35886b20b896c90da4 to your computer and use it in GitHub Desktop.
Output date string in C++
/// iterate from fromD(like "2014-04-02") to toD("2014-05-02")
std::string tpStr = fromD;
do {
//std::cout << tpStr << std::endl;
ParseIntoUserInfoDB(tpStr, root, order);
std::tm tm;
std::stringstream ss(tpStr + " 0:0:1");
ss >> std::get_time(&tm, "%Y-%m-%d %H:%M:%S");
auto tp = std::chrono::system_clock::from_time_t(std::mktime(&tm));
tp += std::chrono::hours(24);
auto in_time_t = std::chrono::system_clock::to_time_t(tp);
std::stringstream ssTp;
ssTp << std::put_time(std::localtime(&in_time_t), "%Y-%m-%d");
tpStr = ssTp.str();
} while (tpStr != toD);
#include <string>
#include <sstream>
#include <chrono>
#include <iomanip>
std::string GetDate(const char* argv)
{
std::string arg(argv);
if (pystring::lower(arg) == "today")
{
auto now = std::chrono::system_clock::now();
auto in_time_t = std::chrono::system_clock::to_time_t(now);
std::stringstream ss;
ss << std::put_time(std::localtime(&in_time_t), "%Y-%m-%d");
return ss.str();
}
return arg;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment