โ† Back

27. Convert Seconds to Time Format

Medium

๐Ÿ“ Problem

Write a program to read the total seconds and print the seconds in time format Example :hr:min:sec

Input Format:
Accept integer as a input
Output Format:
Display the output in the format "**:HOUR **:MIN :**SEC"
Constraints:
1 โ‰ค sec โ‰ค 10^15
Sample Input 1:
54300
Sample Output 1:
15:HOUR 05:MIN :00SEC
Sample Input 2:
76000
Sample Output 2:
21:HOUR 06:MIN :40SEC
๐ŸŽฏ Time Conversion: Mastering Division & Modulo Operations!

The Challenge: Breaking Down Time Units

Converting total seconds into hours, minutes, and seconds is a classic problem that teaches you how to decompose a value into multiple units. This is fundamental to understanding time, currency, and any hierarchical unit system!

๐Ÿ’ก Why This Question is Special
This problem demonstrates the power of division (/) and modulo (%) operators working together. You'll learn how to extract different "layers" of information from a single number - a technique used everywhere from time calculations to currency conversions!

1๏ธโƒฃ Understanding Time Units

The Time Hierarchy:

๐Ÿ“Œ Time Unit Relationships:
1 minute  = 60 seconds
1 hour    = 60 minutes = 3600 seconds
1 day     = 24 hours   = 86400 seconds

// So for 54300 seconds:
// How many full hours? 54300 รท 3600 = 15 hours
// Remaining seconds? 54300 % 3600 = 300 seconds
// How many full minutes? 300 รท 60 = 5 minutes
// Remaining seconds? 300 % 60 = 0 seconds

2๏ธโƒฃ The Algorithm: Step-by-Step Breakdown

๐Ÿ“Œ Example with 54300 seconds:
long long totalSec = 54300;

// Step 1: Extract HOURS
long long hours = totalSec / 3600;
// 54300 / 3600 = 15 hours

// Step 2: Get remaining seconds after removing hours
totalSec = totalSec % 3600;
// 54300 % 3600 = 300 seconds left

// Step 3: Extract MINUTES from remaining seconds
long long minutes = totalSec / 60;
// 300 / 60 = 5 minutes

// Step 4: Get remaining SECONDS
long long seconds = totalSec % 60;
// 300 % 60 = 0 seconds

// Result: 15:HOUR 05:MIN :00SEC โœ“

3๏ธโƒฃ The Magic of Division (/) and Modulo (%)

Operation Symbol Purpose Example
Division / Counts how many times divisor fits 54300 / 3600 = 15
Modulo % Gets the remainder 54300 % 3600 = 300
๐Ÿ’ก The Perfect Partnership
Division (/) and Modulo (%) work together like a team:
  • / tells you "how many complete units"
  • % tells you "what's left over"
Together, they let you decompose any value into hierarchical components!

4๏ธโƒฃ Visual Breakdown of 76000 Seconds

๐Ÿ“Œ Let's Trace Through Example 2:
// Input: 76000 seconds

// Step 1: Get hours
hours = 76000 / 3600 = 21
// 21 complete hours

// Step 2: Remove hours, get remainder
remaining = 76000 % 3600 = 400
// 400 seconds left after removing 21 hours

// Step 3: Get minutes from remainder
minutes = 400 / 60 = 6
// 6 complete minutes

// Step 4: Get final seconds
seconds = 400 % 60 = 40
// 40 seconds remain

// Output: 21:HOUR 06:MIN :40SEC โœ“

// Verification:
// (21 ร— 3600) + (6 ร— 60) + 40 = 75600 + 360 + 40 = 76000 โœ“

5๏ธโƒฃ Understanding setfill and setw

Formatting the Output with Leading Zeros:

๐Ÿ“Œ Using iomanip for Formatting:
#include <iomanip>

// setfill('0') - Fill empty spaces with '0'
// setw(2) - Set width to 2 characters

cout << setfill('0') << setw(2) << 5;  // Prints: "05"
cout << setfill('0') << setw(2) << 15; // Prints: "15"

// Why we need this:
// Without formatting: "15:HOUR 5:MIN :0SEC" (looks wrong!)
// With formatting:    "15:HOUR 05:MIN :00SEC" (perfect!)
Manipulator Purpose Example
setfill('0') Set the fill character Fills empty spaces with '0'
setw(2) Set minimum width Ensures at least 2 digits

6๏ธโƒฃ Complete Solution Breakdown

๐Ÿ“Œ Full Code Explanation:
#include <iostream>
#include <iomanip>

using namespace std;

int main() {
    long long sec;
    cin >> sec;
    
    // Extract hours (3600 seconds = 1 hour)
    long long hours = sec / 3600;
    sec = sec % 3600;  // Update sec to remainder
    
    // Extract minutes (60 seconds = 1 minute)
    long long minutes = sec / 60;
    sec = sec % 60;    // Final seconds
    
    // Format and print with leading zeros
    cout << setfill('0') << setw(2) << hours << ":HOUR "
         << setw(2) << minutes << ":MIN :"
         << setw(2) << sec << "SEC";
    
    return 0;
}

7๏ธโƒฃ Real-World Applications

This technique is used everywhere:

  • Digital Clocks: Converting timestamps to readable time
  • Video Players: Showing duration (2:34:15)
  • Fitness Apps: Workout time tracking
  • Gaming: Countdown timers, session duration
  • Currency Conversion: Breaking down dollars into bills and coins
  • Date Calculations: Days โ†’ Years, Months, Days
  • File Size Display: Bytes โ†’ GB, MB, KB

8๏ธโƒฃ Extension Challenge: Other Conversions

๐Ÿ“Œ Same Logic, Different Units:
// Convert cents to dollars and cents
int totalCents = 567;
int dollars = totalCents / 100;  // 5 dollars
int cents = totalCents % 100;     // 67 cents

// Convert days to years, months, days (simplified)
int totalDays = 400;
int years = totalDays / 365;      // 1 year
int days = totalDays % 365;       // 35 days

// Convert bytes to GB, MB, KB
long long bytes = 5368709120;  // 5 GB
long long gb = bytes / (1024*1024*1024);
bytes = bytes % (1024*1024*1024);
long long mb = bytes / (1024*1024);
๐ŸŽ“ Key Takeaway: The combination of / and % is a fundamental pattern for decomposing values into hierarchical units. Master this, and you can convert anything!
๐Ÿ’ก Pro Tip: The Mathematical Invariant
For any division a / b and a % b:

a = (a / b) ร— b + (a % b)

This means you can always reconstruct the original number from the quotient and remainder!
Example: 54300 = (15 ร— 3600) + (5 ร— 60) + 0 โœ“
๐Ÿ’ป Solution Code
#include <iostream>
#include <iomanip>

using namespace std;

int main() {
    long long sec;
    cin >> sec;
    
    long long hours = sec / 3600;
    sec = sec % 3600;
    
    long long minutes = sec / 60;
    sec = sec % 60;
    
    cout << setfill('0') << setw(2) << hours << ":HOUR "
         << setw(2) << minutes << ":MIN :"
         << setw(2) << sec << "SEC";
    
    return 0;
}