csodaszarvas-gh's solution

to Gigasecond in the Java Track

Published at Jan 18 2020 · 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.

Setup

Go through the setup instructions for Java to install the necessary dependencies:

https://exercism.io/tracks/java/installation

Running the tests

You can run all the tests for an exercise by entering the following in your terminal:

``````\$ gradle test
``````

Use `gradlew.bat` if you're on Windows

In the test suites all tests but the first have been skipped.

Once you get a test passing, you can enable the next one by removing the `@Ignore("Remove to run test")` annotation.

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 can see how others have completed the exercise.

GigasecondTest.java

``````import org.junit.Test;
import org.junit.Ignore;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Month;

import static org.junit.Assert.assertEquals;

public class GigasecondTest {

@Test
public void modernTime() {
Gigasecond gigaSecond = new Gigasecond(LocalDate.of(2011, Month.APRIL, 25));

assertEquals(LocalDateTime.of(2043, Month.JANUARY, 1, 1, 46, 40), gigaSecond.getDateTime());
}

@Ignore("Remove to run test")
@Test
public void afterEpochTime() {
Gigasecond gigaSecond = new Gigasecond(LocalDate.of(1977, Month.JUNE, 13));

assertEquals(LocalDateTime.of(2009, Month.FEBRUARY, 19, 1, 46, 40), gigaSecond.getDateTime());
}

@Ignore("Remove to run test")
@Test
public void beforeEpochTime() {
Gigasecond gigaSecond = new Gigasecond(LocalDate.of(1959, Month.JULY, 19));

assertEquals(LocalDateTime.of(1991, Month.MARCH, 27, 1, 46, 40), gigaSecond.getDateTime());
}

@Ignore("Remove to run test")
@Test
public void withFullTimeSpecified() {
Gigasecond gigaSecond = new Gigasecond(LocalDateTime.of(2015, Month.JANUARY, 24, 22, 0, 0));

assertEquals(LocalDateTime.of(2046, Month.OCTOBER, 2, 23, 46, 40), gigaSecond.getDateTime());
}

@Ignore("Remove to run test")
@Test
public void withFullTimeSpecifiedAndDayRollover() {
Gigasecond gigaSecond = new Gigasecond(LocalDateTime.of(2015, Month.JANUARY, 24, 23, 59, 59));

assertEquals(LocalDateTime.of(2046, Month.OCTOBER, 3, 1, 46, 39), gigaSecond.getDateTime());
}
}``````
``````import java.time.LocalDate;
import java.time.LocalDateTime;

class Gigasecond {

private static final long GIGASECOND = 1000000000;

LocalDateTime moment;

Gigasecond(LocalDate moment) {
this.moment = moment.atStartOfDay();
}

Gigasecond(LocalDateTime moment) {
this.moment = moment;
}

LocalDateTime getDateTime() {
return moment.plusSeconds(GIGASECOND);
}

}``````