Sha256: d56d09a79bb52f13dde2b18a9f51bb34102358b4eba4f5124b469ed53163604b

Contents?: true

Size: 1.39 KB

Versions: 78

Compression:

Stored size: 1.39 KB

Contents

unit uClock;

interface

type
  Clock = Record
  strict private
    class function fltMod(x, y: double): integer; static;
  private
    hours: integer;
    minutes: integer;
  public
    constructor SetHands(aHours: integer; aMinutes: integer=0);
    function Add(minutesToAdd: integer): Clock;
    function Subtract(minutesToSubtract: integer): Clock;
    function ToString: string;
    function Equal(aClock: Clock): Boolean;
  End;

implementation
uses SysUtils, math;

constructor Clock.SetHands(aHours: Integer; aMinutes: Integer = 0);
begin
  hours := fltMod((aHours * 60 + aMinutes) / 60.0, 24);
  minutes := fltMod(aMinutes, 60);
end;

function Clock.Subtract(minutesToSubtract: integer): Clock;
begin
  result := Clock.SetHands(hours, minutes - minutesToSubtract);
end;

function Clock.Add(minutesToAdd: Integer): Clock;
begin
  result := Clock.SetHands(hours, minutes + minutesToAdd);
end;

function Clock.ToString: string;
begin
  result := format('%.2d:%.2d',[hours, minutes]);
end;

function Clock.Equal(aClock: Clock): Boolean;
begin
  result := (aClock.hours = hours) and (aClock.minutes = minutes);
end;

class function Clock.fltMod(x, y: double): integer;
var intX, intY: integer;
    tmpFloat: double;
begin
  intX := trunc(x);
  tmpFloat := x - intX;
  intY := trunc(y);
  result := trunc((intX mod intY) + tmpFloat + intY) mod intY; //Work around for lack of floating point MOD capability
end;

end.

Version data entries

78 entries across 78 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/delphi/exercises/clock/uClockExample.pas
trackler-2.2.1.179 tracks/delphi/exercises/clock/uClockExample.pas
trackler-2.2.1.178 tracks/delphi/exercises/clock/uClockExample.pas
trackler-2.2.1.177 tracks/delphi/exercises/clock/uClockExample.pas
trackler-2.2.1.176 tracks/delphi/exercises/clock/uClockExample.pas
trackler-2.2.1.175 tracks/delphi/exercises/clock/uClockExample.pas
trackler-2.2.1.174 tracks/delphi/exercises/clock/uClockExample.pas
trackler-2.2.1.173 tracks/delphi/exercises/clock/uClockExample.pas
trackler-2.2.1.172 tracks/delphi/exercises/clock/uClockExample.pas
trackler-2.2.1.171 tracks/delphi/exercises/clock/uClockExample.pas
trackler-2.2.1.170 tracks/delphi/exercises/clock/uClockExample.pas
trackler-2.2.1.169 tracks/delphi/exercises/clock/uClockExample.pas
trackler-2.2.1.167 tracks/delphi/exercises/clock/uClockExample.pas
trackler-2.2.1.166 tracks/delphi/exercises/clock/uClockExample.pas
trackler-2.2.1.165 tracks/delphi/exercises/clock/uClockExample.pas
trackler-2.2.1.164 tracks/delphi/exercises/clock/uClockExample.pas
trackler-2.2.1.163 tracks/delphi/exercises/clock/uClockExample.pas
trackler-2.2.1.162 tracks/delphi/exercises/clock/uClockExample.pas
trackler-2.2.1.161 tracks/delphi/exercises/clock/uClockExample.pas
trackler-2.2.1.160 tracks/delphi/exercises/clock/uClockExample.pas