given a standard ISO 8601 timestamp, is there an easy way to get a time stamp from 20 minutes before and another time stamp of 20 minutes after the original timestamp?
For example given
2010-01-25 18:02:00
I would want one or two functions to return,
2010-01-25 17:48:00 and 2010-01-25 18:22:00
The solution I gave up with is rather cumbersome, I use s.substr(14,2) to get the minutes and s.substr(12,2) to get the hours, convert the hour and minute strings to int and subtract or add the minute value with a condition if it goes above 60 or below 0. Is there a better/easier way to do this?
You could:
struct tmusingstd::get_time.struct tm(no need to consider the ranges).struct tmusingmktime.struct tmusingstd::put_timeNote that
std::get_timeandstd::put_timeare absent in g++/libstdc++. You may need to parse the string using the C functionssscanforstrptime, and print the string usingstrftime. See also C++ date and time.Edit: If we want to change by a generic
std::chrono::duration, it is better we manipulate on astd::chrono::time_pointdirectly. We could create a time_point from astruct tmwith a two-step conversion: time_point ↔ time_t ↔ struct tm, as illustrated below: