RexBruin17
RexBruin17

Reputation: 11

I am having trouble finding a formula

So we are supposed to convert 453456 seconds into years, days, hours, minutes, and seconds.

However, I cannot seem to get past years.

Here is what I have so far:

#include<iostream>

using namespace std;



 int main (){
        int secDis;
        int years;
        const int yearsSec = 31536000;
        int days;
        cout << "Please give me the time of travel in seconds.";
        cin >> secDis;
        years = secDis/yearsSec;
        days = (yearsSec%secDis) / 1440; /*idk if I am on the right track*/
        cout << "You have been traveling for: "
             << years << days;

If it is 453456 seconds it should be 0 years 5 days 5 hours 57 minutes and 36 secs.

//I hate my math skills.

Upvotes: 1

Views: 82

Answers (4)

molbdnilo
molbdnilo

Reputation: 66371

You want the remainder from the division secDis / yearsSec, which is secDis % yearsSec – not yearsSec % secDis.
(That is, to get the remainder, replace /with %.)

I believe it gets easier if you define the number of seconds in each unit of time explicitly:

// Let the computer do the computing.
const int perMinute = 60;
const int perHour =   60 * perMinute;
const int perDay =    24 * perHour;
const int perYear =   365 * perDay;

and spell out every step of the computations:

int years =       totalSeconds / perYear;
int daySeconds =  totalSeconds % perYear;
int days =        daySeconds / perDay;
int hourSeconds = daySeconds % perDay;
int hours =       hourSeconds / perHour;
// ...

Upvotes: 0

Howard Hinnant
Howard Hinnant

Reputation: 218720

I really like TemplateRex's answer! (upvoted it).

But here's a way to solve this problem with just the <chrono> library, assuming your definition that a year is 365 days (a pretty coarse assumption):

#include <chrono>
#include <iostream>

int
main()
{
    using namespace std;
    using namespace std::chrono;

    using days  = duration<int, ratio_multiply<ratio<24>, hours::period>>;
    using years = duration<int, ratio_multiply<ratio<365>, days::period>>;

    auto s = 453456s;
    auto y = duration_cast<years>(s);
    s -= y;
    auto d = duration_cast<days>(s);
    s -= d;
    auto h = duration_cast<hours>(s);
    s -= h;
    auto m = duration_cast<minutes>(s);
    s -= m;
    std::cout << y.count() << " years "
              << d.count() << " days "
              << h.count() << " hours "
              << m.count() << " minutes "
              << s.count() << " seconds\n";
}

<chrono> already has units hours, minutes and seconds. Just add two more units for days and years, and now you can just use duration_cast to successively truncate the seconds into coarser units, and then subtract that truncation out from the seconds (a modulo operation). Just continue with finer and finer units until you are down to your finest unit (seconds in this example). The above program outputs:

0 years 5 days 5 hours 57 minutes 36 seconds

This lets <chrono> do all the conversions for you, reducing the chance of errors.

Upvotes: 1

TemplateRex
TemplateRex

Reputation: 70516

@HowardHinnant has provided a Swiss-army-knife date library for such questions, see this Q&A, after which using

std::cout << human_readable_difference(second_point{453456s},
                                       second_point{0s});}

will indeed print:

0 years 0 months 5 days 5 hours 57 minutes 36 seconds

Upvotes: 2

R Sahu
R Sahu

Reputation: 206577

You have the order of secDis and yearsSec reversed in the line to compute the number of days.

Also, the number of seconds in a day are: 24 x 60 x 60, which is 86400. You are off by an order of 60.

The number of seconds left after the number of years is (secDis % yearsSec). Hence, you need to use:

days = (secDis % yearsSec ) / 86400;

Upvotes: 3

Related Questions