分钟分段
static std::map<std::string, std::string> divideTimeIntoSegments(const std::string& btm, const std::string& etm, const int& tmIntervalMinutes)
{
std::map<std::string, std::string> segments;
if (tmIntervalMinutes == 0)
{
segments[etm] = btm;
return segments;
}
boost::posix_time::ptime startTime = simple_time::TimeFromString(btm);
boost::posix_time::ptime endTime = simple_time::TimeFromString(etm);
boost::posix_time::ptime currentSegmentStart = startTime;
boost::posix_time::ptime currentSegmentEnd = startTime + boost::posix_time::minutes(tmIntervalMinutes);
while (currentSegmentEnd <= endTime)
{
segments[TimeToString(currentSegmentEnd, "%Y-%m-%d %H:%M:%S")] = TimeToFormatString(currentSegmentStart, DATE_FORMAT_1);
currentSegmentStart = currentSegmentEnd;
currentSegmentEnd += boost::posix_time::minutes(tmIntervalMinutes);
}
return segments;
}
小时分段
static std::map<std::string, std::string> divideTimeIntoSegments(const std::string& btm, const std::string& etm, const int& tmInterval)
{
std::map<std::string, std::string> segments;
PosixTime startTime = simple_time::TimeFromString(btm);
PosixTime endTime = simple_time::TimeFromString(etm);
PosixTime currentSegmentStart = startTime;
PosixTime currentSegmentEnd = startTime + boost::posix_time::hours(tmInterval);
while (currentSegmentEnd <= endTime)
{
segments[TimeToFormatString(currentSegmentEnd, DATE_FORMAT_1)] = TimeToFormatString(currentSegmentStart, DATE_FORMAT_1);
currentSegmentStart = currentSegmentEnd;
currentSegmentEnd += boost::posix_time::hours(tmInterval);
}
return segments;
}
时间转字符串
static std::string TimeToString(const boost::posix_time::ptime& time, const std::string& format = "%Y-%m-%d %H:%M")
{
std::string retStr;
try
{
boost::posix_time::time_facet* tf = new boost::posix_time::time_facet(format.c_str());
std::stringstream ss;
ss.imbue(std::locale(std::cout.getloc(), tf));
ss << time;
retStr = ss.str();
}
catch (const std::exception& e)
{
SU_ERROR_PRINT("%s", e.what())
}
return retStr;
}