简体   繁体   English

time_t舍入为分钟

[英]time_t rounded to minutes

i am using an API which deals with time_t. 我正在使用处理time_t的API。 Now i have a result also from API which retrieves data in time_t also but the problem is the first one only needs the date, hour and minute but the data i retrieve includes seconds, how can i remove the seconds from the data> 现在我也从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);

where m_trades[x].open_time is the data that i retrieve with seconds on it and the m_chart.start is the filter data that only needs date, hour and minutes. 其中m_trades [x] .open_time是我在上面加上秒数的数据,而m_chart.start是仅需要日期,小时和分钟的筛选器数据。

I hope you can help me with this problem. 希望您能帮我解决这个问题。

Thanks. 谢谢。

time_t is in seconds so if you want it to contain only round minutes you have to to subtract the remainder seconds like: time_t以秒为单位,因此,如果您希望它仅包含整数分钟,则必须减去剩余的秒数,例如:

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

eg 例如

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;

has the following output: 具有以下输出:

seconds: 134 / 120 秒:134/120

minutes 2:14 / 2:0 分钟2:14 / 2:0

And if you want to round it up you can test: 如果您想将其汇总,可以进行以下测试:

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

I would recommend use std::chrono::duration . 我建议使用std::chrono::duration It's a bit longer: 更长一点:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM