// Duration structure which will hold our time
struct Duration {
unsigned long years;
int days;
int hours;
int minutes;
int seconds;
Duration(): years(0), days(0), hours(0), minutes(0), seconds(0) { }
};
// Function that converts seconds into our duration time
// Supports up to 4,294,967,295 seconds on a 32-bit machine
// Limited by unsigned long size.
Duration GetDuration(unsigned long seconds) {
Duration timeDuration;
// Seconds in a year
timeDuration.years = seconds / 31557600;
seconds %= 31557600;
// Seconds in a day
timeDuration.days = seconds / 86400;
seconds %= 86400;
// Seconds in a hour
timeDuration.hours = seconds / 3600;
seconds %= 3600;
// Seconds in a minute
timeDuration.minutes = seconds / 60;
seconds %= 60;
// Rest is seconds
timeDuration.seconds = seconds;
return timeDuration;
}
// Typical use
Duration songDuration = GetDuration(353);
// Print number of minutes
cout << songDuration.Minutes << endl;
Posted: March 20, 2023
Return to the snippets listing