Home > Enterprise >  Displaying seconds, minutes and hours from total seconds in C
Displaying seconds, minutes and hours from total seconds in C

Time:05-31

I am trying to solve a problem where I have a total seconds variable, from which I am trying to determine the hours, minutes and seconds.

I do not want to use any external libraries for this task.

What I have noticed is that my seconds variable seems to result in 1 less than the actual value when it is in int form,

but when it is in double form the answer is correct. Why is this?

I would welcome a different approach, perhaps using the remainder operator.

// Example program
#include <iostream>
#include <string>

int main()
{
int total_seconds;
total_seconds = 3870;
int hours, minutes, seconds;
double total_h, total_m, total_s;
int total_hours_int, total_minutes_int;

total_h = (double)total_seconds / 3600;
total_hours_int = total_seconds / 3600;
hours = total_hours_int;

total_m = (total_h - total_hours_int) * 60;

total_minutes_int = (total_h - total_hours_int) * 60;
minutes = total_minutes_int;

total_s = ((double)total_m - total_minutes_int) * 60;

seconds = ((double)total_m - total_minutes_int) * 60;
//seconds = (double)total_s;

std:: cout << hours;
std:: cout << minutes;
std:: cout << total_s;
std:: cout << seconds;

}

Output : 143029

CodePudding user response:

Update:
The answer below was given before the C 98 tag was added to the question.
The chono library is available since C 11, so you can use it only from that version onwards.


You haven't given any context for this task.
My asnwer below assumes you need to solve the problem in any valid C manner (i.e. that it is not mandatory the caculate the numbers "by hand").

If this is the case, you can use the C chrono library for that, as shown below. This solution is shorter and less error-prone, and avoids the type issues you had altogether.

The main class I used is std::chrono::duration and it's helper types (as you can see in the link), as well as std::chrono::duration_cast.

#include <iostream>
#include <chrono>

int main()
{
    int total_seconds = 3870;

    std::chrono::seconds total_secs(total_seconds);
    auto hours = std::chrono::duration_cast<std::chrono::hours>(total_secs);
    auto mins = std::chrono::duration_cast<std::chrono::minutes>(total_secs - hours);
    auto secs = std::chrono::duration_cast<std::chrono::seconds>(total_secs - hours - mins);

    std::cout << "totals seconds: " << total_secs.count() << std::endl;
    std::cout << "  hours: " << hours.count() << std::endl;
    std::cout << "  minutes: " << mins.count() << std::endl;
    std::cout << "  seconds: " << secs.count() << std::endl;
}

Output:

totals seconds: 3870
  hours: 1
  minutes: 4
  seconds: 30

CodePudding user response:

I've reopened answear since it was updated to C 98.

Before C 11 it can be done nicely using standard library:

#include <iostream>
#include <string>
#include <ctime>

int main()
{
    int seconds;
    while (std::cin >> seconds) {
        std::tm t = {};
        t.tm_sec = seconds;
        t.tm_mday = 1;
        mktime(&t);
        t.tm_hour  = t.tm_yday * 24;

        char buf[32];
        strftime(buf, sizeof(buf), "%H:%M:%S", &t);
        std::cout << t.tm_yday << ' ' << seconds << " = " << buf << '\n';
    }

    return 0;
}

https://godbolt.org/z/ceWWfoP6P

  • Related