using System;
public struct TimeBreakdown
{
public int Years { get; }
public int Months { get; }
public int Days { get; }
public int Hours { get; }
public int Minutes { get; }
public int Seconds { get; }
public TimeBreakdown(int years, int months, int days, int hours, int minutes, int seconds) {
Years = years;
Months = months;
Days = days;
Hours = hours;
Minutes = minutes;
Seconds = seconds;
}
}
public class Program
{
public static void Main()
{
long inputSeconds = 102_420_852;
TimeBreakdown result = GetTimeBreakdown(inputSeconds);
Console.WriteLine($"Years: {result.Years}");
Console.WriteLine($"Months: {result.Months}");
Console.WriteLine($"Days: {result.Days}");
Console.WriteLine($"Hours: {result.Hours}");
Console.WriteLine($"Minutes: {result.Minutes}");
Console.WriteLine($"Seconds: {result.Seconds}");
}
public static TimeBreakdown GetTimeBreakdown(long totalSeconds)
{
// Safe reference date to avoid overflow
DateTime start = new DateTime(2000, 1, 1);
DateTime end = start.AddSeconds(totalSeconds);
// Calculate years
int years = end.Year - start.Year;
DateTime temp = start.AddYears(years);
if (temp > end) {
years--;
temp = start.AddYears(years);
}
// Calculate months
int months = 0;
while (temp.AddMonths(1) <= end) {
temp = temp.AddMonths(1);
months++;
}
// Remaining time
TimeSpan remaining = end - temp;
return new TimeBreakdown(
years,
months,
remaining.Days,
remaining.Hours,
remaining.Minutes,
remaining.Seconds
);
}
}
/*
run:
Years: 3
Months: 2
Days: 30
Hours: 10
Minutes: 14
Seconds: 12
*/