1

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.

TemplateRex
  • 69,038
  • 19
  • 164
  • 304
RexBruin17
  • 11
  • 2
  • 2
    Daunting task. It is impossible to say how many seconds are in the year in general. – SergeyA Apr 08 '16 at 20:39
  • If this is a homework task, please consider that you wont learn anything form it if you get the solution from someone else. I would suggest you to take pen and paper and try to figure out how to do it – 463035818_is_not_an_ai Apr 08 '16 at 20:45
  • It is a basic a 365 day year. – RexBruin17 Apr 08 '16 at 22:09
  • Also, I am not asking anyone to solve it. I just got stumped. If i can get past days, then I can figure everything else out. – RexBruin17 Apr 08 '16 at 22:10
  • If you define a year as 365 days (as distinct from accounting for leap years) then 60 second per minute, 60 minutes per hour, 24 hours per day, 365 days per year. All of those can be done by successive division. – Peter Apr 08 '16 at 22:26

4 Answers4

3

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;
R Sahu
  • 204,454
  • 14
  • 159
  • 270
2

@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

Community
  • 1
  • 1
TemplateRex
  • 69,038
  • 19
  • 164
  • 304
1

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.

Community
  • 1
  • 1
Howard Hinnant
  • 206,506
  • 52
  • 449
  • 577
0

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;
// ...
molbdnilo
  • 64,751
  • 3
  • 43
  • 82