Time_T舍入到几分钟

time_t rounded to minutes

本文关键字:分钟 几分钟 舍入 Time      更新时间:2023-10-16

我正在使用处理时间_t的API。现在,我也有一个API的结果,该结果也可以在time_t中检索数据,但是问题是第一个只需要日期,小时和分钟,但是我检索到的数据包括秒,我如何从数据>

     m_chart.period =   PERIOD_M1;
     m_chart.start  =   m_trades[x].open_time;
     m_chart.end    =   m_trades[x].close_time;
     m_chart.mode   =   CHART_RANGE_IN;
     candles        =   m_manager->ChartRequest(&m_chart, &stamp, &chart_total);

其中m_trades [x] .open_time是我检索到秒的数据,而m_chart.start是仅需要日期,小时和分钟的过滤器数据。

我希望您能帮助我解决这个问题。

谢谢。

time_t在几秒钟内,因此,如果您只包含一个圆形分钟,则必须减去剩余的秒数:

t -= (t % 60) // minutes_only_sec = total_seconds - seconds_reminder_seconds

例如

time_t secs = 2 * 60 + 14;           // 2:14
time_t min_only = (secs - secs % 60);
std::cout << "seconds:" << secs << " / " << min_only << std::endl;
std::cout << "minutes: " << secs / 60 << ":" << (secs % 60)
              << " / " << min_only / 60 << ":" << (min_only % 60) << std::endl;

具有以下输出:

秒:134/120

分钟2:14/2:0

,如果您想围绕它进行测试:

if (secs % 60)
{
    min_only += 60;
}

我建议使用std::chrono::duration。它更长了:

    time_t min_only = std::chrono::seconds(
          std::chrono::duration_cast<std::chrono::minutes>(std::chrono::seconds(secs))
                                           ).count();