ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# freddie2025's solution

## to Clock in the C# Track

Published at Sep 17 2019 · 0 comments
Instructions
Test suite
Solution

#### Note:

This exercise has changed since this solution was written.

Implement a clock that handles times without dates.

You should be able to add and subtract minutes to it.

Two clocks that represent the same time should be equal to each other.

## Running the tests

To run the tests, run the command `dotnet test` from within the exercise directory.

Initially, only the first test will be enabled. This is to encourage you to solve the exercise one step at a time. Once you get the first test passing, remove the `Skip` property from the next test and work on getting that test passing. Once none of the tests are skipped and they are all passing, you can submit your solution using `exercism submit Clock.cs`

## Further information

For more detailed information about the C# track, including how to get help if you're having trouble, please visit the exercism.io C# language page.

## Source

Pairing session with Erin Drummond https://twitter.com/ebdrummond

### ClockTest.cs

``````// This file was auto-generated based on version 2.4.0 of the canonical data.

using Xunit;

public class ClockTest
{
[Fact]
public void On_the_hour()
{
var sut = new Clock(8, 0);
Assert.Equal("08:00", sut.ToString());
}

[Fact(Skip = "Remove to run test")]
public void Past_the_hour()
{
var sut = new Clock(11, 9);
Assert.Equal("11:09", sut.ToString());
}

[Fact(Skip = "Remove to run test")]
public void Midnight_is_zero_hours()
{
var sut = new Clock(24, 0);
Assert.Equal("00:00", sut.ToString());
}

[Fact(Skip = "Remove to run test")]
public void Hour_rolls_over()
{
var sut = new Clock(25, 0);
Assert.Equal("01:00", sut.ToString());
}

[Fact(Skip = "Remove to run test")]
public void Hour_rolls_over_continuously()
{
var sut = new Clock(100, 0);
Assert.Equal("04:00", sut.ToString());
}

[Fact(Skip = "Remove to run test")]
public void Sixty_minutes_is_next_hour()
{
var sut = new Clock(1, 60);
Assert.Equal("02:00", sut.ToString());
}

[Fact(Skip = "Remove to run test")]
public void Minutes_roll_over()
{
var sut = new Clock(0, 160);
Assert.Equal("02:40", sut.ToString());
}

[Fact(Skip = "Remove to run test")]
public void Minutes_roll_over_continuously()
{
var sut = new Clock(0, 1723);
Assert.Equal("04:43", sut.ToString());
}

[Fact(Skip = "Remove to run test")]
public void Hour_and_minutes_roll_over()
{
var sut = new Clock(25, 160);
Assert.Equal("03:40", sut.ToString());
}

[Fact(Skip = "Remove to run test")]
public void Hour_and_minutes_roll_over_continuously()
{
var sut = new Clock(201, 3001);
Assert.Equal("11:01", sut.ToString());
}

[Fact(Skip = "Remove to run test")]
public void Hour_and_minutes_roll_over_to_exactly_midnight()
{
var sut = new Clock(72, 8640);
Assert.Equal("00:00", sut.ToString());
}

[Fact(Skip = "Remove to run test")]
public void Negative_hour()
{
var sut = new Clock(-1, 15);
Assert.Equal("23:15", sut.ToString());
}

[Fact(Skip = "Remove to run test")]
public void Negative_hour_rolls_over()
{
var sut = new Clock(-25, 0);
Assert.Equal("23:00", sut.ToString());
}

[Fact(Skip = "Remove to run test")]
public void Negative_hour_rolls_over_continuously()
{
var sut = new Clock(-91, 0);
Assert.Equal("05:00", sut.ToString());
}

[Fact(Skip = "Remove to run test")]
public void Negative_minutes()
{
var sut = new Clock(1, -40);
Assert.Equal("00:20", sut.ToString());
}

[Fact(Skip = "Remove to run test")]
public void Negative_minutes_roll_over()
{
var sut = new Clock(1, -160);
Assert.Equal("22:20", sut.ToString());
}

[Fact(Skip = "Remove to run test")]
public void Negative_minutes_roll_over_continuously()
{
var sut = new Clock(1, -4820);
Assert.Equal("16:40", sut.ToString());
}

[Fact(Skip = "Remove to run test")]
public void Negative_sixty_minutes_is_previous_hour()
{
var sut = new Clock(2, -60);
Assert.Equal("01:00", sut.ToString());
}

[Fact(Skip = "Remove to run test")]
public void Negative_hour_and_minutes_both_roll_over()
{
var sut = new Clock(-25, -160);
Assert.Equal("20:20", sut.ToString());
}

[Fact(Skip = "Remove to run test")]
public void Negative_hour_and_minutes_both_roll_over_continuously()
{
var sut = new Clock(-121, -5810);
Assert.Equal("22:10", sut.ToString());
}

[Fact(Skip = "Remove to run test")]
{
var sut = new Clock(10, 0);
}

[Fact(Skip = "Remove to run test")]
{
var sut = new Clock(6, 41);
}

[Fact(Skip = "Remove to run test")]
{
var sut = new Clock(0, 45);
}

[Fact(Skip = "Remove to run test")]
{
var sut = new Clock(10, 0);
}

[Fact(Skip = "Remove to run test")]
{
var sut = new Clock(0, 45);
}

[Fact(Skip = "Remove to run test")]
{
var sut = new Clock(23, 59);
}

[Fact(Skip = "Remove to run test")]
{
var sut = new Clock(5, 32);
}

[Fact(Skip = "Remove to run test")]
{
var sut = new Clock(1, 1);
}

[Fact(Skip = "Remove to run test")]
public void Subtract_minutes()
{
var sut = new Clock(10, 3);
Assert.Equal("10:00", sut.Subtract(3).ToString());
}

[Fact(Skip = "Remove to run test")]
public void Subtract_to_previous_hour()
{
var sut = new Clock(10, 3);
Assert.Equal("09:33", sut.Subtract(30).ToString());
}

[Fact(Skip = "Remove to run test")]
public void Subtract_more_than_an_hour()
{
var sut = new Clock(10, 3);
Assert.Equal("08:53", sut.Subtract(70).ToString());
}

[Fact(Skip = "Remove to run test")]
public void Subtract_across_midnight()
{
var sut = new Clock(0, 3);
Assert.Equal("23:59", sut.Subtract(4).ToString());
}

[Fact(Skip = "Remove to run test")]
public void Subtract_more_than_two_hours()
{
var sut = new Clock(0, 0);
Assert.Equal("21:20", sut.Subtract(160).ToString());
}

[Fact(Skip = "Remove to run test")]
public void Subtract_more_than_two_hours_with_borrow()
{
var sut = new Clock(6, 15);
Assert.Equal("03:35", sut.Subtract(160).ToString());
}

[Fact(Skip = "Remove to run test")]
public void Subtract_more_than_one_day_1500_min_25_hrs_()
{
var sut = new Clock(5, 32);
Assert.Equal("04:32", sut.Subtract(1500).ToString());
}

[Fact(Skip = "Remove to run test")]
public void Subtract_more_than_two_days()
{
var sut = new Clock(2, 20);
Assert.Equal("00:20", sut.Subtract(3000).ToString());
}

[Fact(Skip = "Remove to run test")]
public void Clocks_with_same_time()
{
var sut = new Clock(15, 37);
Assert.Equal(new Clock(15, 37), sut);
}

[Fact(Skip = "Remove to run test")]
public void Clocks_a_minute_apart()
{
var sut = new Clock(15, 37);
Assert.NotEqual(new Clock(15, 36), sut);
}

[Fact(Skip = "Remove to run test")]
public void Clocks_an_hour_apart()
{
var sut = new Clock(15, 37);
Assert.NotEqual(new Clock(14, 37), sut);
}

[Fact(Skip = "Remove to run test")]
public void Clocks_with_hour_overflow()
{
var sut = new Clock(34, 37);
Assert.Equal(new Clock(10, 37), sut);
}

[Fact(Skip = "Remove to run test")]
public void Clocks_with_hour_overflow_by_several_days()
{
var sut = new Clock(99, 11);
Assert.Equal(new Clock(3, 11), sut);
}

[Fact(Skip = "Remove to run test")]
public void Clocks_with_negative_hour()
{
var sut = new Clock(-2, 40);
Assert.Equal(new Clock(22, 40), sut);
}

[Fact(Skip = "Remove to run test")]
public void Clocks_with_negative_hour_that_wraps()
{
var sut = new Clock(-31, 3);
Assert.Equal(new Clock(17, 3), sut);
}

[Fact(Skip = "Remove to run test")]
public void Clocks_with_negative_hour_that_wraps_multiple_times()
{
var sut = new Clock(-83, 49);
Assert.Equal(new Clock(13, 49), sut);
}

[Fact(Skip = "Remove to run test")]
public void Clocks_with_minute_overflow()
{
var sut = new Clock(0, 1441);
Assert.Equal(new Clock(0, 1), sut);
}

[Fact(Skip = "Remove to run test")]
public void Clocks_with_minute_overflow_by_several_days()
{
var sut = new Clock(2, 4322);
Assert.Equal(new Clock(2, 2), sut);
}

[Fact(Skip = "Remove to run test")]
public void Clocks_with_negative_minute()
{
var sut = new Clock(3, -20);
Assert.Equal(new Clock(2, 40), sut);
}

[Fact(Skip = "Remove to run test")]
public void Clocks_with_negative_minute_that_wraps()
{
var sut = new Clock(5, -1490);
Assert.Equal(new Clock(4, 10), sut);
}

[Fact(Skip = "Remove to run test")]
public void Clocks_with_negative_minute_that_wraps_multiple_times()
{
var sut = new Clock(6, -4305);
Assert.Equal(new Clock(6, 15), sut);
}

[Fact(Skip = "Remove to run test")]
public void Clocks_with_negative_hours_and_minutes()
{
var sut = new Clock(-12, -268);
Assert.Equal(new Clock(7, 32), sut);
}

[Fact(Skip = "Remove to run test")]
public void Clocks_with_negative_hours_and_minutes_that_wrap()
{
var sut = new Clock(-54, -11513);
Assert.Equal(new Clock(18, 7), sut);
}

[Fact(Skip = "Remove to run test")]
public void Full_clock_and_zeroed_clock()
{
var sut = new Clock(0, 0);
Assert.Equal(new Clock(24, 0), sut);
}

[Fact(Skip = "Remove to run test")]
public void Clocks_are_immutable()
{
var sut = new Clock(0, 0);
Assert.NotEqual(sutPlus1, sut);
}
}``````
``````using System;

