How to turn a total number of seconds into years, months, days, minutes and seconds in Pascal

2 Answers

0 votes
program SecondsBreakdown;

type
  TBreakdown = record
    Years   : LongInt;
    Months  : LongInt;
    Days    : LongInt;
    Hours   : LongInt;
    Minutes : LongInt;
    Seconds : LongInt;
  end;

function BreakdownSeconds(Total: LongInt): TBreakdown;
const
  SecPerMin  = 60;
  SecPerHour = 60 * SecPerMin;
  SecPerDay  = 24 * SecPerHour;
  SecPerMonth = 30 * SecPerDay;
  SecPerYear  = 365 * SecPerDay;
var
  R: TBreakdown;
begin
  R.Years := Total div SecPerYear;
  Total := Total mod SecPerYear;

  R.Months := Total div SecPerMonth;
  Total := Total mod SecPerMonth;

  R.Days := Total div SecPerDay;
  Total := Total mod SecPerDay;

  R.Hours := Total div SecPerHour;
  Total := Total mod SecPerHour;

  R.Minutes := Total div SecPerMin;
  Total := Total mod SecPerMin;

  R.Seconds := Total;

  BreakdownSeconds := R;
end;

var
  B: TBreakdown;
begin
  B := BreakdownSeconds(102300000);

  WriteLn('Years:   ', B.Years);
  WriteLn('Months:  ', B.Months);
  WriteLn('Days:    ', B.Days);
  WriteLn('Hours:   ', B.Hours);
  WriteLn('Minutes: ', B.Minutes);
  WriteLn('Seconds: ', B.Seconds);
end.





(*
run:

Years:   3
Months:  2
Days:    29
Hours:   0
Minutes: 40
Seconds: 0

*)


 



answered Jan 21 by avibootz
0 votes
program SecondsBreakdown;

type
  TTimeBreakdown = record
    Years   : LongInt;
    Months  : LongInt;
    Days    : LongInt;
    Hours   : LongInt;
    Minutes : LongInt;
    Seconds : LongInt;
 end;

{ Function using LongInt to handle up to ~2 billion seconds }
procedure ConvertSeconds(TotalSec: LongInt; var TB: TTimeBreakdown);
var
  Remainder: LongInt;
begin
  { Idiomatic Pascal uses DIV for division and MOD for remainder }
  TB.Years := TotalSec div 31536000; { 365 days }
  Remainder := TotalSec mod 31536000;

  TB.Months := Remainder div 2592000; { 30-day month approximation }
  Remainder := Remainder mod 2592000;

  TB.Days := Remainder div 86400;
  Remainder := Remainder mod 86400;

  TB.Hours := Remainder div 3600;
  Remainder := Remainder mod 3600;

  TB.Minutes := Remainder div 60;
  TB.Seconds := Remainder mod 60;
end;

var
  Input: LongInt;
  Result: TTimeBreakdown;

begin
  Input := 102300000;

  ConvertSeconds(Input, Result);

  WriteLn('Years:   ', Result.Years);
  WriteLn('Months:  ', Result.Months);
  WriteLn('Days:    ', Result.Days);
  WriteLn('Hours:   ', Result.Hours);
  WriteLn('Minutes: ', Result.Minutes);
  WriteLn('Seconds: ', Result.Seconds);
end.



(*
run:

Years:   3
Months:  2
Days:    29
Hours:   0
Minutes: 40
Seconds: 0

*)


 



answered Jan 21 by avibootz

Related questions

...