batch 1999
batch 1999

Reputation: 109

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.

Upvotes: 0

Views: 1795

Answers (1)

Mihayl
Mihayl

Reputation: 3911

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

e.g

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();

Upvotes: 4

Related Questions