public class Clock
{
public Clock(int hours, int minutes)
{
if (minutes < 0)
{
hours = hours + minutes / 60 - 1;
minutes = 60 + minutes % 60;
}

Minutes = minutes % 60;

if (hours < 0)
{
hours = 24 + hours % 24;
}

Hours = (hours + minutes / 60) % 24;

}

public int Hours { get; } = 0;
public int Minutes { get; } = 0;

{
return new Clock(Hours, Minutes + minutesToAdd);
}

public Clock Subtract(int minutesToSubtract)
{
return new Clock(Hours, Minutes - minutesToSubtract);
}

public override string ToString()
{
return \$"{Hours:D2}:{Minutes:D2}";
}

public bool Equals(Clock other)
{
if (other == null) return false;

if (this.Hours == other.Hours && this.Minutes == other.Minutes)
{
return true;
}
else
{
return false;
}
}

public override bool Equals(object obj)
{
if (obj == null || GetType() != obj.GetType())
{
return false;
}
else
{
Clock clockObj = obj as Clock;
return clockObj == null ? false : Equals(clockObj);
}
}

public static bool operator == (Clock clock1, Clock clock2)
{
if (((object)clock1) == null || ((object)clock2) == null)
return Object.Equals(clock1, clock2);

return clock1.Equals(clock2);
}

public static bool operator != (Clock clock1, Clock clock2)
{
if (((object)clock1) == null || ((object)clock2) == null)
return ! Object.Equals(clock1, clock2);

return ! (clock1.Equals(clock2));
}

public override int GetHashCode()
{
unchecked
{
return (Hours * 60 + Minutes);
}
}
}``````

### 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?