// YYYY.MM.DD-hh:mm:ss
unit dcommontime;

interface

uses
  ddateprimitives;

const
  COMMON_TIME_LENGTH = 19;

procedure BuildCommonTime(T: PtrInt;
                          P: PAnsiChar);

implementation

const
  DIGITS: array[0 .. 16 - 1] of AnsiChar = (
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
    '0', '0', '0', '0', '0', '0'
  );

procedure WriteTwoDigits(Num: LongInt; P: PAnsiChar); inline;
var
  Temp: LongInt;
begin
  Temp := Num div 10;
  P^ := DIGITS[Temp and $f];
  (P + 1)^ := DIGITS[(Num - Temp * 10) and $f];
end;

procedure WriteFourDigits(Num: LongInt; P: PAnsiChar); inline;
var
  Temp: LongInt;
begin
  Temp := Num div 10;
  (P + 3)^ := DIGITS[(Num - Temp * 10) and $f];
  Num := Temp div 10;
  (P + 2)^ := DIGITS[(Temp - Num * 10) and $f];
  WriteTwoDigits(Num, P);
end;

procedure BuildCommonTime(T: PtrInt;
                          P: PAnsiChar);
var
  T2: LongInt;
  Civil: TYearMonthDay;
begin
  T2 := T div 60;
  WriteTwoDigits(T - T2 * 60, P + 17);
  (P + 16)^ := ':';
  T := T2 div 60;
  WriteTwoDigits(T2 - T * 60, P + 14);
  (P + 13)^ := ':';
  T2 := T div 24;
  WriteTwoDigits(T - T2 * 24, P + 11);
  (P + 10)^ := '-';
  civil_from_days(T2, Civil);
  WriteTwoDigits(Civil.Day, P + 8);
  (P + 7)^ := '.';
  WriteTwoDigits(Civil.Month, P + 5);
  (P + 4)^ := '.';
  WriteFourDigits(Civil.Year, P);
end;

end.
