1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
#include <iostream>
#include <chrono>
#include <ctime>
#include <string>
//http://cppstdlib.com/code/util/timepoint.hpp.html
// convert timepoint of system clock to calendar time string
inline
std::string asString (const std::chrono::system_clock::time_point& tp)
{
// convert to system time:
std::time_t t = std::chrono::system_clock::to_time_t(tp);
std::string ts = ctime(&t); // convert to calendar time
ts.resize(ts.size()-1); // skip trailing newline
return ts;
}
// convert calendar time to timepoint of system clock
inline
std::chrono::system_clock::time_point
makeTimePoint (int year, int mon, int day,
int hour, int min, int sec=0)
{
struct std::tm t;
t.tm_sec = sec; // second of minute (0 .. 59 and 60 for leap seconds)
t.tm_min = min; // minute of hour (0 .. 59)
t.tm_hour = hour; // hour of day (0 .. 23)
t.tm_mday = day; // day of month (1 .. 31)
t.tm_mon = mon-1; // month of year (0 .. 11)
t.tm_year = year-1900; // year since 1900
t.tm_isdst = -1; // determine whether daylight saving time
std::time_t tt = std::mktime(&t);
if (tt == -1) {
throw "no valid system time";
}
return std::chrono::system_clock::from_time_t(tt);
}
int main()
{
using Days = std::chrono::duration<int, std::ratio<3600*24>>;
// define type for durations that represent day(s):
auto start_tp = makeTimePoint(2012, 5, 4, 7, 42);//2012 4th May 7:42
auto end_tp = makeTimePoint (2015, 11, 19, 23, 6);//2015 19th Nov 23:06
while (start_tp < end_tp)
{
start_tp += Days(3) + std::chrono::hours (13) + std::chrono::minutes(47);
//every 3 days, 13 hours and 47 minutes
std::cout << asString(start_tp) << "\n";//prints the incremental timepoints
}
}
|