Determining how many minutes have passed using two strings with two different set of time ?

Write your question here.


Let's say I have two std::string that have two different times like so
12:05:02
12:20:02
in the HH:MM:SS format
how can I go about getting and storing how many minutes have passed between the two sets of time ? For example, a total of 15 minutes have passed between those two given times.
Last edited on
Use the member function substr and convert the string into minutes using stoi and do your calculations
You can also use std::tm with std::chrono::duration_cast:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <string>
#include <chrono>
#include <sstream>
#include <iomanip>
#include <typeinfo>


int main()
{
    std::string timeStart = "12:05:02";
    std::string timeEnd = "12:20:02";

    std::tm tStart{}, tEnd{};
    //Structure holding a calendar date and time broken down into its components.
    //http://en.cppreference.com/w/cpp/chrono/c/tm

    std::istringstream streamStart(timeStart);
    std::istringstream streamEnd(timeEnd);

    streamStart >> std::get_time(&tStart, "%H:%M:%S");
    streamEnd >> std::get_time(&tEnd, "%H:%M:%S");
    //http://stackoverflow.com/questions/21021388/how-to-parse-a-date-string-into-a-c11-stdchrono-time-point-or-similar

    auto startTime = std::chrono::hours(tStart.tm_hour) + std::chrono::minutes(tStart.tm_min) + std::chrono::seconds(tStart.tm_sec);
    auto endTime = std::chrono::hours(tEnd.tm_hour) + std::chrono::minutes(tEnd.tm_min) + std::chrono::seconds(tEnd.tm_sec);
    //convert std::tm to std::duration for subtraction
    
    auto duration = endTime - startTime;
    
    auto hours   = std::chrono::duration_cast<std::chrono::hours>(duration);
    auto minutes = std::chrono::duration_cast<std::chrono::minutes>(duration - hours);
    auto seconds = std::chrono::duration_cast<std::chrono::seconds>(duration - minutes);
    //Converts a std::chrono::duration to a duration of different type
    //http://en.cppreference.com/w/cpp/chrono/duration/duration_cast
     
    std::tm t{};
   
    t.tm_hour = hours.count();
    t.tm_min  = minutes.count();
    t.tm_sec = seconds.count();

    std::cout << std::put_time(&t, "%H:%M:%S") << '\n';
    //http://en.cppreference.com/w/cpp/io/manip/put_time
}

http://coliru.stacked-crooked.com/a/fb5242db568813fe
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
int seconds_since_midnight( std::string t ) // format is hh:mm:ss (24 hr clock)
{ return std::stoi(t) * 60*60 + std::stoi( t.substr(3) ) * 60 + std::stoi( t.substr(6) ) ; }

int elapsed_completed_minutes( std::string from, std::string to ) // format is hh:mm:ss (24 hr clock)
{
    const auto a = seconds_since_midnight(from) ;

    auto b = seconds_since_midnight(to) ;
    if( b < a ) b += 24*60*60 ; // period crosses over midnight

    return (b-a) / 60 ;
}

http://coliru.stacked-crooked.com/a/bbffa9b005526432
Topic archived. No new replies allowed.