How to convert seconds into years, months, weeks, days, hours, minutes, and seconds in C++

2 Answers

0 votes
#include <iostream>

/*
Use integer division and modulo:

1 minute = 60 seconds
1 hour   = 60 minutes
1 day    = 24 hours
1 week   = 7 days
1 month  = 30.44 days  (2,629,743 seconds)
1 year   = 365.25 days (31,557,600 seconds)
*/

/*
A leap‑year cycle has:
3 normal years -> 3 × 365 = 1095 days
1 leap year -> 366 days
Total: 1095 + 366 = 1461 days
Average per year: 1461 / 4 365.25 days

Convert days -> hours 365.25 * 24 hours/day = 8766 hours
Convert hours -> minutes 8766 * 60 minutes/hour =  525960 minutes
Convert minutes -> seconds 525960 * 60 seconds/minute = 31557600 seconds
*/

/*
    Convert a total number of seconds into years, months, weeks,
    days, hours, minutes, and seconds. The function receives the
    total seconds and outputs each component by reference.
*/
void convertSeconds(long long totalSeconds,
                    long long &years,
                    long long &months,
                    long long &weeks,
                    long long &days,
                    long long &hours,
                    long long &minutes,
                    long long &seconds)
{
    const long long SECS_PER_MIN   = 60;
    const long long SECS_PER_HOUR  = 3600;
    const long long SECS_PER_DAY   = 86400;
    const long long SECS_PER_WEEK  = 604800;
    
    // 4-year cycle: 3 normal years (365*3) + 1 leap year (366) = 1461 days
    const long long SECS_PER_4YEARS = 1461 * SECS_PER_DAY; 
    const long long SECS_PER_YEAR   = 365 * SECS_PER_DAY; // Base year

    // 1. Extract 4-year blocks first to handle leap accuracy safely
    long long fourYearCycles = totalSeconds / SECS_PER_4YEARS;
    totalSeconds %= SECS_PER_4YEARS;

    // 2. Extract remaining individual years (max 3 left)
    years = (fourYearCycles * 4) + (totalSeconds / SECS_PER_YEAR);
    // If it's exactly the end of the 4th year (leap day), cap it at 3
    if (years / 4 > fourYearCycles && totalSeconds / SECS_PER_YEAR == 4) {
        years--;
    }
    totalSeconds %= SECS_PER_YEAR;

    // 3. Extract months using a clean average relative to a standard year
    // 31,536,000 / 12 = 2,628,000 seconds per month
    const long long SECS_PER_MONTH = 2628000; 
    months = totalSeconds / SECS_PER_MONTH;
    totalSeconds %= SECS_PER_MONTH;

    // 4. Standard remainder cascade for the rest
    weeks = totalSeconds / SECS_PER_WEEK;
    totalSeconds %= SECS_PER_WEEK;

    days = totalSeconds / SECS_PER_DAY;
    totalSeconds %= SECS_PER_DAY;

    hours = totalSeconds / SECS_PER_HOUR;
    totalSeconds %= SECS_PER_HOUR;

    minutes = totalSeconds / SECS_PER_MIN;
    seconds = totalSeconds % SECS_PER_MIN;
}

int main() {
    long long input = 101000000; 

    long long y, mo, w, d, h, m, s;
    convertSeconds(input, y, mo, w, d, h, m, s);

    std::cout << y  << " years, "
              << mo << " months, "
              << w  << " week, "
              << d  << " days, "
              << h  << " hours, "
              << m  << " minutes, "
              << s  << " seconds\n";
}


/*
run:

3 years, 2 months, 1 week, 6 days, 3 hours, 33 minutes, 20 seconds

*/

 



answered 14 hours ago by avibootz
edited 13 hours ago by avibootz
0 votes
#include <iostream>

// A structure to bundle all our time units together
struct TimeBreakdown {
    long long years;
    long long months;
    long long weeks;
    long long days;
    long long hours;
    long long minutes;
    long long seconds;
};

// Function definition
TimeBreakdown convert_seconds(long long total_seconds) {
    // Define time constants in seconds
    const long long SECONDS_IN_YEAR   = 31536000; // 365 days
    const long long SECONDS_IN_MONTH  = 2592000;  // 30 days
    const long long SECONDS_IN_WEEK   = 604800;
    const long long SECONDS_IN_DAY    = 86400;
    const long long SECONDS_IN_HOUR   = 3600;
    const long long SECONDS_IN_MINUTE = 60;

    TimeBreakdown result;
    long long remainder = total_seconds;

    // Sequential breakdown using division and modulo
    result.years = remainder / SECONDS_IN_YEAR;
    remainder %= SECONDS_IN_YEAR;

    result.months = remainder / SECONDS_IN_MONTH;
    remainder %= SECONDS_IN_MONTH;

    result.weeks = remainder / SECONDS_IN_WEEK;
    remainder %= SECONDS_IN_WEEK;

    result.days = remainder / SECONDS_IN_DAY;
    remainder %= SECONDS_IN_DAY;

    result.hours = remainder / SECONDS_IN_HOUR;
    remainder %= SECONDS_IN_HOUR;

    result.minutes = remainder / SECONDS_IN_MINUTE;
    result.seconds = remainder % SECONDS_IN_MINUTE;

    return result;
}

int main() {
    long long seconds = 101000000;

    TimeBreakdown time = convert_seconds(seconds);

    // Print the results using the returned struct
    std::cout << "Breakdown of " << seconds << " seconds:\n";
    std::cout << "Years:   " << time.years << "\n"
              << "Months:  " << time.months << "\n"
              << "Weeks:   " << time.weeks << "\n"
              << "Days:    " << time.days << "\n"
              << "Hours:   " << time.hours << "\n"
              << "Minutes: " << time.minutes << "\n"
              << "Seconds: " << time.seconds << "\n";
}



/*
run:

Breakdown of 101000000 seconds:
Years:   3
Months:  2
Weeks:   1
Days:    6
Hours:   23
Minutes: 33
Seconds: 20

*/

 



answered 13 hours ago by avibootz
...