🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of gottaTw

gottaTw's solution

to Gigasecond in the Delphi Pascal Track

Published at Mar 12 2021 · 0 comments
Instructions
Test suite
Solution

Given a moment, determine the moment that would be after a gigasecond has passed.

A gigasecond is 10^9 (1,000,000,000) seconds.

Testing

In order to run the tests for this track, you will need to install DUnitX. Please see the installation instructions for more information.

Loading Exercises into Delphi

If Delphi is properly installed, and *.dpr file types have been associated with Delphi, then double clicking the supplied *.dpr file will start Delphi and load the exercise/project. control + F9 is the keyboard shortcut to compile the project or pressing F9 will compile and run the project.

Alternatively you may opt to start Delphi and load your project via. the File drop down menu.

When Questions Come Up

We monitor the Pascal-Delphi support room on gitter.im to help you with any questions that might arise.

Submitting Exercises

Note that, when trying to submit an exercise, make sure the exercise file you're submitting is in the exercism/delphi/<exerciseName> directory.

For example, if you're submitting ubob.pas for the Bob exercise, the submit command would be something like exercism submit <path_to_exercism_dir>/delphi/bob/ubob.pas.

Source

Chapter 9 in Chris Pine's online Learn to Program tutorial. http://pine.fm/LearnToProgram/?Chapter=09

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you may request help from a mentor.

uGigasecondTests.pas

unit uGigasecondTests;

interface
uses
  DUnitX.TestFramework;

const
  CanonicalVersion = '2.0.0.1';

type
  [TestFixture]
  TGigasecondTest = class(TObject)
  public
    [Test]
//    [Ignore('Comment the "[Ignore]" statement to run the test')]
    procedure date_only_specification_of_time;

    [Test]
    [Ignore]
    procedure second_test_for_date_only_specification_of_time;

    [Test]
    [Ignore]
    procedure third_test_for_date_only_specification_of_time;

    [Test]
    [Ignore]
    procedure full_time_specified;

    [Test]
    [Ignore]
    procedure full_time_with_day_roll_over;
  end;

implementation
uses
  uGigasecond;

procedure TGigasecondTest.date_only_specification_of_time;
begin
  Assert.AreEqual('2043-01-01T01:46:40' ,TGigasecond.Add('2011-04-25'));
end;

procedure TGigasecondTest.full_time_specified;
begin
  Assert.AreEqual('2046-10-02T23:46:40' ,TGigasecond.Add('2015-01-24T22:00:00'));
end;

procedure TGigasecondTest.full_time_with_day_roll_over;
begin
  Assert.AreEqual('2046-10-03T01:46:39' ,TGigasecond.Add('2015-01-24T23:59:59'));
end;

procedure TGigasecondTest.second_test_for_date_only_specification_of_time;
begin
  Assert.AreEqual('2009-02-19T01:46:40' ,TGigasecond.Add('1977-06-13'));
end;

procedure TGigasecondTest.third_test_for_date_only_specification_of_time;
begin
  Assert.AreEqual('1991-03-27T01:46:40' ,TGigasecond.Add('1959-07-19'));
end;

initialization
  TDUnitX.RegisterTestFixture(TGigasecondTest);
end.
unit uGigasecond;

interface
uses
  DateUtils,
  Sysutils;

type
  TGigasecond = class
  public
    class function Add(const aInput: string): string;
  end;

implementation

class function TGigasecond.Add(const aInput: string): string;
const
  Gigasecond: Integer = 1000000000;
var
  OldTime: TDateTime;
  NewTime: TDateTime;
begin
  OldTime := ISO8601ToDate(aInput);
  NewTime := IncSecond(OldTime, GigaSecond);
  Result := FormatDateTime('yyyy-mm-dd"T"hh:mm:ss', NewTime);
end;

end.

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?