简体   繁体   中英

time_t rounded to minutes

i am using an API which deals with time_t. 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>

     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.

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:

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

minutes 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 . It's a bit longer:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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