Imports System
Module Program
' Structure to hold the broken-down time units
Public Structure TimeBreakdown
Public Years As Integer
Public Months As Integer
Public Days As Integer
Public Hours As Integer
Public Minutes As Integer
Public Seconds As Integer
End Structure
''' <summary>
''' Converts a total number of seconds into a calendar-aware breakdown.
''' </summary>
Public Function GetTimeBreakdown(totalSeconds As Long) As TimeBreakdown
' 1. Anchor to a reference point to account for leap years/month lengths
Dim startDate As DateTime = New DateTime(1, 1, 1)
Dim endDate As DateTime = startDate.AddSeconds(totalSeconds)
Dim tb As New TimeBreakdown()
' 2. Calculate Years
tb.Years = endDate.Year - startDate.Year
Dim current As DateTime = startDate.AddYears(tb.Years)
' Adjust if years overshoot the date
If current > endDate Then
tb.Years -= 1
current = startDate.AddYears(tb.Years)
End If
' 3. Calculate Months
While current.AddMonths(1) <= endDate
current = current.AddMonths(1)
tb.Months += 1
End While
' 4. Use TimeSpan for the remaining precise units
Dim remaining As TimeSpan = endDate - current
tb.Days = remaining.Days
tb.Hours = remaining.Hours
tb.Minutes = remaining.Minutes
tb.Seconds = remaining.Seconds
Return tb
End Function
Sub Main()
Dim inputSeconds As Long = 102420852
Dim result As TimeBreakdown = 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}")
End Sub
End Module
' run:
'
' Years: 3
' Months: 2
' Days: 30
' Hours: 10
' Minutes: 14
' Seconds: 12
'