Quick actions

cmd+k|ctrl+k

Navigation

Languages

Seconds To Time

Snippet info

Language

Cpp

Visibility

public

Author

codecannamw

Created

2023-09-15T22:41:55.310444Z

Updated

2023-09-15T22:50:52.610026Z

#include <iostream>
using namespace std;

// Turn seconds into a more readable length of time
void secondsToTime(long long durationInSeconds) {
    // First lets get out seconds
    unsigned int seconds = durationInSeconds % 60;
    // Second lets convert the seconds to minutes
    unsigned long long minutes = durationInSeconds / 60;
    // Then we can figure out our hours
    unsigned long long hours = minutes / 60;
    // We set minutes to the remaineder of minutes left over from converting to hours
    minutes = minutes % 60;
    
    unsigned long long days = hours / 24;
    hours = hours % 24;
    
    // lets get weeks
    unsigned long long weeks = days / 7;
    days = days % 7;
    
    // Get months
    unsigned long long months = weeks / 4;
    weeks = weeks % 4;
    
    unsigned long long years = months / 12;
    months = months % 12;
    
    cout << years << " year(s) " << months << " month(s) " << weeks << " week(s) " << days << " day(s) " << " " << hours << ":" << minutes << ":" << seconds << "\n";
}

int main() {
    //           (100000000000);
    secondsToTime(4847348848383700000);
    secondsToTime(9999999999999999999);
    return 0;
}
INFO